refreshReadmeTable.php ➔ getCodeDesc()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 23
rs 9.552
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 (
112
                                    $codeHasMatchingSuppress = in_array(
113
                                        $foundSuppressValue,
114
                                        [$constant, $constantValue],
115
                                        true
116
                                    )
117
                                ) {
118
                                    $codes[$sniffRule][1] = 'yes';
119
120
                                    break;
121
                                }
122
                            }
123
124
                            if (!$codeHasMatchingSuppress) {
125
                                $codes[$sniffRule][1] = false;
126
                            }
127
                        }
128
                    }
129
                }
130
            }
131
        } catch (ReflectionException $e) {
132
            echo $e;
133
        }
134
    }
135
    return $codes;
136
}
137
138
/**
139
 * Saves the table in the table.md and outputs it.
140
 *
141
 * @param array $codes
142
 *
143
 * @return void
144
 */
145
function outputCodesTable(array $codes): void
146
{
147
    ksort($codes);
148
149
    file_put_contents(
150
        $tmpFile = __DIR__ . DIRECTORY_SEPARATOR . 'table.md',
151
        <<<EOD
152
| Sniff | Description | suppressable |
153
| ----- | ----------- | ------------ |
154
EOD
155
    );
156
157
    foreach ($codes as $code => $codeRule) {
158
        [$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...
159
160
        file_put_contents(
161
            $tmpFile,
162
            sprintf(
163
                "\n| %s | %s | %s |",
164
                $code,
165
                $description,
166
                $hasSuppresses ?: 'no'
167
            ),
168
            FILE_APPEND
169
        );
170
    }
171
172
    echo file_get_contents($tmpFile);
173
}
174