Set of PHP-CS-Fixer rules used in the development of Ely.by PHP projects
Go to file
2023-05-17 23:01:23 +02:00
.github/workflows Resolves #16. Split repository and move all custom fixers to separate repository 2023-05-17 19:54:10 +02:00
src Update rules 2023-05-17 23:01:23 +02:00
.gitignore Resolves #16. Split repository and move all custom fixers to separate repository 2023-05-17 19:54:10 +02:00
.php-cs-fixer.dist.php Migrate to PHP-CS-Fixer 3. Upgrade custom rules. Upgrade ruleset. 2022-09-20 02:50:21 +02:00
CHANGELOG.md Update rules 2023-05-17 23:01:23 +02:00
composer.json Update rules 2023-05-17 23:01:23 +02:00
composer.lock Update rules 2023-05-17 23:01:23 +02:00
LICENSE Resolves #16. Split repository and move all custom fixers to separate repository 2023-05-17 19:54:10 +02:00
README.md Resolves #16. Split repository and move all custom fixers to separate repository 2023-05-17 19:54:10 +02:00

Ely.by PHP-CS-Fixer rules

Set of PHP-CS-Fixer rules used in development of Ely.by PHP projects. It's suited for PHP 7.4 and above.

Latest Version on Packagist Total Downloads Software License Build Status

Installation

First of all install Ely.by PHP-CS-Fixer rules via composer with PHP-CS-Fixer:

composer require --dev friendsofphp/php-cs-fixer ely/php-code-style

Then create a file .php-cs-fixer.php with the following contents:

<?php
$finder = \PhpCsFixer\Finder::create()
    ->in(__DIR__);

return \Ely\CS\Config::create()
    ->setFinder($finder);

And that's it. You can now find code style violations with following command:

vendor/bin/php-cs-fixer --diff --dry-run -v fix 

And then completely fix them all with:

vendor/bin/php-cs-fixer fix

Configuration

You can pass a custom set of rules to the \Ely\CS\Config::create() call. For example, it can be used to validate a project with PHP 7.4 compatibility:

<?php
return \Ely\CS\Config::create([
    'trailing_comma_in_multiline' => [
        'elements' => ['arrays', 'arguments'],
    ],
])->setFinder($finder);

Code style

Our code style is based primarily on PSR-2, while borrowing some ideas from PSR-12 with some changes.

Example

This example encompasses some of the rules below as a quick overview:

<?php
declare(strict_types=1);

namespace Vendor\Package;

use Vendor\Package\SomeNamespace\ClassA;

class Foo extends Bar implements FooInterface {
    use SomeTrait;

    private const SAMPLE_1 = 123;
    private const SAMPLE_2 = 321;

    public Typed $field1;

    public $field2;

    public function sampleFunction(
        int $a,
        private readonly int $b = null,
    ): array {
        if ($a === $this->b) {
            $result = bar();
        } else {
            $result = BazClass::bar($this->field1, $this->field2);
        }

        return $result;
    }

    public function setToNull(): self {
        $this->field1 = null;
        return $this;
    }

}

Key differences:

  • Opening braces for classes MUST be on the same line.

  • Opening braces for methods MUST be on the next line.

Additional rules:

  • There MUST be one empty line before return statement, except when there is only one statement before it.

    <?php
    
    function a() {
        $a = '123';
        return $a . ' is a number';
    }
    
    function b() {
        $a = '123';
        $b = 'is';
    
        return $a . ' ' . $b . ' a number';
    }
    
  • There MUST be one blank line around class body, but there MUST be no blank lines around anonymous class body.

    <?php
    class Test {
    
        public function method() {
            $obj = new class extends Foo {
                public function overriddenMethod() {
                    // code body
                }
            };
        }
    
    }
    
  • Visibility MUST be declared for all methods, properties and constants.

  • There MUST be one blank line after an each of if, switch, for, foreach, while and do-while bodies.

    <?php
    if (true) {
        // some actions here
    }
    
    echo 'the next statement is here';
    
  • There MUST be no alignment around multiline function parameters.

    <?php
    function foo(
        string $input,
        int $key = 0,
    ): void {}