From 0892b8149cbb1c3d6fc4feed8887303078438aa1 Mon Sep 17 00:00:00 2001 From: Sandro Gehri Date: Fri, 24 Nov 2023 13:57:20 +0100 Subject: [PATCH] Improve Mutation Generator class filtering --- WIP.md | 2 +- src/Support/MutationGenerator.php | 18 +++++++++++------- tests/Unit/MutationGeneratorTest.php | 3 +++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/WIP.md b/WIP.md index 27d8f32..24fc199 100644 --- a/WIP.md +++ b/WIP.md @@ -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 diff --git a/src/Support/MutationGenerator.php b/src/Support/MutationGenerator.php index 29ce476..09fb7bb 100644 --- a/src/Support/MutationGenerator.php +++ b/src/Support/MutationGenerator.php @@ -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; diff --git a/tests/Unit/MutationGeneratorTest.php b/tests/Unit/MutationGeneratorTest.php index 1b9dc1b..7e8c2c6 100644 --- a/tests/Unit/MutationGeneratorTest.php +++ b/tests/Unit/MutationGeneratorTest.php @@ -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], ]);