Improving our code with the aid of additional tools

In addition to code coverage and test reports, we have a range of additional tools, which we can use for improving the quality of our code.

The two tools that we're going to talk about are the check style and the cyclomatic complexity through the C.R.A.P. index.

We are going to add more examples and tools to these in Chapter 9, Eliminating Stress with the Help of Automation, as each command would require too much knowledge from the developer's side, and it is something that can be automated and triggered by the flick of a switch.

PHP Checkstyle (PHPCS) is a great tool, albeit it is rather complex at first . This will help us in maintaining a style of code that is uniform for all developers. You might care too much about this, and I've seen situations where decisions on which style to use have resulted in a big fight. However, the benefits of this are quite evident, as it forces the developers to control their style of coding. When used with the cyclomatic complexity, it can standardize the code and avoid any situation involving intricate and difficult code.

There are some already existing code standards available for your use and these have been configured according to your needs. PHPCS only needs a reference for the configuration file or the name of the standard to follow.

We are going to install and use Yii 2 own code standards, which you can use as a base for specifying the rules that are more suited to your needs.

You can install the Yii 2 code standards by using Composer, which will include the actual binary that we need as a dependency:

// composer.json
    "require-dev": {
       ...
        "yiisoft/yii2-coding-standards": "*"

Once we have installed both of them, we can invoke them through the console by using the following command:

$ vendor/bin/phpcs --standard=vendor/yiisoft/yii2-coding-standards/Yii2/ruleset.xml  --extensions=php --ignore=autoload.php models controllers modules

The last three arguments are the folders that we want PHPCS to scan.

If you want to improve your code, then you should make use of the C.R.A.P. index, which is included in the coverage reports generated by Codeception. In the following chapter, we'll see how the cyclomatic complexity index can be used for basing the decisions for modifying your code.

The C.R.A.P. index has been designed for analyzing and predicting the amount of effort, pain, and time required for maintaining an existing body of code.

It is mathematically defined as shown here:

C.R.A.P.(m) = comp(m)^2 * (1 – cov(m)/100)^3 + comp(m)

Where comp(m) is the cyclomatic complexity, and cov(m) is the test code coverage provided by the automated tests. The cyclomatic complexity is calculated as 1 plus the number of unique decisions in the method.

A low C.R.A.P. index indicates a relatively low change and maintenance risk, because it's either not too complex or sufficiently covered by tests. To keep it practical, if your method is a straight sequence of calls, then it is likely that it will have a C.R.A.P. index that is close to 1. The more if, for, and while clauses it has, the more complex it will be, and hence it will have a higher C.R.A.P. index.

This is where testing lets the potential problems emerge and points you in the direction that you should be taking for keeping your code maintainable and modular.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset