Getting started with PHPInsights | Laravel news


0


A composer package created by Nuno Maduro, PHPInsights is a fantastic tool to start analyzing the code quality of your PHP applications.

Whether you’re a pro at code quality tools or a complete novice, PHPInsights has a steady learning curve that you can quickly adapt to as your knowledge improves. Out of the box it works with Laravel, Symfony, Yii, WordPress, Magento 2 and more.

It allows you to gain insights into the quality of your code, your coding style, your application architecture and the complexity of your code.

To get started with this package, all you have to do is run a short composer command:

composer require nunomaduro/phpinsights --dev

Then you can get started because no configuration is required in the delivery state. You can analyze specific directories from the command line by running:

./vendor/bin/phpinsights

This will go through a series of default settings. “sniffs” to check a few basic things for you. You will get an output that looks like this:

 

 

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

From here you can press Enter to view the code issues that may have been reported, then press Enter again to view the reported architecture issues, and finally press Enter again to view code style issues.

Next, we can examine how we can improve our code based on the “sniffs” run. This can be done by configuring PHPInsights configuration, customizing execution, and a few other options. First create one phpinsights.php in the root of your project, then we can start customizing what we want to run.

declare(strict_types=1);

 

return (

'preset' => 'default',

);

Depending on the framework/platform used, we have different options for the preset. To show how this works, I’ll gradually build up my configuration file while explaining each part – so you can see the journey you’ll be going through.

You can use laravel, symfony, magento2, drupalor default For your preset, I’m using Laravel.

declare(strict_types=1);

 

return (

'preset' => 'laravel',

);

You may want to exclude directories in your application that you don’t want to be “listened” to or analyzed. You can add these by adding one exclude configuration option.

declare(strict_types=1);

 

return (

'preset' => 'laravel',

'exclude' => (

'database/*',

),

);

You can configure certain insights in your configuration file, which allows you to set options for things like 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,

),

),

);

Now when we run PHPInsights again, we’re running it with our configuration, which is more specific to our configured specs.

Your analysis is broken down into critical areas to focus on, starting with your code. Analyzing the structure and quality of your code is very well documented on the PHPInsights website and will show you the sniffs and insights that are available should you wish to disable or configure any of the Insight classes running on your code.

Next comes the architecture of your application, which is less in-depth than say Deptrac, but covers some specific areas to ensure more consistency and standards than architecture rules.

Then we turn to the complexity of your code, which provides a smaller insight. This will calculate your “cyclomatic complexity”. The lower the score, the easier your code is to understand. Code can be complicated in terms of functionality and yet easy to understand.

Finally, the style of your code is checked, which is a bit similar to Easy Coding Standards or PSR-2 or PSR-12. Again, the documentation for this insight is extensive, with examples of how you can configure specific insights to get your code exactly how you want it.

Unlike my Laravel Pint tutorial, I don’t have a default configuration for PHPInsights as I have to configure them specifically for the project or team each time I use them. I recently replaced Nuno as maintainer of this project Chris, and we’ve had a lot of conversations about how we can improve this pack in the future and what the future of the pack will be like. There are some very exciting conversations going on and we hope to have even more to talk about this year.

Source link


Like it? Share with your friends!

0
ncult

0 Comments

Your email address will not be published. Required fields are marked *