-
Notifications
You must be signed in to change notification settings - Fork 0
PHP CS Fixer
"The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP coding standards as defined in the PSR-1, PSR-2, etc., or other community driven ones like the Symfony one. You can also define your (team's) style through configuration." [source]
-
Install the package with
./vendor/bin/sail composer require friendsofphp/php-cs-fixer --dev(in wsl terminal) orcomposer require friendsofphp/php-cs-fixer --dev(in the php container) -
Create the file
/app/.php-cs-fixer.phpwith the directories to analyse, and your syntax rules. I used PSR-12 with Allman style braces:
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$finder = Finder::create()
->in([
__DIR__ . '/app',
__DIR__ . '/config',
__DIR__ . '/database',
__DIR__ . '/resources',
__DIR__ . '/routes',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
$config = (new Config())
->setFinder($finder)
->setIndent("\t")
->setLineEnding("\n")
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'braces' => [
'position_after_control_structures' => 'next',
'position_after_anonymous_constructs' => 'next',
],
'new_with_braces' => [
'anonymous_class' => false
],
]);
return $config;- Add
.php-cs-fixer.cachetoapp/.gitignore.
Execute the analysis with ./vendor/bin/php-cs-fixer fix -vvv --show-progress=dots adding --dry-run for preview (in the php container).
The output should look like this:
