From 2a4be6dc7033d6dc617d7fef8f890a74d1809452 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Jul 2026 18:27:57 +0700 Subject: [PATCH 1/2] Fix scoper to clean up prefix under getRuleDefinition() method (part 2) --- scoper.php | 2 +- tests/Scoper/ScoperPatchersTest.php | 38 ++++++++++++++++++----------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/scoper.php b/scoper.php index d6da45ea9e7..dfb26945094 100644 --- a/scoper.php +++ b/scoper.php @@ -119,7 +119,7 @@ static function (string $filePath, string $prefix, string $content): string { return Strings::replace( $content, - '#(public function getRuleDefinition\(\): RuleDefinition\s+\{\R)(.*?)(\R \})#s', + '#(public function getRuleDefinition\(\): RuleDefinition\s+\{\R)(.*?)(\R \);\R \})#s', static function (array $match) use ($prefix): string { $body = str_replace($prefix . '\\', '', $match[2]); $body = Strings::replace($body, '#^\s*namespace ' . preg_quote($prefix, '#') . ';\R\R?#m', ''); diff --git a/tests/Scoper/ScoperPatchersTest.php b/tests/Scoper/ScoperPatchersTest.php index 860db4d4a63..ab7f8659a39 100644 --- a/tests/Scoper/ScoperPatchersTest.php +++ b/tests/Scoper/ScoperPatchersTest.php @@ -4,7 +4,6 @@ namespace Rector\Tests\Scoper; -use Webmozart\Assert\Assert; use Closure; use PHPUnit\Framework\TestCase; use Symfony\Component\Finder\Finder; @@ -30,27 +29,38 @@ public function getRuleDefinition(): RuleDefinition { $metadata = 'RectorPrefix202607\Webmozart\Assert\Assert'; - new ConfiguredCodeSample( - <<<'CODE_SAMPLE' + return new RuleDefinition( + 'Demo', + [ + new ConfiguredCodeSample( + <<<'CODE_SAMPLE' assertStringContainsString('use Webmozart\Assert\Assert;', (string) $content); $this->assertStringContainsString('use RectorPrefix202607\Webmozart\Assert\Assert;', (string) $content); - $this->assertStringContainsString(Assert::class . '::allString($items);', (string) $content); + $this->assertStringContainsString('\Webmozart\Assert\Assert::allString($items);', (string) $content); $this->assertStringContainsString("'SomeVendor\ValueObject::class'", (string) $content); $this->assertStringContainsString("'new SomeVendor\ValueObject()'", (string) $content); $this->assertStringContainsString('$metadata = \'Webmozart\Assert\Assert\';', (string) $content); From 63f8d08c362a4aa3ba47675cc36126a2e305f93c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Jul 2026 18:29:09 +0700 Subject: [PATCH 2/2] fix spacing --- scoper.php | 2 +- tests/Scoper/ScoperPatchersTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scoper.php b/scoper.php index dfb26945094..2dcf08eb98f 100644 --- a/scoper.php +++ b/scoper.php @@ -119,7 +119,7 @@ static function (string $filePath, string $prefix, string $content): string { return Strings::replace( $content, - '#(public function getRuleDefinition\(\): RuleDefinition\s+\{\R)(.*?)(\R \);\R \})#s', + '#(public function getRuleDefinition\(\): RuleDefinition\s+\{\R)(.*?)(\R\s*\);\R\s*\})#s', static function (array $match) use ($prefix): string { $body = str_replace($prefix . '\\', '', $match[2]); $body = Strings::replace($body, '#^\s*namespace ' . preg_quote($prefix, '#') . ';\R\R?#m', ''); diff --git a/tests/Scoper/ScoperPatchersTest.php b/tests/Scoper/ScoperPatchersTest.php index ab7f8659a39..4fb1331a175 100644 --- a/tests/Scoper/ScoperPatchersTest.php +++ b/tests/Scoper/ScoperPatchersTest.php @@ -61,7 +61,7 @@ public function run() 'new RectorPrefix202607\SomeVendor\ValueObject()' ), ] - ); + ); } public function refactor(): void