Passed
Push — master ( 3edb00...c81ad5 )
by Björn
02:28
created

refreshReadmeTable.php ➔ outputCodesTable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
require_once __DIR__ . '/../vendor/autoload.php';
6
7
$directory = new RecursiveDirectoryIterator($baseFolder = './src/Standards/BestIt/Sniffs');
8
$iterator = new RecursiveIteratorIterator($directory);
9
$regexIterator = new RegexIterator($iterator, '/^.+Sniff\.php$/i', RecursiveRegexIterator::GET_MATCH);
10
11
outputCodesTable(handleFiles($regexIterator, $baseFolder));
12
13
/**
14
 * Returns the constants of the class.
15
 *
16
 * @param string $fullQualifiedClassName
17
 *
18
 * @return array
19
 */
20
function getConstants(string $fullQualifiedClassName): array
21
{
22
    $reflection = new ReflectionClass($fullQualifiedClassName);
23
    $constants = $reflection->getConstants();
24
25
    return !$reflection->isAbstract() ? $constants : [];
26
}
27
28
/**
29
 * Returns the description for the code constant by parsing the doc block.
30
 *
31
 * @throws DomainException If there is no valid doc block.
32
 *
33
 * @param string $fullQualifiedClassName
34
 * @param string $constant
35
 *
36
 * @return string
37
 */
38
function getCodeDesc(string $fullQualifiedClassName, string $constant): string
39
{
40
    $constReflection = new ReflectionClassConstant($fullQualifiedClassName, $constant);
41
42
    // $re = ;
43
    if (!$docComment = $constReflection->getDocComment()) {
44
        throw new DomainException(
45
            sprintf('There should be a doc block for %s:%s', $fullQualifiedClassName, $constant)
46
        );
47
    }
48
49
    if (!preg_match('~/\*\*\s(.*)\s~m', $docComment, $matches)) {
50
        throw new DomainException(
51
            sprintf(
52
                'There should be a doc block with summary for %s:%s',
53
                $fullQualifiedClassName,
54
                $constant
55
            )
56
        );
57
    }
58
59
    return trim($matches[1], '* ');
60
}
61
62
/**
63
 * Iterates thru the base folder and parses the sniff files for the documentation.
64
 *
65
 * @param Iterator $regexIterator
66
 * @param string $baseFolder
67
 *
68
 * @return array
69
 */
70
function handleFiles(Iterator $regexIterator, string $baseFolder): array
71
{
72
    $codes = [];
73
74
    foreach ($regexIterator as $file) {
75
        [$file] = $file;
76
77
        $simpleClassName = str_replace([$baseFolder . DIRECTORY_SEPARATOR, '.php', 'Sniff'], '', $file);
78
        $fullQualifiedClassName = 'BestIt\\Sniffs\\' . str_replace('/', '\\', $simpleClassName) . 'Sniff';
79
80
        $hasSuppresses = (bool) preg_match_all(
81
            '/->isSniffSuppressed\((?P<code>\s*.*\s*)\)/mU',
82
            file_get_contents($file),
83
            $suppresses
84
        );
85
86
        try {
87
            $constants = getConstants($fullQualifiedClassName);
88
89
            foreach ($constants as $constant => $constantValue) {
90
                if (substr($constant, 0, 5) === 'CODE_') {
91
                    $sniffDesc = getCodeDesc($fullQualifiedClassName, $constant);
92
93
                    $sniffRule = sprintf(
94
                        'BestIt.%s.%s',
95
                        str_replace(DIRECTORY_SEPARATOR, '.', $simpleClassName),
96
                        $constantValue
97
                    );
98
99
                    $codes[$sniffRule] = [$sniffDesc, $hasSuppresses];
100
101
                    if ($hasSuppresses) {
102
                        if (!array_filter($suppresses['code'])) {
103
                            $codes[$sniffRule][1] = 'yes by class';
104
                        } else {
105
                            $codeHasMatchingSuppress = false;
106
107
                            foreach ($suppresses['code'] as $foundSuppress) {
108
                                $foundSuppressValue = str_replace(['self::', 'static::'], '', $foundSuppress);
109
110
111
                                if ($codeHasMatchingSuppress = in_array(
112
                                    $foundSuppressValue,
113
                                    [$constant, $constantValue],
114
                                    true
115
                                )) {
116
                                    $codes[$sniffRule][1] = 'yes';
117
118
                                    break;
119
                                }
120
                            }
121
122
                            if (!$codeHasMatchingSuppress) {
123
                                $codes[$sniffRule][1] = false;
124
                            }
125
                        }
126
                    }
127
                }
128
            }
129
        } catch (ReflectionException $e) {
130
            echo $e;
131
        }
132
    }
133
    return $codes;
134
}
135
136
/**
137
 * Saves the table in the table.md and outputs it.
138
 *
139
 * @param array $codes
140
 *
141
 * @return void
142
 */
143
function outputCodesTable(array $codes): void
144
{
145
    ksort($codes);
146
147
    file_put_contents(
148
        $tmpFile = __DIR__ . DIRECTORY_SEPARATOR . 'table.md',
149
        <<<EOD
150
| Sniff | Description | suppressable |
151
| ----- | ----------- | ------------ |
152
EOD
153
    );
154
155
    foreach ($codes as $code => $codeRule) {
156
        [$description, $hasSuppresses] = $codeRule;
0 ignored issues
show
Bug introduced by
The variable $description does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $hasSuppresses does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
157
158
        file_put_contents(
159
            $tmpFile,
160
            sprintf(
161
                "\n| %s | %s | %s |",
162
                $code,
163
                $description,
164
                $hasSuppresses ?: 'no'
165
            ),
166
            FILE_APPEND
167
        );
168
    }
169
170
    echo file_get_contents($tmpFile);
171
}
172