Config.php 7.82 KiB
<?php
/*
 * This file is part of "irstea/php-cs-fixer-config".
 * (c) 2018-2019 Irstea <dsi.poleis@irstea.fr>
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Irstea\CS;
use PhpCsFixer\Config as PhpCsFixerConfig;
/**
 * Class Config.
final class Config extends PhpCsFixerConfig
    /**
     * @var string|null
    private $commit;
    /**
     * @var mixed[]
    private $cache;
    /**
     * @var array|null
    private $composerConfig;
    /**
     * @var string
    private $cacheFile = '.php_cs.commit-cache';
    /**
     * @var string
    private $docHeaderfile = '.docheader';
    /** @var array<string, mixed> */
    private $ruleOverrides = [];
    /**
     * Set cacheFile.
     * @param string $cacheFile
    public function setCacheFile($cacheFile)
        $this->cacheFile = $cacheFile;
    /**
     * Set docHeaderfile.
     * @param string $docHeaderfile
    public function setDocHeaderfile($docHeaderfile)
        $this->docHeaderfile = $docHeaderfile;
    /**
     * {@inheritdoc}
    public function getRules()
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
{ return array_replace( $this->ruleSets(), [ // 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], // 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_comment' => [ 'commentType' => 'comment', 'location' => 'after_declare_strict', 'separate' => 'bottom', 'header' => $this->headerComment(), ], ], $this->getRiskyAllowed() ? [ // Risky 'is_null' => ['use_yoda_style' => false], 'non_printable_character' => ['use_escape_sequences_in_strings' => true], ] : [], $this->ruleOverrides ); } /** * @param string $rule * @param mixed $setting */ public function setRule(string $rule, $setting) { $this->ruleOverrides[$rule] = $setting; } /** * {@inheritdoc} */ public function setRules(array $rules) { $this->ruleOverrides = array_replace($this->ruleOverrides, $rules); } /** * @return string */ private function headerComment() { $header = "Create and customize a file named {$this->docHeaderfile} at the root of the project to change this message."; if (file_exists($this->docHeaderfile)) { $header = trim(file_get_contents($this->docHeaderfile)); } return str_replace(['%yearRange%', '%package%'], [$this->getYearRange(), $this->findPackageName()], $header); }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
/** * @return string|null */ private function findPackageName() { return $this->memoize( 'package-name', function () { $config = $this->getComposerConfig(); return isset($config['name']) ? $config['name'] : null; } ); } /** * @return string */ private function getYearRange() { return $this->memoize( 'years', function () { $last = date('Y'); $first = exec('git log --format=%cd --date=format:%Y --date-order | tail -n1'); if (!$first) { $first = '???'; } return ($last !== null && $last !== $first) ? "$first-$last" : $first; } ); } /** * @return bool[] */ private function ruleSets() { $phpVersion = $this->findRequiredPHPVersion(); $risky = $this->getRiskyAllowed(); return [ '@PSR2' => true, '@Symfony' => true, '@Symfony:risky' => $risky, '@PHP56Migration' => $phpVersion >= 5.6, '@PHP56Migration:risky' => $risky && $phpVersion >= 5.6, '@PHP70Migration' => $phpVersion >= 7.0, '@PHP70Migration:risky' => $risky && $phpVersion >= 7.0, '@PHP71Migration' => $phpVersion >= 7.1, '@PHP71Migration:risky' => $risky && $phpVersion >= 7.1, ]; } /** * @return float */ private function findRequiredPHPVersion() { return $this->memoize( 'php-req', function () { $config = $this->getComposerConfig(); if (isset($config['require']['php'])) { if (preg_match('/(?:>=?|\^|~)\s*([57]\.\d)/', $config['require']['php'], $groups)) { return (float) $groups[1]; } }
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
return 5.6; } ); } /** * @return array */ private function getComposerConfig() { if ($this->composerConfig !== null) { return $this->composerConfig; } $this->composerConfig = []; if (file_exists('composer.json')) { $data = json_decode(file_get_contents('composer.json'), true); if (\is_array($data)) { $this->composerConfig = $data; } } return $this->composerConfig; } /** * @param string $key * @param callable $generator * * @return mixed */ private function memoize($key, $generator) { $commit = $this->getHeadCommit(); $cache = $this->loadCache(); if (!isset($cache[$commit][$key])) { if (!isset($cache[$commit])) { // TODO: load a template from a local file. $cache[$commit] = []; } $cache[$commit][$key] = $generator(); $this->saveCache($cache); } return $cache[$commit][$key]; } /** * @return string */ private function getHeadCommit() { if ($this->commit === null) { $this->commit = trim(shell_exec('git rev-parse HEAD')); } return $this->commit; } /** * @return mixed[] */ private function loadCache() { if ($this->cache === null) { $this->cache = []; if ($this->getUsingCache() && file_exists($this->cacheFile)) { $this->cache = json_decode(file_get_contents($this->cacheFile), true); } }
281282283284285286287288289290291292293
return $this->cache; } /** * @param array $cache */ private function saveCache(array $cache) { file_put_contents($this->cacheFile, json_encode($cache)); } }