Skip to content

PHP CS Fixer

Clio Brichaut edited this page Jan 14, 2023 · 1 revision

What is it?

"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]

Setup

  1. Install the package with ./vendor/bin/sail composer require friendsofphp/php-cs-fixer --dev (in wsl terminal) or composer require friendsofphp/php-cs-fixer --dev (in the php container)

  2. Create the file /app/.php-cs-fixer.php with 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;
  1. Add .php-cs-fixer.cache to app/.gitignore.

Usage

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:

image

Clone this wiki locally