DEV Community

Ariel Mejia
Ariel Mejia

Posted on • Edited on

add phpinsights in your laravel project

phpinsights is a helpful package to add code quality analysis, it measure the code in four areas:

  • Code (code reliable, loosely coupled, simple, and clean).

  • Complexity (check the statements and methods complexity).

  • Architecture (check the patterns and directories that you add, traits, interfaces, etc).

  • Style (check how you follow PSR-4 or PSR-12).

So it makes sense right?, lets install it.

How to install it

composer require nunomaduro/phpinsights --dev
Enter fullscreen mode Exit fullscreen mode

Publish config file

php artisan vendor:publish --provider="NunoMaduro\PhpInsights\Application\Adapters\Laravel\InsightsServiceProvider"
Enter fullscreen mode Exit fullscreen mode

Execute PHPinsights

php artisan insights
Enter fullscreen mode Exit fullscreen mode

Customize it

On Laravel there are some code standards that could mismatch on how PHPInsights works, so you can edit the config file to adjust to your project/needs/packages etc.

Here an example of how I use it with a Laravel project with Jetstream.

You can exclude files by typing the full path name, as an array item.

On config/insights.php:

    'exclude' => [
        'app/Actions/Jetstream',
        'HandleInertiaRequests.php',
        '_ide_macros.php'
    ],
Enter fullscreen mode Exit fullscreen mode

You can add custom rules:

    'add' => [
        Classes::class => [
            ForbiddenFinalClasses::class,
        ],
    ],
Enter fullscreen mode Exit fullscreen mode

You can even remove rules that does not make sense, you can customize it as you need:

    'remove' => [
        AlphabeticallySortedUsesSniff::class,
        DeclareStrictTypesSniff::class,
        DisallowMixedTypeHintSniff::class,
        ForbiddenDefineFunctions::class,
        ForbiddenNormalClasses::class,
        ForbiddenTraits::class,
        ParameterTypeHintSniff::class,
        PropertyTypeHintSniff::class,
        ReturnTypeHintSniff::class,
        UselessFunctionDocCommentSniff::class,
        UnusedParameterSniff::class,
        LineLengthSniff::class,
        DocCommentSpacingSniff::class,
        ClassInstantiationSniff::class,
        NewWithBracesFixer::class,
        NullableTypeForNullDefaultValueSniff::class,
        DisallowArrayTypeHintSyntaxSniff::class,
        NoEmptyCommentFixer::class,
        DisallowShortTernaryOperatorSniff::class,
        ForbiddenPublicPropertySniff::class,
        DisallowEmptySniff::class

    ],
Enter fullscreen mode Exit fullscreen mode

Another cool feature that you can customize is the "level" of qualification to print it as "green", "yellow" for example, you can customize it on requirements key, but if you comment, all the content it will work as it is designed by default:

    'requirements' => [
//        'min-quality' => 0,
//        'min-complexity' => 0,
//        'min-architecture' => 0,
//        'min-style' => 0,
//        'disable-security-check' => false,
    ],
Enter fullscreen mode Exit fullscreen mode

This are some useful customizations, if you want to run automatically PHPInsights on every commit or push you can install husky and configure it to run PHPInsights on the husky hooks, is just a cool idea to make your work easier.

That is all for this post, thanks for reading!

Top comments (0)