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 :
- With the laravel preset,
phpinsights
will excludeconfig
,storage
,resources
,bootstrap
,nova
,database
,server.php
,_ide_helper.php
,_ide_helper_models.php
,app/Providers/TelescopeServiceProvider.php
andpublic
. - With the symfony preset,
phpinsights
will excludevar
,translations
,config
, andpublic
. - With the magento2 preset,
phpinsights
will excludebin
,dev
,generated
,lib
,phpserver
,pub
,setup
,update
,var
,app/autoload.php
,app/bootstrap.php
,app/functions.php
andindex.php
. - With the drupal preset,
phpinsights
will excludecore
,modules/contrib
,sites
,profiles/contrib
, andthemes/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
NAME
constant doesn't exist, go to the2nd option
paragraph (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
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
annotation:
final class YourClass
{
/**
* @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',
],
],
],
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' => ' ',
],
],