The best Laravel developer knows that poor management of codes is bound to crash eventually. Code writing is fun but not like managing the code and dependencies when dealing with the continuous growth of your codebase and feature branches.
As a developer aim to make the lives of others more comfortable and less complicated. One way to do so is the use of continuous integration (CI) tools. In this article, we have several integration tools and software that you can use to adopt a more refined programming practice to prevent dangerous integration pitfalls.
CI Tools automate many tedious tasks and make it easier to quickly backtrack before you end up releasing a disaster, all while keeping a clean and tidy record of the growth of your project. The idea is to set up various tests for our code, without having to provision or maintain any servers. Create a quick feedback cycle that will catch errors and fix them before becoming a nuisance.
- Code analysis tests
Laravel developers use Code analysis to scanning the source for certain structures that may point to deeper problems, such as design flaws and bad coding practices, usually called code smells.
Some code analysis tools focus on finding out bad patterns: functions with too many parameters, fat classes, or too deeply nested structures while others check for style: indentation rules, name conventions, etc.
Every analysis must start with some standard to follow. These rules are subjective but informed by previous experience. The rules can be generally configured and customized.
- PHP Mess Detector
PHP Mess Detector (phpmd) checks for code smells awkward, overcomplicated, or unused code. The PMD project inspires it. phpmd ships with several rulesets that can be enabled or disabled independently.
Built-in rules are:
- cleancode: enforce clean code base rules.
- codesize: complexity rules, excessively long classes, etc.
- controversial: camelcase rules, globals, etc.
- design: forbid eval, goto, exit. Also, coupling and depth rules.
- naming: long and short identifier names, method name rules.
- unusedcode: dead code and unused variables rules.
If you’ve never done any code analysis on your project before, it is likely to make you want to pull your hairs out. Don’t worry and be patient, after we have gotten our code in order. Our lives will be more comfortable.
Instead of setting the rules by command line, we can create an XML file that can be checked in source control
- PHP Code Sniffer
PHP Code sniffer (phpcs) is a style checker. If you’ve ever used a linter (jshint, pylint, checkstyle, etc.), you already know what it does. Phpcs can check for indentation, missing comments, naming conventions, etc.
PHP Code sniffer ships with various popular PHP styles such as PEAR, PSR2, and Zend among others. We can also make our own rules or mix and match checks from existing ones.
PHP Code Sniffer also includes phpcbf. A program then can automatically fix some problems.
- PHP Copy Paste Detector
PHP Copy Paste Detector (phpcpd) does what it says on the tin: finds duplicate code inside your project.
Having duplicate code usually signals the need for refactoring, the repeated parts should find a new home in a shared library or component. Duplicates also force developers to make shotgun surgery: a single change must be repeated multiple times.
- Unit testing: phpunit
Unit testing ensures that our implementation does what it has been designed to do. Units are the smallest testable pieces of code, e.g., a class method, a function, an API call.
Unit tests also act as a form of living documentation, by reading what they do, we can infer how the tested parts should work, what inputs do they take, and what outputs should they provide. They also validate that code still valid after refactoring.
As the new code is being written, we should also be creating tests to validate its behavior through the hire Laravel developer.
- Browser tests: Laravel Dusk
Dusk interfaces with a real Chrome browser to programmatically browse sites, perform actions, select elements, and do assertions.
- Security tests: Sensiolabs
With SensioLabs security checker, we can scan our project dependencies for known vulnerabilities. It scans our composer file for dependencies and runs them down through a vulnerabilities database:
- Running Continuous Integration with Semaphore
- Continuous Integration (CI) allows testing early and test often.
- We can set up a CI pipeline to build the application on every push. The pipeline can drive the tests and optionally deploy the application.
Semaphore is a cloud-hosted continuous integration and delivery service. It’s easy to use, really fast, and there is no need to install any software, manage any servers, or maintain any systems. Semaphore fully supports Linux and macOS applications. The best thing is we can set up the whole process from a single config file.
Getting started with Semaphore is a breeze if you don’t have any, repositories you may fork Semaphore’s PHP Laravel Demo. Alternatively, we create our project with the command line tool.
- The Semaphore pipeline
The semaphore file is found at .semaphore/semaphore.yml. This is the configuration we’ll be using:
Let us walk through a pipeline that includes all the tools we have discussed so far.
1. Naming the pipeline
In the beginning, we have the Semaphore version (v1.0) and the name for our pipeline.
Selecting a server and OS
2. Next, we define the agent.
The agent is the environment in which our code will run, which includes a virtual machine and operating system. The machine type sets the virtual machine type to be provisioned and os_image its operating system. Semaphore automatically provisions machines on demand.
With great continuous integration tools for PHP working well together, we can focus on our code with a peace of mind that code quality is ensured.
With a few clicks and lines of code, we’ve come a long way. And we’re just scratching the surface on what Semaphore can do through the hire of a Senior PHP Laravel Developer.