DEV Community

loading...

Install PHP Insights on a Laravel project

Ariel Mejia
Fullstack/Mobile developer, Laravel and Flutter enthusiast.
Updated on ・2 min read

PHP Insights is a great tool for static code analysis, it add a review about your code on four different areas,

  • the code.
  • the complexity of the project.
  • the architecture of the project.
  • and the code style.

it works on any php project, but in a Laravel project there are some custom rules that would help to start to work with the project as soon as posible, here some tips.

Install the package

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

Run the analysis

php artisan insights
Enter fullscreen mode Exit fullscreen mode

Ok, here the tips...

See all the advices at once

By default it will show some tips to fix, so if you fix this issues, you can re-run the insights command to get more advices to level up some of the four categories that the package review.

if you want to see all tips at once you need the flag -v, so the command would be: php artisan insights -v

Auto fix the issues

There are some cases that would be useful, I did not use this feature because there are some codebase changes that could be affected, to auto-fix an advice, run this command:

php artisan insights path/to/analyse --fix

Discard so rules

If you install the package maybe you could see that it marks some Laravel default codebase as it could be change, to avoid issues with the default Laravel codebase and code style of some Laravel packages like Jetstream you can set this rules on config/insights.php file:

    'exclude' => [
        'app/Actions/Jetstream',
        'HandleInertiaRequests.php',
        '_ide_macros.php'
    ],

    'add' => [
        Classes::class => [
            ForbiddenFinalClasses::class,
        ],
    ],

    '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

Here you can see an example of some classes that you can, exclude, add or remove from the analysis.

In this example I exclude some files related to Jetstream and Inertia packages, but you can add code from more vendors as your project needs, in most of the cases this is correct, because we only need to get a review about our project codebase performance.

That is all, thanks for reading!

Discussion (0)