Skip to content

Commit 3305752

Browse files
Fix scoper to clean up prefix under getRuleDefinition() method (#8165)
* Fix scoper to clean up prefix under getRuleDefinition() method * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 3d89807 commit 3305752

2 files changed

Lines changed: 153 additions & 0 deletions

File tree

‎scoper.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ static function (string $filePath, string $prefix, string $content): string {
107107
);
108108
},
109109

110+
// keep public documentation examples readable
111+
static function (string $filePath, string $prefix, string $content): string {
112+
if (! \str_contains($filePath, 'rules/')) {
113+
return $content;
114+
}
115+
116+
if (! \str_ends_with($filePath, 'Rector.php')) {
117+
return $content;
118+
}
119+
120+
return Strings::replace(
121+
$content,
122+
'#(public function getRuleDefinition\(\): RuleDefinition\s+\{\R)(.*?)(\R \})#s',
123+
static function (array $match) use ($prefix): string {
124+
$body = str_replace($prefix . '\\', '', $match[2]);
125+
$body = Strings::replace($body, '#^\s*namespace ' . preg_quote($prefix, '#') . ';\R\R?#m', '');
126+
127+
return $match[1] . $body . $match[3];
128+
}
129+
);
130+
},
131+
110132
// un-prefix composer plugin
111133
static function (string $filePath, string $prefix, string $content): string {
112134
if (! \str_ends_with($filePath, 'vendor/rector/extension-installer/src/Plugin.php')) {
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Scoper;
6+
7+
use Webmozart\Assert\Assert;
8+
use Closure;
9+
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Finder\Finder;
11+
12+
final class ScoperPatchersTest extends TestCase
13+
{
14+
private static bool $isFinderAliased = false;
15+
16+
public function testKeepsPrefixedClassesInGetRuleDefinitionUnprefixed(): void
17+
{
18+
$scoperConfig = $this->provideScoperConfig();
19+
20+
$content = <<<'PHP'
21+
<?php
22+
23+
namespace Rector\Assert\Rector\ClassMethod;
24+
25+
use RectorPrefix202607\Webmozart\Assert\Assert;
26+
27+
final class AddAssertArrayFromClassMethodDocblockRector
28+
{
29+
public function getRuleDefinition(): RuleDefinition
30+
{
31+
$metadata = 'RectorPrefix202607\Webmozart\Assert\Assert';
32+
33+
new ConfiguredCodeSample(
34+
<<<'CODE_SAMPLE'
35+
<?php
36+
37+
namespace RectorPrefix202607;
38+
39+
use RectorPrefix202607\Webmozart\Assert\Assert;
40+
CODE_SAMPLE
41+
,
42+
<<<'CODE_SAMPLE'
43+
<?php
44+
45+
\RectorPrefix202607\Webmozart\Assert\Assert::allString($items);
46+
CODE_SAMPLE
47+
,
48+
[]
49+
);
50+
51+
new CodeSample(
52+
'RectorPrefix202607\SomeVendor\ValueObject::class',
53+
'new RectorPrefix202607\SomeVendor\ValueObject()'
54+
);
55+
}
56+
57+
public function refactor(): void
58+
{
59+
\RectorPrefix202607\SomeVendor\Runtime::class;
60+
}
61+
}
62+
PHP;
63+
64+
foreach ($scoperConfig['patchers'] as $patcher) {
65+
$content = $patcher->__invoke(
66+
__DIR__ . '/../../rules/Assert/Rector/ClassMethod/AddAssertArrayFromClassMethodDocblockRector.php',
67+
'RectorPrefix202607',
68+
$content
69+
);
70+
}
71+
72+
$this->assertStringContainsString('use Webmozart\Assert\Assert;', (string) $content);
73+
$this->assertStringContainsString('use RectorPrefix202607\Webmozart\Assert\Assert;', (string) $content);
74+
$this->assertStringContainsString(Assert::class . '::allString($items);', (string) $content);
75+
$this->assertStringContainsString("'SomeVendor\ValueObject::class'", (string) $content);
76+
$this->assertStringContainsString("'new SomeVendor\ValueObject()'", (string) $content);
77+
$this->assertStringContainsString('$metadata = \'Webmozart\Assert\Assert\';', (string) $content);
78+
$this->assertStringContainsString('\RectorPrefix202607\SomeVendor\Runtime::class;', (string) $content);
79+
$this->assertStringNotContainsString('namespace RectorPrefix202607;', (string) $content);
80+
}
81+
82+
public function testRemovesPrefixedNamespaceInGetRuleDefinitionWithWindowsLineEndings(): void
83+
{
84+
$scoperConfig = $this->provideScoperConfig();
85+
86+
$content = str_replace(
87+
"\n",
88+
"\r\n",
89+
<<<'PHP'
90+
<?php
91+
92+
final class SomeRector
93+
{
94+
public function getRuleDefinition(): RuleDefinition
95+
{
96+
return new CodeSample(
97+
'<?php
98+
namespace RectorPrefix202607;
99+
100+
RectorPrefix202607\SomeVendor\ValueObject::class;'
101+
);
102+
}
103+
}
104+
PHP
105+
);
106+
107+
foreach ($scoperConfig['patchers'] as $patcher) {
108+
$content = $patcher->__invoke(
109+
__DIR__ . '/../../rules/Some/Rector/SomeRector.php',
110+
'RectorPrefix202607',
111+
$content
112+
);
113+
}
114+
115+
$this->assertStringNotContainsString('namespace RectorPrefix202607;', (string) $content);
116+
$this->assertStringContainsString('SomeVendor\ValueObject::class;', (string) $content);
117+
}
118+
119+
/**
120+
* @return array{patchers: list<Closure(string, string, string): string>}
121+
*/
122+
private function provideScoperConfig(): array
123+
{
124+
if (! self::$isFinderAliased) {
125+
class_alias(Finder::class, 'Isolated\Symfony\Component\Finder\Finder');
126+
self::$isFinderAliased = true;
127+
}
128+
129+
return require __DIR__ . '/../../scoper.php';
130+
}
131+
}

0 commit comments

Comments
 (0)