Skip to content

duanle8bit/EasyCodingStandard

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Easiest Way to Use Any Coding Standard

Build Status Downloads total Subscribe

Used by Nette and Sylius.

ECS-Run

Features

Install

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

Usage

1. Create Configuration and Setup Checkers

Create an easy-coding-standard.neon in your root directory and add Sniffs or Fixers you'd love to use.

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

checkers:
    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?

More Features

Use Prepared Checker Sets

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

You pick config in CLI with --config:

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

Too long? Try --level shortcut:

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

or include more of them in config:

# easy-coding-standard.neon
includes:
    - vendor/symplify/easy-coding-standard/config/clean-code.neon
    - vendor/symplify/easy-coding-standard/config/psr2.neon

Exclude Checkers

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

includes:
    - vendor/symplify/easy-coding-standard/config/symfony.neon

parameters:
    exclude_checkers:
        - PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer

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 that you know from PHP_CodeSniffer:

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

Do you need to Include tests, *.php, *.inc or *.phpt files?

Normally you want to exclude these files, because they're not common code - they're just test files or dummy fixtures. In case you want to check them as well, you can.

Let's say you want to include *.phpt files.

  • Create a class in src/Finder/PhpAndPhptFilesProvider.php

  • Implement Symplify\EasyCodingStandard\Contract\Finder\CustomSourceProviderInterface

  • Register it as services to easy-coding-standard.neon like any other Symfony service:

    services:
        App\Finder\PhpAndPhptFilesProvider: ~

The PhpAndPhptFilesProvider might look like this:

namespace App\Finder;

use IteratorAggregate;
use Nette\Utils\Finder;
use SplFileInfo;
use Symplify\EasyCodingStandard\Contract\Finder\CustomSourceProviderInterface;

final class PhpAndPhptFilesProvider implements CustomSourceProviderInterface
{
    /**
     * @param string[] $source
     */
    public function find(array $source): IteratorAggregate
    {
        # $source is "source" argument passed in CLI
        # inc CLI: "vendor/bin/ecs check /src" => here: ['/src']
        return Finder::find('*.php', '*.phpt')->in($source);
    }
}

Don't forget to autoload it with composer.

Use any Finder you like

You can use Nette\Finder or Symfony\Finder.

FAQ

How to show all loaded checkers?

vendor/bin/ecs show

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

How to clear cache?

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

Can I use tabs?

parameters:
    indentation: tab # "spaces" by default

How do I find the slowest checkers?

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

Contributing

Send issue or pull-request to main repository.

Packages

No packages published

Languages

  • PHP 100.0%