What is the use of phpcs?
As a project grows, it becomes necessary to maintain and enforce some defined standards for the code. Such standards help achieve uniformity, readability, and maintainability, irrespective of the engineer that wrote the code. For a PHP project, one of the best ways to enforce such standards is PHP_CodeSniffer (or PHPCS for short). Show PHPCS is a tool that helps detect violations of pre-defined coding standards. It also includes an additional tool that can automatically correct those violations. PrerequisitesTo complete this tutorial you will only need Composer globally installed. About the projectPHPCS is framework-agnostic, but for simplicity, we will be using it with a simple Laravel project. The project, called “Twilio Greeter”, displays a list of common greetings depending on the language chosen by the user. You can view the complete source code here. The project files that are of interest to us are:
You can learn more about how the above files work and Laravel in general from the official Laravel documentation. Setting up the projectClone our greeter app and change into the project directory with:
Next, run to install the project dependencies and to start the server. Our application is now accessible at Installing PHPCSSince it’s desirable to run the PHPCS CLI as a stand-alone command, we will install it globally with the following command:
You can check your installation with: NOTE: If you get an error similar to “command not found”, ensure you place the Composer bin folder in your PATH (usually Our PreferencesOur application works fine after cloning, but the coding style could use some improvements. PHPCS supports lots of coding standards out of the box such as PSR12, PSR2, PEAR, and Zend. You can also create your own rules if they are all found wanting. For our use case, we want some rules such as:
Fortunately for us, our preferences are conventional and have been covered by the PSR12 standard, so we will just go ahead and use the PSR12 as our base configuration. Configuring PHPCS for our ProjectTo start, let’s create a new file called and add the following:
The code above has been commented to provide some explanation, but you can look up the PHPCS wiki to see the different options you can provide. Running our first testAt this point, our project is ready and we can test for violations now. In the root directory, run: If all went well, you should see an output similar to:
Fixing as we go.The report above included the files and the line number in which we went against the rules. For instance, we need to specify the method visibility on line 8 and move the opening brace on line 18 to a new line. A quick way out would be to run: in the project directory and PHPCS will automatically try to fix the violations marked with From our report, the only file we need to clean up is
Then, we specify the visibility for the
with:
Finally, we mark our greetings method as
With:
NOTE: The clean version of the project is on the Now, run phpcs again and our errors should have disappeared. ConclusionCode guidelines help to reduce the cognitive load required to work in a project with multiple developers, and PHPCodeSniffer does a good job of ensuring adherence to pre-set standards. If you’re looking to build your own custom rules, the existing rulesets are a good place to start. I hope you enjoyed the post and if there are any issues or questions, please feel free to reach out to me via Email or on Github. Additional ReadingTo further your education, check out the related topics:
What does a code sniffer do?Code sniffers will check source codes for compliance with standards set by internal development teams or regulatory bodies. Code sniffing is similar to code linting but more pedantic. Linting primarily checks for bugs and syntax errors while sniffing looks for issues in the way code is written.
How do I set up Phpcs?Plugin Installation. Open Visual Studio Code.. Press Ctrl+P on Windows or Cmd+P on Mac to open the Quick Open dialog.. Type ext install phpcs to find the extension.. Press Enter or click the cloud icon to install it.. Restart Visual Studio Code when prompted.. How do I add Phpcs to my global path?18 Answers. Install the phpcs by using composer with composer global require squizlabs/php_codesniffer.. Press Command + , (Click Code -> Preferences -> Settings). Select User Settings and locate ' PHP CodeSniffer '. Scroll to ' Executatble Path ' and put. /Users/your-username/.composer/vendor/bin/phpcs.. How do I install CodeSniffer?Installing PHP CodeSniffer with Composer. Make PHP Globally Accessible. To have access from a MAMP version of PHP from the command line, you need to make sure that it's part of your environmental variables. ... . Install Composer. ... . Verify Installation. ... . Install PHP Code Sniffer. ... . Install The WordPress Coding Standards Rules.. |