Skip to content

Commit 1768c41

Browse files
committed
update regex
1 parent fb0e828 commit 1768c41

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎scoper.php‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,21 @@ static function (string $filePath, string $prefix, string $content): string {
119119

120120
return Strings::replace(
121121
$content,
122-
"#(<<<'?(?<label>CODE_SAMPLE|SAMPLE)'?\\R)(.*?)(\\R\\s*\\k<label>)#s",
123-
static function (array $match) use ($prefix): string {
124-
$body = str_replace($prefix . '\\', '', $match[3]);
125-
$body = Strings::replace($body, '#^[ \t]*namespace ' . preg_quote($prefix, '#') . ';\R\R?#m', '');
126-
$body = Strings::replace($body, '#^[ \t]*\\\\class_alias\(.*?\);\R?#m', '');
127-
128-
return $match[1] . $body . $match[4];
122+
'#(public function getRuleDefinition\(\): RuleDefinition\s+\{\R)(.*?)(\R\s*\);\R\s*\})#s',
123+
static function (array $ruleDefinitionMatch) use ($prefix): string {
124+
$ruleDefinitionContent = Strings::replace(
125+
$ruleDefinitionMatch[2],
126+
"#(<<<'?(?<label>CODE_SAMPLE|SAMPLE)'?\\R)(.*?)(\\R\\s*\\k<label>)#s",
127+
static function (array $codeSampleMatch) use ($prefix): string {
128+
$body = str_replace($prefix . '\\', '', $codeSampleMatch[3]);
129+
$body = Strings::replace($body, '#^[ \t]*namespace ' . preg_quote($prefix, '#') . ';\R\R?#m', '');
130+
$body = Strings::replace($body, '#^[ \t]*\\\\class_alias\(.*?\);\R?#m', '');
131+
132+
return $codeSampleMatch[1] . $body . $codeSampleMatch[4];
133+
}
134+
);
135+
136+
return $ruleDefinitionMatch[1] . $ruleDefinitionContent . $ruleDefinitionMatch[3];
129137
}
130138
);
131139
},

‎tests/Scoper/ScoperPatchersTest.php‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public function run()
7676
7777
public function refactor(): void
7878
{
79+
$sample = <<<'CODE_SAMPLE'
80+
<?php
81+
82+
namespace RectorPrefix202607;
83+
84+
\RectorPrefix202607\ShouldStayPrefixed::run();
85+
\class_alias('SomeClass', 'SomeClass', \false);
86+
CODE_SAMPLE;
87+
7988
\RectorPrefix202607\SomeVendor\Runtime::class;
8089
}
8190
}
@@ -89,7 +98,14 @@ public function refactor(): void
8998
);
9099
}
91100

92-
preg_match_all("#<<<'CODE_SAMPLE'\R(.*?)\RCODE_SAMPLE#s", (string) $content, $matches);
101+
preg_match(
102+
'#public function getRuleDefinition\(\): RuleDefinition\s+\{\R(.*?)\R \}\R\R public function refactor\(\): void#s',
103+
(string) $content,
104+
$getRuleDefinitionMatches
105+
);
106+
$this->assertNotSame([], $getRuleDefinitionMatches);
107+
108+
preg_match_all("#<<<'CODE_SAMPLE'\R(.*?)\RCODE_SAMPLE#s", $getRuleDefinitionMatches[1], $matches);
93109
$codeSampleContent = implode("\n", $matches[1]);
94110

95111
$this->assertStringContainsString('use RectorPrefix202607\Webmozart\Assert\Assert;', (string) $content);
@@ -101,6 +117,7 @@ public function refactor(): void
101117
$this->assertStringNotContainsString('namespace RectorPrefix202607;', $codeSampleContent);
102118
$this->assertStringNotContainsString('use RectorPrefix202607\Webmozart\Assert\Assert;', $codeSampleContent);
103119
$this->assertStringNotContainsString('\class_alias', $codeSampleContent);
120+
$this->assertStringContainsString('\RectorPrefix202607\ShouldStayPrefixed::run();', (string) $content);
104121
}
105122

106123
public function testRemovesPrefixedNamespaceInGetRuleDefinitionWithWindowsLineEndings(): void

0 commit comments

Comments
 (0)