forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
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
23 changed files
with
338 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
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 @@ | ||
--only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector" |
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,7 @@ | ||
{ | ||
"require": { | ||
"php": "^8.1" | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
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,22 @@ | ||
1 file with changes | ||
=================== | ||
|
||
1) src/MultiRules.php:9 | ||
|
||
---------- begin diff ---------- | ||
@@ @@ | ||
echo 'a statement'; | ||
} | ||
} | ||
- | ||
- private function notUsed() | ||
- { | ||
- } | ||
} | ||
----------- end diff ----------- | ||
|
||
Applied rules: | ||
* RemoveUnusedPrivateMethodRector | ||
|
||
|
||
[OK] 1 file would have been changed (dry-run) by Rector |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector; | ||
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->paths([ | ||
__DIR__ . '/src', | ||
]); | ||
|
||
$rectorConfig->rules([ | ||
RemoveAlwaysElseRector::class, | ||
RemoveUnusedPrivateMethodRector::class, | ||
]); | ||
}; | ||
|
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,17 @@ | ||
<?php | ||
|
||
final class MultiRules | ||
{ | ||
public function doSomething() | ||
{ | ||
if (true === false) { | ||
return -1; | ||
} else { | ||
echo 'a statement'; | ||
} | ||
} | ||
|
||
private function notUsed() | ||
{ | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
class RemoveAlwaysElse | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
throw new \InvalidStateException; | ||
} else { | ||
return 10; | ||
} | ||
} | ||
} |
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 | ||
{ | ||
private ?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
Oops, something went wrong.