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.

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
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with Vue and Livewire stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Get your ideas to market faster with a developer data platform built on the leading modern database. Support transactional, search, analytics, and mobile use cases while using a common query interface and the data model developers love.

MongoDB

The latest

View all →
Laravel Error Solutions on the Default Exception Page image

Laravel Error Solutions on the Default Exception Page

Read article
Build Custom Admin Panels With Backpack for Laravel image

Build Custom Admin Panels With Backpack for Laravel

Read article
New String Helpers and ServeCommand Improvements in Laravel 11.14 image

New String Helpers and ServeCommand Improvements in Laravel 11.14

Read article
Auth Screens for Your Laravel Apps image

Auth Screens for Your Laravel Apps

Read article
Adding Real Time Chat to Laravel Using Reverb & Vue image

Adding Real Time Chat to Laravel Using Reverb & Vue

Read article
Build Tables Based on Eloquent Models with Inertia Table image

Build Tables Based on Eloquent Models with Inertia Table

Read article