Code quality, tools to help

Good afternoon, dear Habrovites!



Recently, in the PHP community, I often hear discussions about code review, I decided to contribute, I want to talk about the possibilities that are intended to simplify your life and improve the quality of the code in your project. It is relevant not only for PHP, similar tools described here exist also for other programming languages, for example, I learned about them when I participated in a project on NodeJS.



I want to share tools that are designed to improve the quality of the code, find syntax errors, bring everything to one code style, etc.



Pre-commit



If you type β€œpre-commit php” in a search engine, it will give you several ready-made scripts that analyze the written code. For git, the script must be placed in the β€œ.git / hooks /” folder, and then every time you commit it will run and if errors are found, it simply will not allow you to push the code into the repository. Some IDEs have separate plugins that do the same thing, but what if you have a large team and everyone uses different editors? And it's easier to configure this script once by uploading it to your repository than to configure its IDE for each developer separately. What's more important, in this script you can add any other tools that you use in your team, such as a static analyzer (for example phpstan), and / or unit testing (for example phpunit )



Tools to help improve code quality



php -l (Syntax check only (lint)) is a syntax checker built into the PHP core.



php-cs-fixer (PHP Coding Standards Fixer) - Fixes your code to PSR-1, PSR-2, etc., or other communities like Symfony. You can also define your (team) style through configuration. Those. your entire team will have the same code style.



php-cs + php-cbf (PHP CodeSniffer + PHP Code Beautifier) ​​- It is a set of two PHP scripts; The main phpcs script, which extracts PHP, JavaScript and CSS files to detect violations of a specific coding standard, and the second phpcbf script automatically fixes common coding violations. Similar tool to php-cs-fixer.



php-md (PHP Mess Detector) - Side projectPHP Depend , which aims to be the PHP equivalent of the well-known Java PMD tool. Takes a given PHP source code base and looks for several potential issues in this source. These problems can be such as syntax errors, suboptimal code, overly complex expressions, unused parameters / methods / properties.



php-cpd (PHP Copy / Paste Detector) - Copy / paste detector for PHP code. Those. finds the same blocks of code in different parts of the application that can be moved into separate functions / methods.



You can dive deeper into automated code review and you will probably find many other interesting tools.



Output



Using the tools available in the development world, you can automate many routine processes for checking code, which will improve its quality by an order of magnitude (since we exclude the human factor). For greater confidence in the use of these tools by the whole team, you can inject them into a pre-commit script that is run before the commit is created in your source control system.



All Articles