Repository navigation
Expand file tree
/
Copy pathlint-traits.php
More file actions
241 lines (196 loc) · 6.4 KB
/
Copy pathlint-traits.php
File metadata and controls
241 lines (196 loc) · 6.4 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* @file
* Trait composition check.
*
* A trait's directory determines its kind: 'src/Steps' holds the step
* vocabulary and 'src/Steps/Helper' holds the plumbing shared by step traits.
*
* This script reads both trees and fails when a step trait composes another
* step trait, or when a helper trait registers Gherkin.
*
* A helper may register a hook: the trait that owns a teardown carries the
* hook that runs it.
*
* Run with --path=path/to/repo to check a tree other than this repository.
*/
declare(strict_types=1);
/**
* Directory holding the step vocabulary, relative to the repository root.
*/
const TRAITS_STEPS_DIRECTORY = 'src/Steps';
/**
* Directory holding the plumbing, relative to the repository root.
*/
const TRAITS_HELPER_DIRECTORY = 'src/Steps/Helper';
/**
* Attributes registering Gherkin against a method.
*/
const TRAITS_VOCABULARY_ATTRIBUTES = ['Given', 'When', 'Then', 'Transform'];
// The entry function runs only when the script is run directly, not when it
// is included.
// @codeCoverageIgnoreStart
if (basename((string) $_SERVER['SCRIPT_FILENAME']) === 'lint-traits.php') {
$options = getopt('', ['path::']);
lint_traits($options);
}
// @codeCoverageIgnoreEnd
/**
* Reports every composition rule a shipped trait breaks.
*
* @param array<string, bool|string|array<int, string>> $options
* Command line options.
*
* @codeCoverageIgnoreStart
*/
function lint_traits(array $options = []): void {
$base_path = is_string($options['path'] ?? NULL) ? $options['path'] : dirname(__DIR__);
$traits = traits_collect($base_path);
$violations = traits_violations($traits);
if ($violations !== []) {
echo 'Trait composition is inconsistent:' . PHP_EOL . PHP_EOL;
echo implode(PHP_EOL, $violations) . PHP_EOL;
exit(1);
}
echo sprintf('Every trait composes what its directory allows: %d traits checked.' . PHP_EOL, count($traits));
exit(0);
}
// @codeCoverageIgnoreEnd
/**
* Reads the traits of a repository, keyed by short name.
*
* @param string $base_path
* Absolute path to the repository root.
*
* @return array<string, array{kind: string, composed: array<int, string>, members: array<int, string>}>
* 1 entry per trait, sorted by short name.
*/
function traits_collect(string $base_path): array {
$traits = [];
foreach (['steps' => TRAITS_STEPS_DIRECTORY, 'helper' => TRAITS_HELPER_DIRECTORY] as $kind => $directory) {
$path = $base_path . '/' . $directory;
if (!is_dir($path)) {
continue;
}
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS));
foreach ($iterator as $file) {
if (!$file instanceof \SplFileInfo || $file->getExtension() !== 'php') {
continue;
}
// The helpers sit under the steps directory and are walked as helpers.
if ($kind === 'steps' && str_starts_with($file->getPathname(), $base_path . '/' . TRAITS_HELPER_DIRECTORY . '/')) {
continue;
}
$facts = traits_file_facts($file->getPathname());
if (!$facts['is_trait']) {
continue;
}
$traits[$file->getBasename('.php')] = [
'kind' => $kind,
'composed' => $facts['composed'],
'members' => $facts['members'],
];
}
}
ksort($traits);
return $traits;
}
/**
* Reads the composition facts of 1 trait file.
*
* A trait declaration splits the file: an attribute after it belongs to a
* member, and a 'use' after it composes another trait.
*
* @param string $file
* Absolute path to the file to read.
*
* @return array{is_trait: bool, composed: array<int, string>, members: array<int, string>}
* Whether the file declares a trait, the traits it composes and the
* attributes on its members.
*/
function traits_file_facts(string $file): array {
$facts = ['is_trait' => FALSE, 'composed' => [], 'members' => []];
$is_declared = FALSE;
$attribute = 0;
$parentheses = 0;
$is_composing = FALSE;
foreach (token_get_all((string) file_get_contents($file)) as $token) {
$type = is_array($token) ? $token[0] : NULL;
$text = is_array($token) ? $token[1] : $token;
if ($type === T_ATTRIBUTE) {
$attribute = 1;
$parentheses = 0;
continue;
}
if ($attribute > 0) {
$attribute += (int) ($text === '[') - (int) ($text === ']');
$parentheses += (int) ($text === '(') - (int) ($text === ')');
$name = $attribute === 1 && $parentheses === 0 ? traits_name($type, $text) : NULL;
if ($name !== NULL && $is_declared) {
$facts['members'][] = $name;
}
continue;
}
if ($type === T_TRAIT) {
$is_declared = TRUE;
$facts['is_trait'] = TRUE;
continue;
}
if ($is_composing) {
$is_composing = $text !== ';' && $text !== '{';
$name = traits_name($type, $text);
if ($name !== NULL) {
$facts['composed'][] = $name;
}
continue;
}
$is_composing = $is_declared && $type === T_USE;
}
return $facts;
}
/**
* Reads the short name a token declares, if it declares one.
*
* @param int|null $type
* The token type, or NULL for a character token.
* @param string $text
* The token text.
*
* @return string|null
* The short name, or NULL when the token names nothing.
*/
function traits_name(?int $type, string $text): ?string {
if (!in_array($type, [T_STRING, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED], TRUE)) {
return NULL;
}
$separator = strrchr($text, '\\');
return $separator === FALSE ? $text : substr($separator, 1);
}
/**
* Finds every composition rule the given traits break.
*
* @param array<string, array{kind: string, composed: array<int, string>, members: array<int, string>}> $traits
* The traits to check, keyed by short name.
*
* @return array<int, string>
* 1 row per violation, in trait order.
*/
function traits_violations(array $traits): array {
$violations = [];
foreach ($traits as $name => $facts) {
if ($facts['kind'] === 'steps') {
foreach ($facts['composed'] as $composed) {
if (($traits[$composed]['kind'] ?? NULL) === 'steps') {
$violations[] = sprintf('%s composes the step trait %s', $name, $composed);
}
}
continue;
}
foreach ($facts['members'] as $member) {
if (in_array($member, TRAITS_VOCABULARY_ATTRIBUTES, TRUE)) {
$violations[] = sprintf('%s registers Gherkin through #[%s]', $name, $member);
}
}
}
return $violations;
}