PHP Insights was created by, and is maintained by Nuno Maduro, and is the perfect starting point to analyze the code quality of your PHP projects. Carefully crafted to simplify the analysis of your code directly from your terminal.
- Analysis of code quality and coding style
- Beautiful overview of code architecture and it's complexity
- Designed to work out-of-the-box with Laravel, Symfony, and more
- Contains built-in checks for making code reliable, loosely coupled, simple, and clean
- Friendly console interface build on top of PHPCS, PHPLOC, and EasyCodingStandard
Requires:
First, install PHP Insights via the Composer package manager:
composer require nunomaduro/phpinsights --devThen, use the phpinsights binary:
./vendor/bin/phpinsightsOn Windows:
.\vendor\bin\phpinsights.batFirst, 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 insightsFirst, you should create the config-file with:
cp vendor/nunomaduro/phpinsights/stubs/symfony.php phpinsights.phpThen, use the phpinsights binary:
./vendor/bin/phpinsightsOn Windows:
.\vendor\bin\phpinsights.batYou may customize insights creating and editing the configuration file:
cp vendor/nunomaduro/phpinsights/stubs/config.php phpinsights.phpYou can run PHP Insights in your CI by defining level you want to reach with the options --min-quality, --min-complexity, --min-architecture, --min-style.
If the minimum level defined is not reached, the console will return an exit error code.
./vendor/bin/phpinsights --no-interaction --min-quality=80 --min-complexity=90 --min-architecture=75 --min-style=95 # Within Laravel php artisan insights --no-interaction --min-quality=80 --min-complexity=90 --min-architecture=75 --min-style=95Note: The --no-interaction option is mandatory when it's launch in CI to avoid prompts.
All others are optional, so if you want to focus only on style, add the --min-style and forget others.
PHP Insights console command have 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 -vOn Windows:
.\vendor\bin\phpinsights.bat -vIf 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 If you have trouble while requiring phpinsights with composer, try install 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/phpinsightsThe project is under development. As such, any help is welcome!
- Create a new insight from scratch
- Add a new insight from PHP CS Sniff
- Create or improve create a preset for your favorite framework
- Create the test suite
Imagine that you want to create a new Insight that doesn't allow the usage of final classes:
- Create a new file under
src/Domain/Insightswith the content:
final class ForbiddenFinalClasses extends Insight { public function hasIssue(): bool { return (bool) count($this->collector->getConcreteFinalClasses()); } public function getTitle(): string { return 'The use of `final` classes is prohibited'; } }- Attach the
Insightto a specific insidesrc/Domain/Metrics:
final class Classes implements HasInsights { // ... public function getInsights(): array { return [ ForbiddenFinalClasses::class, ]; } }Are you aware of a PHPCS sniff that you would like to add to PHP Insights? You can add it in the following way:
- Identify the related metric, and add it to the list of insights:
final class Classes implements HasInsights { // ... public function getInsights(): array { return [ UnusedPropertySniff::class, ]; } }Would you like to exclude a directory or remove an Insight for your favorite framework? You can add it in the following way:
In this example we are going to use the Laravel Framework.
- Open the file
src/Application/Adapters/Laravel/Preset.phpand update the config file:
final class Preset implements PresetContract { public static function getName(): string { return 'laravel'; } public static function get(): array { return [ 'exclude' => [ 'config', 'storage', 'resources', 'bootstrap', 'nova', 'database', 'server.php', '_ide_helper.php', '_ide_helper_models.php', 'public', ], 'add' => [ Classes::class => [ ForbiddenFinalClasses::class, ], ], 'remove' => [ AlphabeticallySortedUsesSniff::class, DeclareStrictTypesSniff::class, DisallowMixedTypeHintSniff::class, ForbiddenDefineFunctions::class, ForbiddenNormalClasses::class, ForbiddenTraits::class, TypeHintDeclarationSniff::class, ], 'config' => [ ForbiddenPrivateMethods::class => [ 'title' => 'The usage of private methods is not idiomatic in Laravel.', ], ForbiddenDefineGlobalConstants::class => [ 'ignore' => ['LARAVEL_START'], ], ForbiddenFunctionsSniff::class => [ 'forbiddenFunctions' => [ 'dd' => null, 'dump' => null, ], ], ], ]; } }At the moment, this package doesn't have any test. Would you like to contribute? This is the perfect task.
Thank you to all the people who have already contributed to PHP Insights!
Nuno Maduro | Caneco | Quynh Xuan Nguyen | Mike Erickson | Viktor Szépe | Owen Voke |
PHP Insights is open-sourced software licensed under the MIT license.
