Skip to content

easy-coding-standard/easy-coding-standard

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
January 25, 2023 12:35
bin
December 30, 2022 01:40
December 31, 2022 14:08
February 5, 2023 15:00
January 11, 2023 12:28
src
January 30, 2023 16:24
February 5, 2023 15:00
December 30, 2022 01:40
February 5, 2023 15:00
December 30, 2022 01:40
January 30, 2023 15:15
December 30, 2022 02:17
February 5, 2023 15:00
December 30, 2022 19:38
cs
January 30, 2023 11:37
February 5, 2023 15:00
February 5, 2023 15:00
December 30, 2022 01:40

The Easiest Way to Use Any Coding Standard

Downloads total

ECS-Run

Features

  • Blazing fast parallel run
  • Use PHP_CodeSniffer || PHP-CS-Fixer - anything you like
  • 2nd run under few seconds with un-changed file cache
  • Skipping files for specific checkers
  • Prepared sets - PSR-12, arrays, use statements, spaces and more... - see SetList class for all
  • Prefixed version by default to allow install without conflicts on any PHP 7.2+ project

Are you already using another tool?


Install

composer require symplify/easy-coding-standard --dev

Usage

1. First Run

To start using ECS, just run it:

vendor/bin/ecs

It will instantly offer to create the ecs.php with your directories from your project.

2. Setup Sets and Checkers

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ecsConfig): void {
    // A. full sets
    $ecsConfig->sets([SetList::PSR_12]);

    // B. standalone rule
    $ecsConfig->ruleWithConfiguration(ArraySyntaxFixer::class, [
        'syntax' => 'short',
    ]);
};

3. Run Again

vendor/bin/ecs check src

The runs above are dry runs, so you can check the code diffs, before they get applied. If you're sure, go for a fix command:

vendor/bin/ecs check src --fix

Configuration

Configuration can be extended with many options. Here is list of them with example values and little description what are they for:

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
    $ecsConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);

    $ecsConfig->skip([
        // skip whole rule
        ArraySyntaxFixer::class,

        // skip directory by absolute
        __DIR__ . '/packages/Migrations',

        // skip directories by mask
        __DIR__ . '/packages/*/src/Legacy',

        // skip single rule in particular paths
        LineLenghtFixer::class => [
            __DIR__ . '/packages/EasyCodingStandard/packages/SniffRunner/src/File/File.php',
            '*Sniff.php',
        ],
    ]);

    // file extensions to scan [default: [php]]
    $ecsConfig->fileExtensions(['php', 'phpt']);

    // configure cache paths & namespace - useful for Gitlab CI caching, where getcwd() produces always different path
    // [default: sys_get_temp_dir() . '/_changed_files_detector_tests']
    $ecsConfig->cacheDirectory('.ecs_cache');

    // [default: \Nette\Utils\Strings::webalize(getcwd())']
    $ecsConfig->cacheNamespace('my_project_namespace');

    // indent and tabs/spaces [default: spaces]
    $ecsConfig->indentation('tab');

    // end of line [default: PHP_EOL]; other options: "\n"
    $ecsConfig->lineEnding("\r\n");
};

Parallel Run

ECS runs in X parallel threads, where X is number of your threads.

Do you have 16 threads? That will speed up the process from 2,5 minutes to 10 seconds.


This process is enabled by default. To disable it, use disableParallel() method:

use Symplify\EasyCodingStandard\Config\ECSConfig;

return function (ECSConfig $ecsConfig): void {
    $ecsConfig->disableParallel();
};

FAQ

How do I clear cache?

vendor/bin/ecs check src --clear-cache

How to load Custom Config?

vendor/bin/ecs check src --config another-config.php

Acknowledgment

The parallel run is heavily inspired by phpstan/phpstan-src by Ondřej Mirtes. Thank you.