EasyCodingStandard

[READ-ONLY] Easiest way to start using PHP CS Fixer and PHP_CodeSniffer with 0-knowledge

Github星跟踪图

The Easiest Way to Use Any Coding Standard

Downloads total

ECS-Run

Used by:

Features

Are you already using another tool?

Install

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

Usage

1. Create Configuration and Setup Checkers

Create an ecs.yaml in your root directory and add Sniffs or Fixers you'd love to use.

Let's start with the most common one - array() => []:

# ecs.yaml
services:
    PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer:
        syntax: short

2. Run in CLI

# dry
vendor/bin/ecs check src

# fix
vendor/bin/ecs check src --fix

Tip: Do you want autocomplete too? Just use Symfony Plugin

ECS-Run

Features

Use Prepared Checker Sets

There are prepared sets in /config/set directory that you can use:

You pick config in CLI with --config:

vendor/bin/ecs check src --config vendor/symplify/easy-coding-standard/config/set/clean-code.yaml

Too long? Try --set shortcut:

vendor/bin/ecs check src --set clean-code

or include more of them in config:

# ecs.yaml
parameters:
    sets:
        - 'clean-code'
        - 'psr12'

Set Paths

You have 2 options to set paths you want to analyse.

  1. In CLI:
vendor/bin/ecs check src
  1. In paths parameter in ecs.yaml:
# ecs.yaml
parameters:
    paths:
        - 'src'
        - 'tests'

The CLI has higher priority than parameter, so if you use CLI argument, the sets parameter will be ignored.

Exclude Checkers

What if you add symfony set, but don't like PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer?

# ecs.yaml
parameters:
    sets:
        - 'symfony'

    skip:
        PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer: ~

Include Checkers Only for Some Paths

This feature is the exact opposite of skip. Do you want to run your rule only on new code? Limit it with only parameter:

services:
    Symplify\CodingStandard\Sniffs\Naming\AbstractClassNameSniff: ~

parameters:
    only:
        Symplify\CodingStandard\Sniffs\Naming\AbstractClassNameSniff:
            - 'src/NewCode/*'

Ignore What You Can't Fix

Sometimes, checker finds an error in code that inherits from code you can't change.

No worries! Just skip checker for this file:

parameters:
    skip:
        SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
            # relative path to file (you can copy this from error report)
            - 'packages/EasyCodingStandard/packages/SniffRunner/src/File/File.php'

            # or multiple files by path to match against "fnmatch()"
            - '*packages/CodingStandard/src/Sniffs/*/*Sniff.php'

You can also skip specific codes or messages that you know from PHP_CodeSniffer:

parameters:
    skip:
        # code to skip for all files
        SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.UselessDocComment: ~

        # same syntax is used for skipping specific sniff messages
        'Cognitive complexity for method "addAction" is 13 but has to be less than or equal to 8.': ~

        # code to skip for specific files/patterns
        SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversableParameterTypeHintSpecification:
            -  '*src/Form/Type/*Type.php'

Or just 2 files?

parameters:
    exclude_files:
        # generated files
        - 'lib/PhpParser/Parser/Php5.php'
        - 'lib/PhpParser/Parser/Php7.php'
        # or with fnmatch() pattern
        - '*/lib/PhpParser/Parser/Php*.php'

Do you need to Include other than *.php files?

# ecs.yaml
parameters:
    file_extensions:
        - 'php'
        - 'phpt'

FAQ

How can I see all loaded checkers?

vendor/bin/ecs show
vendor/bin/ecs show --config ...

How do I find checkers by group or type?

vendor/bin/ecs find
vendor/bin/ecs find symplify # for Symplify rules
vendor/bin/ecs find array # for array-related rules

How do I clear cache?

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

How can I change the cache directory?

parameters:
    cache_directory: .ecs_cache # defaults to sys_get_temp_dir() . '/_changed_files_detector_tests'

How can I change the cache namespace?

parameters:
    cache_namespace: my_project_namespace # defaults to Strings::webalize(getcwd())'

This comes in handy when you want to apply ecs caching mechanism on your gitlab pipelines for example, where getcwd() may not always produce same cache key, thus introducing side effect, where cache may not be detected correctly.

Example getcwd() on gitlab CI:

  • /builds/0956d275/0/sites/my_project
  • /builds/0956d275/1/sites/my_project
  • /builds/0956d275/2/sites/my_project

Can I use tabs, 2 spaces or "\r\n" line endings?

parameters:
    indentation: "tab" # "spaces" by default, you can also use "  " (2 spaces), "    " (4 spaces) or "	" (tab)
    line_ending: "\r\n" # PHP_EOL by default; you can also use "\n"

Your IDE Integration

PHPStorm

EasyCodingStandard can be used as an External Tool

PHPStorm Configuration

Go to Preferences > Tools > External Tools and click + to add a new tool.

  • Name: ecs (Can be any value)
  • Description: easyCodingStandard (Can be any value)
  • Program: $ProjectFileDir$/vendor/bin/ecs (Path to ecs executable; On Windows path separators must be a \)
  • Parameters: check $FilePathRelativeToProjectRoot$ (append --fix to auto-fix)
  • Working directory: $ProjectFileDir$

Press Cmd/Ctrl + Shift + A (Find Action), search for ecs, and then hit Enter. It will run ecs for the current file.

To run ecs on a directory, right click on a folder in the project browser go to external tools and select ecs.

You can also create a keyboard shortcut in Preferences > Keymap to run ecs.

Visual Studio Code

EasyCodingStandard for Visual Studio Code extension adds support for running EasyCodingStandard inside the editor.

Tool Integration, Tool, Extension, Description, ----, ---------, -----------, GrumPHP, ECS Task, Provides a new task for GrumPHP which runs ECS, ## Contributing

Send issue or pull-request to main repository.

主要指标

概览
名称与所有者easy-coding-standard/easy-coding-standard
主编程语言PHP
编程语言PHP (语言数: 3)
平台
许可证MIT License
所有者活动
创建于2017-02-28 15:14:51
推送于2025-05-30 11:42:09
最后一次提交
发布数734
最新版本名称12.5.20 (发布于 )
第一版名称v1.4.0 (发布于 )
用户参与
星数1.5k
关注者数19
派生数86
提交数336
已启用问题?
问题数102
打开的问题数6
拉请求数143
打开的拉请求数0
关闭的拉请求数33
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?