Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Configuration/OnlyRuleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public function resolve(string $rule): string
}

if (! str_contains($rule, '\\')) {
// the shell has eaten unescaped backslashes, e.g. --only=\Rector\Some\Rule
$flattenMatching = [];
foreach ($this->rectors as $rector) {
if (str_replace('\\', '', $rector::class) === $rule) {
$flattenMatching[] = $rector::class;
}
}

$flattenMatching = array_unique($flattenMatching);
if (count($flattenMatching) === 1) {
return $flattenMatching[0];
}

$message = sprintf(
'Rule "%s" was not found.%sThe rule has no namespace. Make sure to escape the backslashes, and add quotes around the rule name: --only="My\\Rector\\Rule"',
$rule,
Expand Down
13 changes: 11 additions & 2 deletions tests/Configuration/OnlyRuleResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,23 @@ public function testResolveOkSingleQuotes(): void
}

public function testResolveMissingBackslash(): void
{
$this->assertSame(
RemoveDoubleAssignRector::class,
$this->onlyRuleResolver->resolve('RectorDeadCodeRectorAssignRemoveDoubleAssignRector'),
'The shell eats unescaped backslashes, resolve the flattened rule name anyway'
);
}

public function testResolveMissingBackslashNotFound(): void
{
$this->expectExceptionMessageIsOrContains(
'Rule "RectorDeadCodeRectorAssignRemoveDoubleAssignRector" was not found.' . PHP_EOL
'Rule "ThisRuleDoesNotExist" was not found.' . PHP_EOL
. 'The rule has no namespace. Make sure to escape the backslashes, and add quotes around the rule name: --only="My\\Rector\\Rule"'
);
$this->expectException(RectorRuleNotFoundException::class);

$this->onlyRuleResolver->resolve('RectorDeadCodeRectorAssignRemoveDoubleAssignRector');
$this->onlyRuleResolver->resolve('ThisRuleDoesNotExist');
}

public function testResolveNotFound(): void
Expand Down
Loading