Getting started with PHPInsights

Published on by

Getting started with PHPInsights image

PHPInsights is a composer package created by Nuno Maduro and is a fantastic tool to get started with analyzing the code quality of your PHP applications.

Whether you are a pro at code quality tools or a complete newbie - 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 need to do is run a quick composer command:

composer require nunomaduro/phpinsights --dev

You are then set to go, as out of the box, it requires no configuration. You can analyze specific directories using the command line by running:

./vendor/bin/phpinsights

This will run through a series of default. "sniffs" to check a few basic things for you. You will get an output that looks like the below:

 
 
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 see the code issues that may be reported, then enter again to see architectural issues reported, and finally, enter again to see code style issues.

Next, we can look into how we might improve our code based on the "sniffs" run. This can be done by configuring the PHPInsights configuration, customizing what is run, and a few other options. To get started, create a phpinsights.php in the root of your project, then we can begin customizing what we want to run.

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

We have various options for the preset, depending on the framework/platform you are using. To show how this works, I will gradually build up my configuration file as I explain each part - so you can see the journey you might go through.

You can use laravel, symfony, magento2, drupal, or default for your preset - I will use Laravel.

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

You may want to exclude directories within your application that you do not want to be "sniffed" or analyzed. You can add these by adding an exclude configuration option.

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

You can configure specific insights within your configuration file, which allows you to set the options for things such as 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,
],
],
];

If we rerun PHPInsights now, we will run it with our configuration, which will be more specific to our configured specifications.

Your analysis will be split into critical areas for you to focus on, starting with your code. Analyzing the structure and quality of your code, this is extremely well documented on the PHPInsights website showing you the sniffs and insights that are available should you want to disable or configure any of the insight classes that are ran against your code.

Next up will be the Architecture of your application, which is less in-depth than something like Deptrac but covers a few specific areas to ensure consistency and standards more than architectural rules.

We then move on to the Complexity of your code, which is a smaller insight. This calculates your "cyclomatic complexity", where the lower the score, the easier your code is to understand. Code can be complicated in terms of functionality while still being easy to understand.

Finally, it checks the Style of your code, which is a little like Easy Coding Standards or PSR-2 or PSR-12. Again, the documentation on this insight is extensive, with examples of how you can configure specific insights to get your code exactly how you want it.

Unlike my tutorial on Laravel Pint, I don't have a default configuration for PHPInsights, as each time I use it - I need to configure it specifically for the project or team. I recently took over from Nuno as a maintainer on this project alongside Chris, and we have been having many conversations about how to improve this package moving forward and what the future of the package will be. There are some very exciting conversations, and we hope to have more to talk about this year.

Steve McDougall photo

Technical writer at Laravel News, Developer Advocate at Treblle. API specialist, veteran PHP/Laravel engineer. YouTube livestreamer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project.

Visit No Compromises
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Add Architecture Tests to Saloon API Integrations with Lawman image

Add Architecture Tests to Saloon API Integrations with Lawman

Read article
Protect Routes with JWT Tokens Using This Package for Laravel image

Protect Routes with JWT Tokens Using This Package for Laravel

Read article
Increment a Rate Limiter by a Custom Amount in Laravel 10.46 image

Increment a Rate Limiter by a Custom Amount in Laravel 10.46

Read article
Add Kanban Boards to Your Laravel App in Seconds image

Add Kanban Boards to Your Laravel App in Seconds

Read article
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article