Skip to content

Path-aware fixer sets #3126

@mlocati

Description

@mlocati

(ref. #3077)

What about implementing a way to specify fixer sets that depend on the path of the file currently being fixed?

For instance, we could have the following .php_cs configuration file:

<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use PhpCsFixer\PathAwareRules;

return Config::create()
    ->setRiskyAllowed(true)
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in(__DIR__)
    )
    ->setRules([
        '@PSR2' => true,
        'psr4' => true,
    ])
    ->addPathAwareRules(
        PathAwareRules::create()
            ->setRelativeTo(__DIR__)
            ->setInheritRules(true)
            ->setRegularExpressions([
                '/^tests\//',
            ])
            ->setRules([
                'psr4' => false,
            ])
    )
    ->addPathAwareRules(
        PathAwareRules::create()
            ->setRelativeTo(__DIR__)
            ->setInheritRules(false)
            ->setRegularExpressions([
                '/^bin\//',
                '/^dev-bin\//',
            ])
            ->setRules([
                '@PSR1' => true,
            ])
   )
;

This file:

  1. defines a default set of rules (Config::setRules())
  2. for paths relative to __DIR__ (PathAwareRules::setRelativeTo(__DIR__)) that match /^tests\// (PathAwareRules::setRegularExpressions()) we use the default set of rules (PathAwareRules::setInheritRules(true)) but disabling the psr4 fixer.
  3. for paths relative to __DIR__ (PathAwareRules::setRelativeTo(__DIR__)) that match /^bin\//or /^dev-bin\// (PathAwareRules::setRegularExpressions()) we use only (PathAwareRules::setInheritRules(false)) the @PSR1 preset.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions