26.9 C
Pakistan
Monday, July 15, 2024

Introduction to PHPInsights

Nuno Maduro’s composer package PHPInsights is an excellent resource for beginning the analysis of the code quality of your PHP applications.

Regardless of your level of experience with code quality tools, PHPInsights offers a progressive learning curve that you can easily adjust to as your understanding grows. It is compatible out of the box with WordPress, Magento 2, Symfony, Yii, and Laravel.

You can use it to learn more about your code’s complexity, coding style, application architecture, and overall quality.

All you have to do to begin using this package is to issue the following quick composer command:

composer require nunomaduro/phpinsights --dev

After that, you are ready to go because it doesn’t need to be configured out of the box. You can use the command line to examine particular directories by executing:

./vendor/bin/phpinsights

This will check a few fundamental things for you by running through a series of default “sniffs”. What you will see in the output is as follows:

 
 
                89.7%                  87.5%                  94.1%                  90.4%
 
 
                Code                 Complexity            Architecture              Style
 
 
Score scale: ◼ 1-49 ◼ 50-79 ◼ 80-100
 
[CODE] 89.7 pts within 367 lines
 
Comments ...................................................... 64.6 %
Classes ....................................................... 12.3 %
Functions ...................................................... 1.1 %
Globally ...................................................... 22.1 %
 
[COMPLEXITY] 87.5 pts with average of 1.38 cyclomatic complexity
 
[ARCHITECTURE] 94.1 pts within 28 files
 
Classes ....................................................... 75.0 %
Interfaces ..................................................... 0.0 %
Globally ...................................................... 25.0 %
Traits ......................................................... 0.0 %
 
[MISC] 90.4 pts on coding style and 0 security issues encountered

You can then hit Enter to view any reported code issues, Enter again to view reported architectural issues, and Enter once more to view reported code style issues. After that, we can investigate how the “sniffs” run could help us make our code better. This can be accomplished by adjusting the PHPInsights configuration, changing the execution environment, and a few other settings. Make a phpinsights.php file in the project root to get started, and we can start customizing what we want to run.

declare(strict_types=1);
 
return [
    'preset' => 'default',
];

Depending on the platform or framework you are using, we have different options for the preset. I’ll progressively build up my configuration file as I go over each step to demonstrate how this works and give you an idea of the possible path you could take.

For your preset, you have the option of using Laravel, Symfony, Magento 2, Drupal, or Default; I’ll go with Laravel.

declare(strict_types=1);
 
return [
    'preset' => 'laravel',
];

If there are directories in your application that you would prefer not to have “sniffed” or examined, you might want to exclude them. By including an exclude configuration option, you can include these.

declare(strict_types=1);
 
return [
    'preset' => 'laravel',
    'exclude' => [
        'database/*',
    ],
];

Your configuration file contains specific insights that you can configure. For example, you can set the options for Line Length.

declare(strict_types=1);
 
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff;
 
return [
    'preset' => 'laravel',
    'exclude' => [
        'database/*',
    ],
    'config' => [
        LineLengthSniff::class => [
            'lineLimit' => 120,
            'absoluteLineLimit' => 160,
        ],
    ],
];

Rerunning PHPInsights now will use our configuration, which is more tailored to the specifications we have set.

You will be able to concentrate on the crucial sections of your analysis, beginning with your code. The PHPInsights website has excellent documentation on analyzing the structure and quality of your code, as well as the sniffs and insights that are available if you wish to disable or modify any of the insight classes that are applied to your code.

The Architecture of your application will come next; it covers a few key areas to ensure consistency and standards more than architectural rules, but it is not as detailed as something like Deptrac.

Next, we discuss the smaller insight of the complexity of your code. Your “cyclomatic complexity” is determined by this; the lower the score, the simpler your code is to comprehend. Although it can be easily understood, code can have complex functionality.

Lastly, it verifies the style of your code, which is somewhat akin to PSR-2, PSR-12, or Easy Coding Standards. Once more, there is a ton of documentation available for this insight, including examples of how to configure different insights to get the exact code you desire.

In contrast to my Laravel Pint tutorial, I have to configure PHPInsights specifically for each project or team that I use it for, so I don’t have a default configuration. Chris and I have been working together as maintainers on this project since I took over from Nuno, and we have had numerous discussions about how to make this package better going forward and what the package’s future holds.

There have been some really interesting discussions, and we hope to have more this year.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles