-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMutationGenerator.php
More file actions
212 lines (169 loc) · 6.76 KB
/
Copy pathMutationGenerator.php
File metadata and controls
212 lines (169 loc) · 6.76 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
<?php
declare(strict_types=1);
namespace Pest\Mutate\Support;
use Pest\Mutate\Contracts\Mutator;
use Pest\Mutate\Factories\NodeTraverserFactory;
use Pest\Mutate\Mutation;
use Pest\Support\Str;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use Symfony\Component\Finder\SplFileInfo;
class MutationGenerator
{
private bool $mutated;
private int $offset;
private Node $originalNode; // @phpstan-ignore-line
private ?Node $modifiedNode = null; // @phpstan-ignore-line
/**
* @param array<int, class-string<Mutator>> $mutators
* @param array<int, int> $linesToMutate
* @param array<int, string> $classesToMutate
* @return array<int, Mutation>
*/
public function generate(
SplFileInfo $file,
array $mutators,
array $linesToMutate = [],
array $classesToMutate = [],
): array {
$mutations = [];
$contents = $file->getContents();
if ($this->doesNotContainClassToMutate($contents, $classesToMutate)) {
return $mutations;
}
$mutatorsToIgnoreByLine = [];
foreach (explode(PHP_EOL, $contents) as $lineNumber => $line) {
if (str_contains($line, '@pest-mutate-ignore')) {
if (Str::after($line, '@pest-mutate-ignore:') !== $line) {
$mutatorsToIgnore = explode(',', Str::after($line, '@pest-mutate-ignore:'));
$mutatorsToIgnore = array_map(fn (string $mutator): string => trim($mutator), $mutatorsToIgnore);
}
$mutatorsToIgnoreByLine[$lineNumber + 1] = $mutatorsToIgnore ?? ['all'];
}
}
$parser = PhpParserFactory::make();
$mutators = $this->filterMutators($mutators, $contents, $parser);
$cache = MutationCache::instance();
foreach ($mutators as $mutator) {
if ($cache->has($file, $contents, $linesToMutate, $mutator)) {
$newMutations = $cache->get($file, $contents, $linesToMutate, $mutator);
} else {
$newMutations = [];
$this->offset = 0; // @pest-mutate-ignore: IntegerIncrement,IntegerDecrement
while (true) {
$this->mutated = false;
$traverser = NodeTraverserFactory::create();
$traverser->addVisitor(new NodeVisitor(
mutator: $mutator,
offset: $this->offset,
linesToMutate: $linesToMutate,
mutatorsToIgnoreByLine: $mutatorsToIgnoreByLine,
hasAlreadyMutated: $this->hasMutated(...),
trackMutation: $this->trackMutation(...),
));
$stmts = $parser->parse($contents);
assert($stmts !== null);
$modifiedAst = $traverser->traverse($stmts);
if (! $this->mutated) { // @phpstan-ignore-line
break;
}
$newMutations[] = Mutation::create( // @phpstan-ignore-line
file: $file,
mutator: $mutator,
originalNode: $this->originalNode,
modifiedNode: $this->modifiedNode,
modifiedAst: $modifiedAst,
);
}
$cache->put($file, $contents, $linesToMutate, $mutator, $newMutations);
}
$mutations = [
...$mutations,
...$newMutations,
];
}
// // filter out mutations that are ignored
// $mutations = array_filter($mutations, function (Mutation $mutation) use ($ignoreComments): bool {
// foreach ($ignoreComments as $comment) {
// if ($comment['line'] === $mutation->startLine) {
// return false;
// }
// }
//
// return true;
// });
// sort mutations by line number
usort($mutations, fn (Mutation $a, Mutation $b): int => $a->startLine <=> $b->startLine);
return $mutations;
}
private function trackMutation(int $nodeCount, Node $original, ?Node $modified): void
{
$this->mutated = true;
$this->offset = $nodeCount;
$this->originalNode = $original;
$this->modifiedNode = $modified;
}
private function hasMutated(): bool
{
return $this->mutated;
}
/**
* @param array<int, string> $classesToMutate
*/
private function doesNotContainClassToMutate(string $contents, array $classesToMutate): bool
{
if ($classesToMutate === []) {
return false;
}
foreach ($classesToMutate as $classOrNamespace) {
$parts = explode('\\', $classOrNamespace);
$class = array_pop($parts);
$namespace = preg_quote(implode('\\', $parts));
$classOrNamespace = preg_quote($classOrNamespace);
if (preg_match("/namespace\\s+$namespace/", $contents) === 1 && preg_match("/(?:class|trait)\\s+$class\b.*/", $contents) === 1) {
return false;
}
if (preg_match("/(?:class|trait)\\s+$class\[{\\s*\]/", $contents) === 1) {
return false;
}
if (preg_match("/namespace\\s+$classOrNamespace/", $contents) === 1) {
return false;
}
}
return true;
}
/**
* @param array<int, class-string<Mutator>> $mutators
* @return array<int, class-string<Mutator>>
*/
private function filterMutators(array $mutators, string $contents, Parser $parser): array
{
$nodeTypes = [];
$traverser = new NodeTraverser;
$traverser->addVisitor(new class(function (string $nodeType) use (&$nodeTypes): string {
return $nodeTypes[] = $nodeType;
}) extends NodeVisitorAbstract {
/**
* @param callable $callback
*/
public function __construct(private $callback) // @pest-ignore-type
{}
public function enterNode(Node $node): ?Node
{
($this->callback)($node::class);
return null;
}
});
$traverser->traverse($parser->parse($contents)); // @phpstan-ignore-line
$nodeTypes = array_unique($nodeTypes);
$mutatorsToUse = [];
foreach ($nodeTypes as $nodeType) {
foreach (MutatorMap::get()[$nodeType] ?? [] as $mutator) {
$mutatorsToUse[] = $mutator;
}
}
return array_intersect($mutators, $mutatorsToUse);
}
}