/EasyCodingStandard

[READ-ONLY] Easiest and fastest way to start coding standard in your project

Primary LanguagePHPMIT LicenseMIT

Combine PHP_CodeSniffer and PHP-CS-Fixer in one Config

Build Status Downloads total

Includes caching that speeds-up 2nd run to few seconds, skipping files for specific checkers and checker autocomplete.

The easiest coding standard to start with:

# easy-coding-standard.neon
checkers:
    - PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowShortArraySyntaxSniff
    - PhpCsFixer\Fixer\ArrayNotation\TrailingCommaInMultilineArrayFixer

Install

composer require symplify/easy-coding-standard

Usage

1. Create Configuration and Setup Checkers

Create an easy-coding-standard.neon file in your root directory.

Here you can use 2 checker classes: Sniffs and Fixers

Start Slow, Grow Fast

Let's start slow with 2 checkers, so we have everything under control.

checkers:
    - PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowShortArraySyntaxSniff
    - PhpCsFixer\Fixer\ArrayNotation\TrailingCommaInMultilineArrayFixer

Don't write Checker Classes, Make use of NEON Plugin

I didn't really type PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowShortArraySyntaxSniff. I'm too lazy for that.

I used class autocomplete thanks to awesome NEON plugin for PHPStorm by David Matejka 👏.

ECS-Run

2. Run it in CLI... and Fix It!

vendor/bin/ecs check src

You can also use name matching:

vendor/bin/ecs check src/Doctrine*

Or multiple sources:

vendor/bin/ecs check src tests

You can also use lazy-friendly typo-proof shortcut ecs:

vendor/bin/ecs check src tests

How to Fix Things?

vendor/bin/ecs check src --fix

ECS-Run

How to Clear Cache?

To be sure your code base it checked completely, just clear the cache:

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

Cache stores all files without errors that haven't changed. It's handled by ChangedFilesDetector

More Features

Use Prepared Checker Sets

There are several common sets you can use. You find all in /config directory:

vendor/symplify/easy-coding-standard/config/php54-checkers.neon
vendor/symplify/easy-coding-standard/config/php70-checkers.neon
vendor/symplify/easy-coding-standard/config/php71-checkers.neon
vendor/symplify/easy-coding-standard/config/psr2-checkers.neon
vendor/symplify/easy-coding-standard/config/symfony-checkers.neon
vendor/symplify/easy-coding-standard/config/symfony-risky-checkers.neon
vendor/symplify/easy-coding-standard/config/symplify.neon
vendor/symplify/easy-coding-standard/config/spaces.neon
vendor/symplify/easy-coding-standard/config/common.neon

You pick single config in CLI:

vendor/bin/ecs --config vendor/symplify/easy-coding-standard/config/symfony-checkers.neon

or include more files in config:

includes:
    - vendor/symplify/easy-coding-standard/php70-checkers.neon
    - vendor/symplify/easy-coding-standard/php71-checkers.neon
    - vendor/symplify/easy-coding-standard/psr2-checkers.neon

Exclude Checkers

If you use preset, there might be 1 or 2 checkers you don't like. I love Symfony set, but I don't like PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer:

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

Configure Your Checker

There are also user-friendly checkers that allow you to setup YOUR preferences.

For example short array [] vs long array(). I prefer []:

checkers:
    PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer:
        syntax: short

Ignore What You Can't Fix

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

No worries! You can skip checker for this file in skip section.

parameters:
    skip:
        # checkers to skip (you can use autocomplete here as well)
        SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
            # A. relative path to file (I usually just copy this from error report)
            - packages/EasyCodingStandard/packages/SniffRunner/src/File/File.php
            # B. or multiple files by path to match against "fnmatch()"
            - *packages/CodingStandard/src/Sniffs/*/*Sniff.php

show Command to Display All Checkers

To see how many checkers and which exactly are loaded run:

vendor/bin/ecs show

List of active checkers will be shown.

You can also see what checkers are in another config

vendor/bin/ecs show --config vendor/nette/coding-standard/coding-standard-php71.neon

Or explore a fixer set of PHP-CS-Fixer:

vendor/bin/ecs show --fixer-set Symfony

Or explore or a sniff set of PHP_CodeSniffer:

vendor/bin/ecs show --sniff-set PSR2

And print with config-like configuration - handy for copy-pasting to your own config:

vendor/bin/ecs show --fixer-set Symfony --with-config

Pick Config in CLI

Do you want to use another config than easy-coding-standard.neon in your root directory?

You can use --config option:

vendor/bin/ecs show --config vendor/nette/coding-standard/general-coding-standard.neon

How to Combine More Configs

Do you have one global configuration and still want something extra for this repository? Just include the global configuration via includes section and add extra checkers.

# easy-coding-standard.neon

includes:
    - vendor/nette/coding-standard/general-coding-standard.neon

checkers:
    - PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer

Tabs over Spaces? We got this!

parameters:
    indentation: tab # "spaces" by default

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.

Contributing

Send issue or pull-request to main repository.