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 Prerequisites

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:

 Copy code

$ php composer.phar require --dev phing/phing
Copy the code

Then, create some basic build.xml files in the root directory of your project.

<? 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.

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:

<? 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, check github.com/leaphub/php… Standards to learn how to use PHPCS to check Symfony standards.

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 linesin 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

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. Check it out at 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 is amazing – it reads your entire code and performs multiple checks on it, including comparing PHPDoc declarations to the actual use of variables, methods, classes, etc. You can check it out at github.com/phan/phan#f… A list of all the characteristics of.

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.

Many PHPer will always encounter some problems and bottlenecks when they are advanced. They have no sense of direction because they write too much business code, so they don’t know where to start to improve. I have sorted out some information about this, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be shared for free
, you need toplease
Click here to
.