Skip to content

PHP coding standard used in Alma Career Czechia (LMC) projects, but usable anywhere else

License

Notifications You must be signed in to change notification settings

lmc-eu/php-coding-standard

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a96f5e1 · Apr 18, 2024
Apr 16, 2024
Apr 16, 2024
Apr 16, 2024
May 14, 2018
Mar 16, 2018
May 13, 2021
Apr 16, 2024
Mar 15, 2018
Apr 16, 2024
Jun 4, 2021
Apr 16, 2024
Apr 18, 2024
Apr 16, 2024
Apr 16, 2024
Apr 16, 2024
Apr 18, 2024
Mar 28, 2022
May 13, 2021

Repository files navigation

LMC Coding Standard for PHP

Latest Stable Version

PHP coding standard used in LMC projects.

Standard is based on PSR-12 and adds various checks to make sure the code is readable, does follow the same conventions and does not contain common mistakes.

We use EasyCodingStandard to define and execute checks created for both PHP-CS-Fixer and PHP_CodeSniffer.

Installation

composer require --dev lmc/coding-standard

Usage

  1. Create ecs.php file in the root directory of your project and import the LMC code-style rules:
<?php declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withSets(
        [
            __DIR__ . '/vendor/lmc/coding-standard/ecs.php',
        ]
    );
    
    // Be default only checks compatible with PHP 8.0 are enabled.
    // Depending on the lowest PHP version your project need to support, you can enable additional checks for
    // PHP 8.1, 8.2 and 8.3.


    // Import one of ecs-8.1.php, ecs-8.2.php or ecs-8.3.php. Use only one file (for the highest possible PHP version).
    //->withSets(
    //    [
    //        __DIR__ . '/vendor/lmc/coding-standard/ecs.php',
    //        __DIR__ . '/vendor/lmc/coding-standard/ecs-8.3.php',
    //    ]
    //);
  1. Run the check command (for src/ and tests/ directories):
vendor/bin/ecs check src/ tests/
  1. Optionally we recommend adding this to scripts section of your composer.json:
    "scripts": {
        "analyze": [
            "vendor/bin/ecs check src/ tests/ --ansi",
            "[... other scripts, like PHPStan etc.]"
        ],
        "fix": [
            "...",
            "vendor/bin/ecs check ./src/ ./tests/ --ansi --fix"
        ],
    }

Now you will be able to run the fix using composer analyze and execute automatic fixes with composer fix.

Add custom checks or override default settings

On top of default code-style rules you are free to add any rules from PHP-CS-Fixer or PHP_CodeSniffer. If needed, you can also override some default settings.

Be aware you must add these settings after import of the base LMC code-style:

<?php declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withSets(
        [
            __DIR__ . '/vendor/lmc/coding-standard/ecs.php',
        ]
    )
    // Enforce line-length to 120 characters
    ->withConfiguredRule(LineLengthSniff::class, ['absoluteLineLimit' => 120])
    // Tests must have @test annotation
    ->withConfiguredRule(PhpUnitTestAnnotationFixer::class, ['style' => 'annotation']);

See EasyCodingStandard docs for more configuration options.

Exclude (skip) checks or files

You can configure your ecs.php to entirely skip some files, disable specific checks of suppress specific errors.

Unlike adding/modifying checks, skips must be added before import of the base LMC code-style.

<?php declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays\ArrayDeclarationSniff;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withSkip([
        // Ignore specific check only in specific files
        ForbiddenFunctionsSniff::class => [__DIR__ . '/src-tests/bootstrap.php'],
        // Disable check entirely
        ArrayDeclarationSniff::class,
        // Skip one file
        __DIR__ . '/file/to/be/skipped.php',
        // Skip entire directory
        __DIR__ . '/ignored/directory/*',
    ])
    ->withSets(
        [
            __DIR__ . '/vendor/lmc/coding-standard/ecs.php',
        ]
    );

See EasyCodingStandard docs for more configuration options.

IDE integration

For integration with PHPStorm etc. follow instructions in EasyCodingStandard README.

Changelog

For latest changes see CHANGELOG.md file. We follow Semantic Versioning.

License

This library is open source software licensed under the MIT license.