sajad torkamani

What is PHP CS Fixer?

PHP CS Fixer is a PHP tool that detects and fixes coding standards issues in your PHP code. You can configure it to follow PHP coding standards like PHP-FIG’s PER Coding Style or community-driven ones like the Symfony standards. You can also define custom standards via configuration.

Usage

Install the package via Composer

composer require --dev friendsofphp/php-cs-fixer

Fix files

./vendor/bin/php-cs-fixer fix src

By default, the @PSR12 (PSR-12) standard will be used.

List files that need fixing

./vendor/bin/php-cs-fixer list-files

Configuration

If you don’t provide a config file, the @PSR12 (PSR-12) standard will be used by default.

But you can add a .php-cs-fixer.dist.php file in the root of your project that returns an instance of PhpCsFixer\ConfigInterface. Here, you can configure the rules and the files & directories to be analysed.

Here’s an example config:

<?php

$finder = (new PhpCsFixer\Finder())
    ->in(__DIR__)
;

return (new PhpCsFixer\Config())
    ->setRules([
        '@PER-CS' => true,
        '@PHP82Migration' => true,
    ])
    ->setFinder($finder)
;

You can see the full list of available rules here.

Integrate with PHPStorm

Configure PHP interpreter

  • Go to Settings > PHP > CLI Interpreter (click the ... button to the right)
  • Provide the path to the php executable.

Sources