From f33e169df572f25d4f63c81712d5c842ea5bfbc6 Mon Sep 17 00:00:00 2001 From: Steffen Grahl Date: Sat, 21 Feb 2026 19:24:50 +0100 Subject: [PATCH 1/4] Add test coverage for a second command run. --- Applier.php | 28 ++++++++++++++-------------- AuditClassNameGenerator.php | 26 ++++++++++++++++++++++++++ Tests/ApplierTest.php | 12 ++++++++++++ 3 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 AuditClassNameGenerator.php diff --git a/Applier.php b/Applier.php index 5699645..00cf60e 100644 --- a/Applier.php +++ b/Applier.php @@ -8,24 +8,24 @@ public function applyAllAuditTags(string $contents): string { preg_match_all('/class="[^"]*"/', $contents, $matches); - foreach ($matches as $index => $match) { - if ($index = 0) { + foreach ($matches as $match) { + if (!is_array($match)) { continue; } - if (is_array($match)) { - foreach ($match as $matchingString) { - $hash = md5(rand(0, 1000000000) . "This will provide a nice, long, randomish string."); - $shorterFingerprint = 'audit_' . substr($hash, 0, 6); - $withSpace = ' ' . $shorterFingerprint . ' '; - - //Now just do a simple string replacement - $contents = str_replace( - $matchingString, - substr($matchingString, 0, -1) . $withSpace . '"', - $contents, - ); + foreach ($match as $matchingString) { + if (str_contains($matchingString, 'audit_')) { + continue; } + + $withSpace = AuditClassNameGenerator::generate(); + + //Now just do a simple string replacement + $contents = str_replace( + $matchingString, + substr($matchingString, 0, -1) . $withSpace . '"', + $contents, + ); } } diff --git a/AuditClassNameGenerator.php b/AuditClassNameGenerator.php new file mode 100644 index 0000000..3f71a74 --- /dev/null +++ b/AuditClassNameGenerator.php @@ -0,0 +1,26 @@ +assertMatchesRegularExpression('/class="foo audit_[0-9a-f]{6} "/', $output); } + public function testReRunSingleClassInjection(): void + { + $applier = new Applier(); + + $input = '
'; + $output = $applier->applyAllAuditTags($input); + + $this->assertMatchesRegularExpression('/class="foo audit_[0-9a-f]{6} "/', $output); + } public function testMultipleClasses(): void { From 28af302502518ce3afcd4b6437632c44331e0622 Mon Sep 17 00:00:00 2001 From: Steffen Grahl Date: Sun, 22 Feb 2026 12:07:54 +0100 Subject: [PATCH 2/4] Update namespace after moving class to right place --- .../BusinessLogic/AuditClassNameGenerator.php | 9 +++++++-- tests/Application/BusinessLogic/ApplierTest.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Application/BusinessLogic/AuditClassNameGenerator.php b/src/Application/BusinessLogic/AuditClassNameGenerator.php index 3f71a74..bde498c 100644 --- a/src/Application/BusinessLogic/AuditClassNameGenerator.php +++ b/src/Application/BusinessLogic/AuditClassNameGenerator.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace PatrickMaynard\AuditClassGenerator; +namespace PatrickMaynard\AuditClassGenerator\Application\BusinessLogic; class AuditClassNameGenerator { @@ -17,7 +17,12 @@ public static function generate(): string ' %s_%s ', self::AUDIT_CLASS_PREFIX, substr( - md5(random_int(0, self::UPPER_THRESHOLD_RANDOM_INT) . self::RANDOM_STRING_SUFFIX), + md5( + AuditClassNameGenerator . phprandom_int( + 0, + self::UPPER_THRESHOLD_RANDOM_INT + ) . self::RANDOM_STRING_SUFFIX + ), 0, self::MAX_HASH_LENGTH ) diff --git a/tests/Application/BusinessLogic/ApplierTest.php b/tests/Application/BusinessLogic/ApplierTest.php index daf4d1b..587f356 100644 --- a/tests/Application/BusinessLogic/ApplierTest.php +++ b/tests/Application/BusinessLogic/ApplierTest.php @@ -3,7 +3,7 @@ namespace PatrickMaynard\AuditClassGenerator\Tests\Application\BusinessLogic; use PatrickMaynard\AuditClassGenerator\Application\BusinessLogic\Applier; -use PatrickMaynard\AuditClassGenerator\AuditClassNameGenerator; +use PatrickMaynard\AuditClassGenerator\Application\BusinessLogic\AuditClassNameGenerator; use PHPUnit\Framework\TestCase; class ApplierTest extends TestCase From 1c3c29777041841b193da740feca406a0914201c Mon Sep 17 00:00:00 2001 From: Steffen Grahl Date: Sun, 22 Feb 2026 12:09:37 +0100 Subject: [PATCH 3/4] Move instantiationtest to right test case folder --- .../Application/{Config => BusinessLogic}/InstantiationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/Application/{Config => BusinessLogic}/InstantiationTest.php (81%) diff --git a/tests/Application/Config/InstantiationTest.php b/tests/Application/BusinessLogic/InstantiationTest.php similarity index 81% rename from tests/Application/Config/InstantiationTest.php rename to tests/Application/BusinessLogic/InstantiationTest.php index 219a44c..e5510a1 100644 --- a/tests/Application/Config/InstantiationTest.php +++ b/tests/Application/BusinessLogic/InstantiationTest.php @@ -1,6 +1,6 @@ Date: Sun, 22 Feb 2026 12:15:24 +0100 Subject: [PATCH 4/4] Fix strange code change after using IDE move tool on file --- src/Application/BusinessLogic/AuditClassNameGenerator.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Application/BusinessLogic/AuditClassNameGenerator.php b/src/Application/BusinessLogic/AuditClassNameGenerator.php index bde498c..ae68731 100644 --- a/src/Application/BusinessLogic/AuditClassNameGenerator.php +++ b/src/Application/BusinessLogic/AuditClassNameGenerator.php @@ -17,12 +17,7 @@ public static function generate(): string ' %s_%s ', self::AUDIT_CLASS_PREFIX, substr( - md5( - AuditClassNameGenerator . phprandom_int( - 0, - self::UPPER_THRESHOLD_RANDOM_INT - ) . self::RANDOM_STRING_SUFFIX - ), + md5(random_int(0, self::UPPER_THRESHOLD_RANDOM_INT) . self::RANDOM_STRING_SUFFIX), 0, self::MAX_HASH_LENGTH )