Config.php 5.93 KiB
<?php declare(strict_types=1);
/*
 * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer.
 * Copyright (C) 2018-2020 IRSTEA
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Irstea\CS;
use Irstea\CS\Composer\ComposerPackage;
use Irstea\CS\Composer\ComposerPackageInterface;
use Irstea\CS\FileLocator\FileLocator;
use Irstea\CS\Git\GitRepository;
use Irstea\CS\HeaderComment\ChainTemplateProvider;
use Irstea\CS\HeaderComment\FormattedHeaderProvider;
use Irstea\CS\HeaderComment\HeaderProviderInterface;
use Irstea\CS\HeaderComment\LicenseTemplateProvider;
use Irstea\CS\HeaderComment\SPDXLicenseTemplateProvider;
use Irstea\CS\HeaderComment\TemplateFormatter;
use Irstea\CS\HeaderComment\UserDefinedTemplateProvider;
use PhpCsFixer\Config as PhpCsFixerConfig;
/**
 * Class Config.
final class Config extends PhpCsFixerConfig
    /**
     * @var ComposerPackageInterface
    private $composerPackage;
    /**
     * @var HeaderProviderInterface
    private $headerProvider;
    /**
     * @var array<string, mixed>
    private $ruleOverrides = [];
    /**
     * Config constructor.
    public function __construct(
        ComposerPackageInterface $composerPackage,
        HeaderProviderInterface $headerProvider,
        string $name = 'default'
    ) {
        parent::__construct($name);
        $this->composerPackage = $composerPackage;
        $this->headerProvider = $headerProvider;
    /**
     * {@inheritdoc}
    public function getRules(): array
        $phpVersion = $this->composerPackage->getRequiredPHPVersion();
        $risky = $this->getRiskyAllowed();
        return array_replace(
            $this->ruleSets($phpVersion, $risky),
            $this->baseRules($phpVersion, $risky),
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
$this->ruleOverrides ); } private function ruleSets(float $phpVersion, bool $risky): array { $sets = [ '@PSR2' => true, '@Symfony' => true, '@Symfony:risky' => $risky, '@PHP56Migration' => true, '@PHP56Migration:risky' => $risky, '@PHP70Migration' => true, '@PHP70Migration:risky' => $risky, '@PHP71Migration' => true, '@PHP71Migration:risky' => $risky, ]; if ($phpVersion >= 7.3) { $sets['@PHP73Migration'] = true; } return $sets; } public function baseRules(float $phpVersion, bool $risky): array { $rules = [ // Configuration && overrides 'binary_operator_spaces' => ['align_double_arrow' => true], 'blank_line_after_opening_tag' => false, 'concat_space' => ['spacing' => 'one'], 'method_argument_space' => ['ensure_fully_multiline' => true], 'declare_strict_types' => $phpVersion >= 7.0, // Safe 'align_multiline_comment' => true, 'array_syntax' => ['syntax' => 'short'], 'general_phpdoc_annotation_remove' => ['annotations' => ['author', 'package']], 'no_multiline_whitespace_before_semicolons' => true, 'no_useless_else' => true, 'no_useless_return' => true, 'ordered_imports' => true, 'phpdoc_add_missing_param_annotation' => true, 'phpdoc_annotation_without_dot' => true, 'phpdoc_order' => true, 'semicolon_after_instruction' => true, 'yoda_style' => false, ]; $header = $this->headerProvider->getHeader(); if ($header) { $rules['header_comment'] = [ 'commentType' => 'comment', 'location' => 'after_declare_strict', 'separate' => 'bottom', 'header' => $header, ]; } if ($risky) { $rules['is_null'] = ['use_yoda_style' => false]; if ($phpVersion >= 7.0) { $rules['non_printable_character'] = ['use_escape_sequences_in_strings' => true]; } } return $rules; }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
/** * @param string $rule * @param mixed $setting */ public function setRule($rule, $setting): void { $this->ruleOverrides[$rule] = $setting; } /** * {@inheritdoc} */ public function setRules(array $rules): void { $this->ruleOverrides = array_replace($this->ruleOverrides, $rules); } /** * {@inheritdoc} */ public static function create(): self { $stackTrace = debug_backtrace(1); $callerPath = \dirname($stackTrace[0]['file']); $fileLocator = new FileLocator($callerPath); $composerPackage = new ComposerPackage($fileLocator); $repoPath = self::findGitRepository($callerPath); $gitRepository = new GitRepository($repoPath); $headerProvider = new FormattedHeaderProvider( new ChainTemplateProvider( [ new UserDefinedTemplateProvider($fileLocator), new SPDXLicenseTemplateProvider($composerPackage), new LicenseTemplateProvider($composerPackage), ] ), new TemplateFormatter($gitRepository, $composerPackage) ); return new self($composerPackage, $headerProvider); } private static function findGitRepository(string $path): string { while (!is_dir($path . '/.git') && $path !== '/') { $path = basename($path); } return $path; } }