-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "--only" option to process only a single rule
The option for the "process" and "list-rules" commands applies the single given rule only, without needing to modify the configuration file. The option value must be a fully classified class name: --only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector" A hint is given when the user forgot to escape the backslashes. ---- It is impossible to modify the injected "$rectors" after the command line configuration is parsed, so I had to introduce the ConfigurationRuleFilter singleton. Since both ListRulesCommand and ProcessCommand make use of the ConfigurationRuleFilter - but list-rules does not have a Configuration - I had to make the filterOnlyRule() method public to prevent code duplication. Resolves rectorphp/rector#8899
- Loading branch information
Showing
15 changed files
with
266 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Configuration; | ||
|
||
use Rector\Contract\Rector\RectorInterface; | ||
use Rector\ValueObject\Configuration; | ||
|
||
/** | ||
* Modify available rector rules based on the configuration options | ||
*/ | ||
final class ConfigurationRuleFilter | ||
{ | ||
protected ?Configuration $configuration = null; | ||
|
||
public function setConfiguration(Configuration $configuration): void | ||
{ | ||
$this->configuration = $configuration; | ||
} | ||
|
||
/** | ||
* @param array<RectorInterface> $rectors | ||
* @return array<RectorInterface> | ||
*/ | ||
public function filter(array $rectors): array | ||
{ | ||
if ($this->configuration === null) { | ||
return $rectors; | ||
} | ||
|
||
$onlyRule = $this->configuration->getOnlyRule(); | ||
if ($onlyRule !== null) { | ||
$rectors = $this->filterOnlyRule($rectors, $onlyRule); | ||
return $rectors; | ||
} | ||
|
||
return $rectors; | ||
} | ||
|
||
/** | ||
* @param array<RectorInterface> $rectors | ||
* @return array<RectorInterface> | ||
*/ | ||
public function filterOnlyRule(array $rectors, string $onlyRule): array | ||
{ | ||
$activeRectors = []; | ||
foreach ($rectors as $rector) { | ||
if (is_a($rector, $onlyRule)) { | ||
$activeRectors[] = $rector; | ||
} | ||
} | ||
|
||
return $activeRectors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Configuration; | ||
|
||
use Rector\Contract\Rector\RectorInterface; | ||
use Rector\Exception\Configuration\RectorRuleNotFoundException; | ||
|
||
/** | ||
* @see \Rector\Tests\Configuration\OnlyRuleResolverTest | ||
*/ | ||
final class OnlyRuleResolver | ||
{ | ||
/** | ||
* @param RectorInterface[] $rectors | ||
*/ | ||
public function __construct( | ||
private readonly array $rectors | ||
) { | ||
} | ||
|
||
public function resolve(string $rule): string | ||
{ | ||
$rule = ltrim($rule, '\\'); | ||
|
||
foreach ($this->rectors as $rector) { | ||
if (is_a($rector, $rule, true)) { | ||
return $rule; | ||
} | ||
} | ||
|
||
if (strpos($rule, '\\') === false) { | ||
$message = sprintf( | ||
'Rule "%s" was not found.%sThe rule has no namespace - make sure to escape the backslashes correctly.', | ||
$rule, | ||
PHP_EOL | ||
); | ||
} else { | ||
$message = sprintf( | ||
'Rule "%s" was not found.%sMake sure it is registered in your config or in one of the sets', | ||
$rule, | ||
PHP_EOL | ||
); | ||
} | ||
throw new RectorRuleNotFoundException($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Exception/Configuration/RectorRuleNotFoundException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Exception\Configuration; | ||
|
||
use Exception; | ||
|
||
final class RectorRuleNotFoundException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.