Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 17, 2021 11:47 pm GMT

Install PHP code sniffer fixer in a Laravel project

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.


Original Link: https://dev.to/arielmejiadev/install-php-code-sniffer-fixer-in-a-laravel-project-32ha

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To