php_cs.dist 3.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
define('COMMIT_CACHE', '.php_cs.commit-cache');

$finder = PhpCsFixer\Finder::create()
    ->exclude('vendor')
    ->exclude('node_modules')
    ->exclude('cache')
    ->files()
    ->name('*.php')
    ->in('.');

$yearRange = getGitCommitYears();

$ruleSets = ['@PSR2' => true, '@Symfony' => true];

$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],

    // Risky
    'is_null'                                   => ['use_yoda_style' => false],
    'non_printable_character'                   => ['use_escape_sequences_in_strings' => 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'      => <<<HEADER
Copyright (C) $yearRange IRSTEA
All rights reserved.
HEADER
        ,
    ],
];

$phpVersion = findComposerPhpReq();

if ($phpVersion >= 5.6) {
    $ruleSets['@PHP56Migration'] = true;
}
if ($phpVersion >= 7.0) {
    $ruleSets['@PHP70Migration'] = true;
    $rules['declare_strict_types'] = true;
}
if ($phpVersion >= 7.1) {
    $ruleSets['@PHP71Migration'] = true;
}

echo "Rulesets: " . implode(', ', array_keys($ruleSets)) . ".\n";

return PhpCsFixer\Config::create()
  ->setRiskyAllowed(true)
  ->setUsingCache(false)
  ->setIndent('    ')
  ->setLineEnding("\n")
  ->setRules(array_merge($ruleSets, $rules))
  ->setFinder($finder);

/**
 * @return string
 */
function getGitCommitYears(): string
{
    return getCachedValue('years', function () {
        echo "Examining git history...\n";
        $last = date('Y');
        $first = exec('git log --format=%cd --date=format:%Y --date-order | tail -n1') ?? $last;
        return (null !== $last && $last !== $first) ? "$first-$last" : $first;
    });
}

/**
 * @return float
 */
function findComposerPhpReq()
{
    return getCachedValue('php-req', function () {
        if (file_exists('composer.json')) {
            $data = json_decode(file_get_contents('composer.json'), true);
            if (is_array($data) && isset($data['require']['php'])) {
                if (preg_match('/(?:>=?|\^|~)\s*([57]\.[0-9])/', $data['require']['php'], $groups)) {
                    return (float) $groups[1];
                }
            }
        }

        return 5.6;
    });
}

/**
 * @param string   $key
 * @param callable $produce
 *
 * @return string|int|bool|array
 */
function getCachedValue($key, $produce)
{
    static $commit = null;
    if (null === $commit) {
        $commit = trim(`git rev-parse HEAD`);
    }
    if (file_exists(COMMIT_CACHE) && filemtime(COMMIT_CACHE) >= filemtime(__FILE__)) {
        $cache = json_decode(file_get_contents(COMMIT_CACHE), true);
    } else {
        $cache = [];
    }
    if (!isset($cache[$commit][$key])) {
        if (!isset($cache[$commit])) {
            $cache[$commit] = [];
        }
        $cache[$commit][$key] = $produce();
        file_put_contents(COMMIT_CACHE, json_encode($cache));
    }

    return $cache[$commit][$key];
}

// vim:filetype=php