This content originally appeared on DEV Community and was authored by Ariel Mejia
Code sniffer is a package to apply syntax rules to our php code to follow PSR-standards, here a guide to install and use it:
Install code sniffer
composer require friendsofphp/php-cs-fixer
Add a configuration file
touch .php-cs-fixer.php
You can add your configuration rules, here a good example to use it in a laravel projects:
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude(['bootstrap', 'storage', 'vendor'])
->name('*.php')
->name('_ide_helper')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
])
->setUsingCache(false)
->setFinder($finder);
Execute the code sniffer fixer
On terminal execute this command:
vendor/bin/php-cs-fixer fix
You can add more flags to customize the behavior of the fixer, here another example:
vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run --verbose
You can install husky with npm
to fire this commands on husky hooks and execute any of this commands automatically, this is all thanks for reading.
This content originally appeared on DEV Community and was authored by Ariel Mejia
Ariel Mejia | Sciencx (2021-05-17T23:47:22+00:00) Install PHP code sniffer fixer in a Laravel project. Retrieved from https://www.scien.cx/2021/05/17/install-php-code-sniffer-fixer-in-a-laravel-project/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.