Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpinsights Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Get started

Requires: PHP 7.4+

First, install PHP Insights via the Composer package manager:

composer require nunomaduro/phpinsights --dev

Then, use the phpinsights binary:

# Mac & Linux
./vendor/bin/phpinsights

# Windows
.\vendor\bin\phpinsights.bat

Within Laravel

First, you should publish the config-file with:

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

Then, use the insights Artisan command:

php artisan insights

Note for Laravel 7 users: phpinsights requires PHP 7.3+, and PHPUnit 9.0+. For upgrading PHPUnit, you can use following command:

composer require --dev phpunit/phpunit:^9.0 --update-with-dependencies

Within Lumen

Because we cannot use Artisan's publish command within a Lumen project you must manually copy the config file into your project:

cp vendor/nunomaduro/phpinsights/stubs/laravel.php config/insights.php

Then register the phpinsights provider and load the configuration into the application within your bootstrap/app.php file:

$app->register(\NunoMaduro\PhpInsights\Application\Adapters\Laravel\InsightsServiceProvider::class);
$app->configure('insights');

And setup is done, so you can now run phpinsights with the following command:

php artisan insights

With Docker

You can also use phpinsights via Docker:

docker run -it --rm -v "$(pwd):/app" nunomaduro/phpinsights

Analyse a sub-directory or a specific file

You can ask phpinsights to analyse only a directory or even a specific file by providing path with analyse command:

# For a directory
./vendor/bin/phpinsights analyse path/to/analyse

# For a file
./vendor/bin/phpinsights analyse path/to/analyse.php

In laravel, launch command as usual with your path:

php artisan insights path/to/analyse

Fixing errors automatically <Badge text="^2.0"/>

Some Insights support automatic fixing. To fix your code automatically, there are two possibilities:

  • Add --fix option to your command. The output will be the classical output, with a summary of all issues fixed.
  • Or launch phpinsights fix [directory]
# Classical command with --fix option
vendor/bin/phpinsights analyse path/to/analyse --fix

# In laravel
php artisan insights path/to/analyse --fix

# Just fix
vendor/bin/phpinsights fix path/to/analyse

Analyse multiple paths <Badge text="^2.0"/>

You can ask phpinsights to analyse multiple directories, multiple files or even combining them by providing multiple paths with analyse command:

# For multiple directories
./vendor/bin/phpinsights analyse path/to/dir1 path/to/dir2

# For multiple files
./vendor/bin/phpinsights analyse path/to/file1.php path/to/file2.php

# Combined
./vendor/bin/phpinsights analyse path/to/dir path/to/file.php

In laravel, launch command as usual with your paths:

php artisan insights path/to/dir path/to/file.php

Specify composer file <Badge text="^2.0"/>

Optionally, you can specify your composer file by adding the composer flag as below:

./vendor/bin/phpinsights analyse --composer=/var/www/composer.json

Formatting the output

For changing the output format you can add the format flag. The following formats are supported:

  • console
  • json
  • checkstyle
./vendor/bin/phpinsights analyse --format=json

Saving output to file

You can pipe the result to a file or to anywhere you like. A common use case is parsing the output formatted as json to a json file.

./vendor/bin/phpinsights analyse --format=json > test.json

When piping the result remember to add the no interaction flag -n, as the part where you need to interact is also getting piped. (the json format does not have any interaction) While piping data, if you want the progress bar to refresh itself instead of printing a new one, add the --ansi flag.

Allowed memory size of X bytes exhausted

If you encounter the error Allowed memory size of XXXXX bytes exhausted, the current workaround is to increase the memory limit:

php -d memory_limit=2000M ./vendor/bin/phpinsights

Display issues omitted

The PHP Insights console command has different verbosity levels, which determine the quantity of issues displayed. By default, commands display only the 3 first issues per Insight, but you can display them all with the -v option:

./vendor/bin/phpinsights -v

Avoid Composer conflicts

If you have trouble while requiring phpinsights with composer, try installing it with bamarni/composer-bin-plugin to isolate it from others dependencies:

composer require --dev bamarni/composer-bin-plugin
composer bin phpinsights require nunomaduro/phpinsights
./vendor/bin/phpinsights

Flush cache results <Badge text="^2.0"/>

Between 2 analyses, issues are cached. PHPInsights is smart enough to invalidate cache when it detects changes in your code, but you may completely flush cache before analysis by adding --flush-cache flag.

Configure diff <Badge text="^2.0"/>

Some insights display a diff output. If you want more context on the diff, configure it in the phpinsights.php file:

<?php

return [
    // ...
    'diff_context' => 3,
    // ...
];
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests