Skip to content

Commit

Permalink
Improve Mutation Generator class filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 24, 2023
1 parent ba63261 commit 0892b81
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- [ ] Awesome docs

# Backlog Prio 1
- [ ] Fix PHPStorm indexing Issues. Maybe do not end cached files with .php?
- [ ] Fix parallel for Laravel (set proper ENV variables)
- [ ] Automatically empty cache when package version changes / Maybe there is another approach: Use the same cache key per php file, but store a hash of the file content and the package version in the cache. If the hash changes, the cache is invalid.
- [ ] What should we do with interfaces? ignore them completely?
- [ ] Finish: Disable mutations by annotation
Expand Down
18 changes: 11 additions & 7 deletions src/Support/MutationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,23 @@ private function doesNotContainClassToMutate(string $contents, array $classesToM
return false;
}

foreach ($classesToMutate as $class) {
$parts = explode('\\', $class);
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) {
continue;
if (preg_match("/namespace\\s+$namespace/", $contents) === 1 && preg_match("/class\\s+$class.*/", $contents) === 1) {
return false;
}
if (preg_match("/class\\s+$class/", $contents) !== 1) {
continue;

if (preg_match("/class\\s+$class\[{\\s*\]/", $contents) === 1) {
return false;
}

return false;
if (preg_match("/namespace\\s+$classOrNamespace/", $contents) === 1) {
return false;
}
}

return true;
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/MutationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ classesToMutate: $classes,
[[AgeHelper::class, SizeHelper::class], 2],
[['AgeHelper'], 2],
[['SizeHelper'], 0],
[[AgeHelper::class], 2],
[['Tests\\Fixtures\\Classes\\AgeHelp'], 2],
[['Invalid\\Namespace\\AgeHelper'], 0],
[['Invalid\\Namespace\\AgeHelp'], 0],
[['Invalid\\Namespace\\AgeHelper', AgeHelper::class], 2],
[[SizeHelper::class, AgeHelper::class], 2],
]);
Expand Down

0 comments on commit 0892b81

Please sign in to comment.