nunomaduro/phpinsights
PHP Insights is a terminal tool to analyze PHP code quality, style, architecture, and complexity. Works out of the box with Laravel (artisan insights), Symfony, Yii, Magento, and more, with built-in checks for reliability and loose coupling.
The default configuration attached with PHP Insights is opinionated and may be not convenient for you.
Here you will learn how to configure PHPInsights for your project.
Before continuing, please create a phpinsights.php file by reading the configuration docs.
You should have the following structure:
<?php
declare(strict_types=1);
return [
'preset' => 'default',
'exclude' => [
// 'path/to/directory-or-file'
],
'add' => [
// ExampleMetric::class => [
// ExampleInsight::class,
// ]
],
'remove' => [
// ExampleInsight::class,
],
'config' => [
// ExampleInsight::class => [
// 'key' => 'value',
// ],
],
];
By default, phpinsights will analyse all your php files in your project directory, except folders bower_components, node_modules and vendor.
::: tip For others preset In addition to these folders :
phpinsights will exclude config, storage, resources, bootstrap, nova, database, server.php, _ide_helper.php, _ide_helper_models.php, app/Providers/TelescopeServiceProvider.php and public.phpinsights will exclude var, translations, config, and public.phpinsights will exclude bin, dev, generated, lib, phpserver, pub, setup, update, var, app/autoload.php, app/bootstrap.php, app/functions.php and index.php.phpinsights will exclude core, modules/contrib, sites, profiles/contrib, and themes/contrib.
:::In your phpinsights.php file, you can add to the exclude key everything you want to exclude.
For example:
'exclude' => [
'src/Migrations', // will exclude Migrations Folder in src
'*Repository.php', // will exclude every php files that match pattern
'src/Kernel.php' // will exclude this file only
],
Open the insight class and look for private const NAME constant.
If the
NAMEconstant doesn't exist, go to the2nd optionparagraph (below).
Copy value of the NAME constant and open a class with method that you would like exclude insight for. In the phpDoc add [@phpcsSuppress](https://github.com/phpcsSuppress) annotation.
After running vendor/bin/phpinsights you saw an error:
• [Code] Unused parameter:
src/YourClass.php:19: Unused parameter $thisIsUnusedParameter.
After verification in documentation, you know that \SlevomatCodingStandard\Sniffs\Functions\UnusedParameterSniff class is responsible for the [Code] Unused parameter error and it contains private const NAME = 'SlevomatCodingStandard.Functions.UnusedParameter’;. Let's use it together with the [@phpcsSuppress](https://github.com/phpcsSuppress) annotation:
final class YourClass
{
/**
* [@phpcsSuppress](https://github.com/phpcsSuppress) SlevomatCodingStandard.Functions.UnusedParameter
*/
public function yourMethod(array $thisIsUnusedParameter): void
{
// ...
}
}
If you create an Insight, or an Insight is not enabled, you can enable it in the add section.
For example, if you want to enable "Fully Qualified ClassName In Annotation":
'add' => [
\NunoMaduro\PhpInsights\Domain\Metrics\Code\Comments::class => [
\SlevomatCodingStandard\Sniffs\Namespaces\FullyQualifiedClassNameInAnnotationSniff::class
]
]
::: tip
You could also simplify the namespace with use My\Insight\Namespace;
:::
::: tip
Although PHPInsights has it's own insights, it can handle Sniffs from PHP CodeSniffer and Fixers from PHP CS Fixer.
So you can add every sniff or fixers that implements PHP_CodeSniffer\Sniffs\Sniff or PhpCsFixer\Fixer\FixerInterface.
:::
If there is an insight that goes against your standards, you can add it in the remove section.
For example, if you don't like adding a space after not (! $myVariable):
'remove' => [
\PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff::class,
]
::: tip
To know the className of an Insight, launch phpinsights with -v option (verbose)
:::
The config section allows you to refine default insight configuration.
For example, to increase the line length limits:
'config' => [
\PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff::class => [
'lineLimit' => 120,
'absoluteLineLimit' => 160
]
]
You can also configure the exclude parameter on each insight, to disallow an
insight on a specific file.
For example, to remove "Unused Parameters" Insight only for some file:
'config' => [
\SlevomatCodingStandard\Sniffs\Functions\UnusedParameterSniff::class => [
'exclude' => [
'src/Path/To/My/File.php',
'src/Path/To/Other/File.php',
],
],
],
<Badge text="^2.0"/> For insights that come from PHP-CS-Fixer and implements WhitespacesAwareFixerInterface, you can also configure the indentation to respect:
'configure' => [
\PhpCsFixer\Fixer\Basic\BracesFixer::class => [
'indent' => ' ',
],
],
How can I help you explore Laravel packages today?