See rates for the top Laravel developers in Latin America

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

Educator and Content creator, freelance consultant, API evangelist

Cube

Laravel Newsletter

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

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

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

Bacancy
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
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
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant 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

The latest

View all →
Laravel Gets a Claude Code Simplifier Plugin image

Laravel Gets a Claude Code Simplifier Plugin

Read article
Laravel Boost Update Adds Support for the New MCP Protocol image

Laravel Boost Update Adds Support for the New MCP Protocol

Read article
Pest Adds withHost for Browser Testing Subdomains in Laravel image

Pest Adds withHost for Browser Testing Subdomains in Laravel

Read article
Run Artisan Make Commands in Laravel VS Code Extension image

Run Artisan Make Commands in Laravel VS Code Extension

Read article
Livewire 4 Is Dropping Next Week, and wire:transition Makes Animations Effortless image

Livewire 4 Is Dropping Next Week, and wire:transition Makes Animations Effortless

Read article
Laravel 12.45.1, 12.45.2, and 12.46.0 Released image

Laravel 12.45.1, 12.45.2, and 12.46.0 Released

Read article