The declaration of Japanese
To be honest, PHP is under a lot of pressure in terms of code quality. By reading this series, you’ll learn how to improve the quality of your PHP code.
PS: Rich first-line technology, diversified forms of expression, all in “HULK first-line technology talk”, point attention oh!
To be honest, PHP is under a lot of pressure in terms of code quality. By reading this series, you’ll learn how to improve the quality of your PHP code.
We can blame this for many reasons, but it’s certainly not just the lack of proper testing tools in the PHP ecosystem. In this article, I want to show you a simple setup for basic quality testing for a project. I won’t go into detail about any specific tools, but rather focus on setting up the test environment.
This article has a demonstration code can be found on the lot: https://github.com/mkosiedowski/php-testing-demo if you have any question about the example in this article, you can refer to.
1
Prerequisite for
I assume that you are familiar with PHP 7.1 syntax, and that you can use Composer and PSR-4 for auto-loading and coding standards for PSR-1&PSR-2. In my example, the vendor binary is installed in the./bin directory.
2
Build tools
We’ll be using a few different test tools, so it’s good to have something that runs them with a script. PHING provides us with an excellent solution to this problem. PHing, like Apache Ant, can easily automate tasks using XML configuration. We can install it by running the following command:
$ php composer.phar require --dev phing/phing
Copy the code
Then, create some basic build.xml files in the root directory of your project.
<? The XML version = "1.0" encoding = "utf-8"? ><project name="MyProject" default="run"></project>
Copy the code
In the next steps, we will add some targets run by PHing.
3
Static code analysis
The first thing you can do to improve code quality is set up a static code parser. They will read your error code without actually running it. It’s like having a code review done by a robot in a matter of seconds. Cool, isn’t it?
4
Code style.
When written in the right style, your code is easier to maintain. Everyone knows that (if you don’t, you should at least start reading Robert C. Martin’s “Clean Code”), but many teams still have problems adhering to the standards they agree on. We can automate this task with phPCS-PHP code sniffing, isn’t it amazing?
We can install it by running the following command:
$ php composer.phar require --dev squizlabs/php_codesniffer
Copy the code
Then add a target to run it in build.xml. Your build.xml should now look like this:
<? The XML version = "1.0" encoding = "utf-8"? ><project name="MyProject" default="run"> <target name="phpcs" description="Check code style with PHP_CodeSniffer"> <exec executable="bin/phpcs" passthru="true" checkreturn="true"> <arg line="--standard=PSR1,PSR2 -extensions=php src"/> </exec> </target> <target name="run" depends="phpcs"/></project>
Copy the code
Now you can run./bin/phing, and PHPC will automatically check if you have any errors on the PSR-1 and PSR-2 coding standards.
Many frameworks, such as Symfony, define their own code style rules, which can also be checked automatically. For example, if you are using the Symfony framework, please check https://github.com/leaphub/phpcs-symfony2 standard, in order to understand how to use the Symfony PHPCS inspection standard.
Example output for a wrongly formed file:
MyProject > phpcs:FILE: /home/maciej/workspace/php-testing/src/Domain/Price.php
-------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------28 | ERROR | Method name "Price::get_value" is not in camel caps format
-------------------------------------------------------------------------Time: 67ms; Memory: 6Mb
Copy the code
No more time is wasted checking coding standards during code reviews, from now on it will be automated!
5
Copy/paste detector
Duplicate code is bad, and everyone knows that. Sometimes we create such code by mistake and we never notice it. Sometimes we do it because we are lazy. It’s a good idea to have a tool that prompts this at build time. Phpcpd-php copy/paste detector.
Install it by running the following command:
$ php composer.phar require --dev sebastian/phpcpd
Copy the code
Then add the target to build.xml:
<target name="phpcpd" description="Generate pmd-cpd.xml using PHPCPD"> <exec executable="bin/phpcpd" passthru="true"> <arg line="src"/> </exec></target>... <target name="run" depends="phpcs,phpcpd"/>
Copy the code
Example output of a duplicate code check running in the Vendor directory:
PHPCPD 4.0.0 by Sebastian Bergmann. Found 74 Clones with 2929 duplicated lines in 97 files:- /home/maciej/workspace/php-testing/vendor/phpspec/phpspec/src/PhpSpec/Matcher/TriggerMatcher.php:81-102 /home/maciej/workspace/php-testing/vendor/phpspec/phpspec/src/PhpSpec/Matcher/TriggerMatcher.php:114-135- /home/maciej/workspace/php-testing/vendor/squizlabs/php_codesniffer/src/Reports/Full.php:81-114 /home/maciej/workspace/php-testing/vendor/squizlabs/php_codesniffer/src/Reports/Code.php:162-195(...)
Copy the code
6
Want really deep code analysis?
If you are starting your project from scratch, you should check out Phan – it is a very powerful code analyzer that will make your code beautiful. To view on https://github.com/phan/phan. Installation is very simple – just install the php-ast extension (in Ubuntu you can try running sudo apt-get install php-ast) and run:
$ php composer.phar require --dev phan/phan
Copy the code
Then create a config file. Phan /config.php with the following contents:
<?phpreturn [ 'target_php_version' => '7.1', 'directory_list' => [ 'src', 'vendor/symfony/console',
], "exclude_analysis_directory_list" => [ 'vendor/'
],
];
Copy the code
Create the phAN target in the build.xml file as well:
<target name="phan" description="Check code with phan"> <exec executable="bin/phan" passthru="true" checkreturn="true"/></target>... <target name="run" depends="phpcs,phpcpd,phan"/>
Copy the code
Now you can run your code analysis if you make a mistake (for example… Declaring an incorrect PHPDoc type for a class attribute), you should see a message like this:
MyProject > phan:src/Domain/PriceComparator.php:17 PhanTypeMismatchProperty Assigning \Domain\PriceConverter to property but \Domain\PriceComparator::priceConverter is intsrc/Domain/PriceComparator.php:35 PhanNonClassMethodCall Call to method convert on non-class type int
Copy the code
Phan amazing – it reads your whole code as well as to carry out inspection for many times, it including the phpdoc declaration and the actual use variables, methods, classes, such as comparison, you can view the list of all the characteristics of the https://github.com/phan/phan#features.
conclusion
You now have three fully automated tools in your project to protect the quality of your code. All you need to do is run./bin/phing manually, or attach it to your Git-hook or continuous integration. Your code will be checked for coding standards, duplicates, and formal errors. These checks should result in a more reliable runtime and less time spent reviewing code.
HULK First Line technology chat
The technology sharing public account created by 360 cloud platform team covers cloud computing, database, big data, monitoring, pan-front-end, automated testing and many other technical fields. Through solid technical accumulation and rich front-line practical experience, it will bring you the most promising technology sharing