-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Add oustide any set to help detecting rules that could be part o…
…f some set (#3853)
- Loading branch information
1 parent
7f49261
commit c24338f
Showing
6 changed files
with
140 additions
and
43 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
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Utils\Command; | ||
|
||
use Rector\Core\Contract\Rector\RectorInterface; | ||
use Rector\Set\ValueObject\SetList; | ||
use Rector\Utils\Finder\RectorClassFinder; | ||
use Rector\Utils\Finder\SetRectorClassesResolver; | ||
use ReflectionClass; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
final class OutsideAnySetCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly SymfonyStyle $symfonyStyle | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('outside-any-set'); | ||
$this->setDescription('[DEV] Show rules outside any set'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$setDefinedRectorClasses = $this->resolveSetDefinedRectorClasses(); | ||
$existingRectorClasses = RectorClassFinder::find([__DIR__ . '/../../rules']); | ||
|
||
$rectorClassesOutsideAnySet = array_diff($existingRectorClasses, $setDefinedRectorClasses); | ||
|
||
sort($rectorClassesOutsideAnySet); | ||
|
||
$this->symfonyStyle->listing($rectorClassesOutsideAnySet); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
/** | ||
* @return array<class-string<RectorInterface>> | ||
*/ | ||
private function resolveSetDefinedRectorClasses(): array | ||
{ | ||
$setListReflectionClass = new ReflectionClass(SetList::class); | ||
|
||
$setDefinedRectorClasses = []; | ||
|
||
foreach ($setListReflectionClass->getConstants() as $constantValue) { | ||
$currentSetDefinedRectorClasses = SetRectorClassesResolver::resolve($constantValue); | ||
$setDefinedRectorClasses = array_merge($setDefinedRectorClasses, $currentSetDefinedRectorClasses); | ||
} | ||
|
||
return $setDefinedRectorClasses; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Utils\Finder; | ||
|
||
use Nette\Loaders\RobotLoader; | ||
use Rector\Core\Contract\Rector\RectorInterface; | ||
|
||
final class RectorClassFinder | ||
{ | ||
/** | ||
* @param string[] $directories | ||
* @return array<class-string<RectorInterface>> | ||
*/ | ||
public static function find(array $directories): array | ||
{ | ||
$robotLoader = new RobotLoader(); | ||
$robotLoader->setTempDirectory(sys_get_temp_dir() . '/rector-missing-in-set'); | ||
$robotLoader->addDirectory(...$directories); | ||
|
||
$robotLoader->acceptFiles = ['*Rector.php']; | ||
$robotLoader->rebuild(); | ||
|
||
$filePathsToRectorClasses = array_keys($robotLoader->getIndexedClasses()); | ||
|
||
return array_filter( | ||
$filePathsToRectorClasses, | ||
static fn (string $rectorClass): bool => is_a($rectorClass, RectorInterface::class, 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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Utils\Finder; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Strings; | ||
use Rector\Core\Contract\Rector\RectorInterface; | ||
|
||
final class SetRectorClassesResolver | ||
{ | ||
/** | ||
* @see https://regex101.com/r/HtsmKC/1 | ||
* @var string | ||
*/ | ||
private const RECTOR_CLASS_REGEX = '#use (?<class_name>[\\\\\w]+Rector)#m'; | ||
|
||
/** | ||
* @return array<class-string<RectorInterface>> | ||
*/ | ||
public static function resolve(string $setFile): array | ||
{ | ||
$rectorClasses = []; | ||
|
||
$setFileContents = FileSystem::read($setFile); | ||
$matches = Strings::matchAll($setFileContents, self::RECTOR_CLASS_REGEX); | ||
|
||
foreach ($matches as $match) { | ||
$rectorClassName = $match['class_name']; | ||
if ($rectorClassName === 'Rector\Config\Rector') { | ||
continue; | ||
} | ||
|
||
$rectorClasses[] = $rectorClassName; | ||
} | ||
|
||
return $rectorClasses; | ||
} | ||
} |