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

tou…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Install PHP code sniffer fixer in a Laravel project." Ariel Mejia | Sciencx - Monday May 17, 2021, https://www.scien.cx/2021/05/17/install-php-code-sniffer-fixer-in-a-laravel-project/
HARVARD
Ariel Mejia | Sciencx Monday May 17, 2021 » Install PHP code sniffer fixer in a Laravel project., viewed ,<https://www.scien.cx/2021/05/17/install-php-code-sniffer-fixer-in-a-laravel-project/>
VANCOUVER
Ariel Mejia | Sciencx - » Install PHP code sniffer fixer in a Laravel project. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/17/install-php-code-sniffer-fixer-in-a-laravel-project/
CHICAGO
" » Install PHP code sniffer fixer in a Laravel project." Ariel Mejia | Sciencx - Accessed . https://www.scien.cx/2021/05/17/install-php-code-sniffer-fixer-in-a-laravel-project/
IEEE
" » Install PHP code sniffer fixer in a Laravel project." Ariel Mejia | Sciencx [Online]. Available: https://www.scien.cx/2021/05/17/install-php-code-sniffer-fixer-in-a-laravel-project/. [Accessed: ]
rf:citation
» Install PHP code sniffer fixer in a Laravel project | Ariel Mejia | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.