From 920491359353bb052e98113a98418dec44e9a2a5 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 26 Jan 2024 21:43:08 +0000 Subject: [PATCH] Updated Rector to commit a69e1ff013720727f052437d051ca5ab2c11b89e https://github.com/rectorphp/rector-src/commit/a69e1ff013720727f052437d051ca5ab2c11b89e Add PHPUnit 9 annotations support detectoin in "custom-rule" command, add RectorConfig::withPreparedSets() (#5506) --- src/Application/VersionResolver.php | 4 +- src/Configuration/RectorConfigBuilder.php | 32 +++++++++++++++ src/Console/Command/CustomRuleCommand.php | 41 +++++++++++++++++-- .../utils/rector/src/Rector/__Name__.php | 2 +- .../tests/Rector/__Name__/__Name__Test.php | 28 +++++++++++++ templates/rector.php.dist | 19 ++++----- 6 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 templates/custom-rules-annotations/utils/rector/tests/Rector/__Name__/__Name__Test.php diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 1a1e2f2f618..bfa8736f5a0 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '701ab22b8681518f21e371c0b070f8f51a45b539'; + public const PACKAGE_VERSION = 'a69e1ff013720727f052437d051ca5ab2c11b89e'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-01-26 20:48:37'; + public const RELEASE_DATE = '2024-01-26 21:40:53'; /** * @var int */ diff --git a/src/Configuration/RectorConfigBuilder.php b/src/Configuration/RectorConfigBuilder.php index 4fecc836bc8..63267395581 100644 --- a/src/Configuration/RectorConfigBuilder.php +++ b/src/Configuration/RectorConfigBuilder.php @@ -6,6 +6,7 @@ namespace Rector\Configuration; use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface; use Rector\Config\RectorConfig; use Rector\Contract\Rector\RectorInterface; +use Rector\Set\ValueObject\SetList; use Rector\ValueObject\PhpVersion; use RectorPrefix202401\Symfony\Component\Finder\Finder; /** @@ -212,6 +213,37 @@ final class RectorConfigBuilder $this->sets = $sets; return $this; } + public function withPreparedSets(bool $deadCode = \false, bool $codeQuality = \false, bool $codingStyle = \false, bool $typeDeclarations = \false, bool $privatization = \false, bool $naming = \false, bool $instanceOf = \false, bool $earlyReturn = \false, bool $strictBooleans = \false) : self + { + if ($deadCode) { + $this->sets[] = SetList::DEAD_CODE; + } + if ($codeQuality) { + $this->sets[] = SetList::CODE_QUALITY; + } + if ($codingStyle) { + $this->sets[] = SetList::CODING_STYLE; + } + if ($typeDeclarations) { + $this->sets[] = SetList::TYPE_DECLARATION; + } + if ($privatization) { + $this->sets[] = SetList::PRIVATIZATION; + } + if ($naming) { + $this->sets[] = SetList::NAMING; + } + if ($instanceOf) { + $this->sets[] = SetList::INSTANCEOF; + } + if ($earlyReturn) { + $this->sets[] = SetList::EARLY_RETURN; + } + if ($strictBooleans) { + $this->sets[] = SetList::STRICT_BOOLEANS; + } + return $this; + } /** * @param array> $rules */ diff --git a/src/Console/Command/CustomRuleCommand.php b/src/Console/Command/CustomRuleCommand.php index 353775f14c8..3f47ab778e0 100644 --- a/src/Console/Command/CustomRuleCommand.php +++ b/src/Console/Command/CustomRuleCommand.php @@ -4,6 +4,7 @@ declare (strict_types=1); namespace Rector\Console\Command; use RectorPrefix202401\Nette\Utils\FileSystem; +use RectorPrefix202401\Nette\Utils\Strings; use Rector\Exception\ShouldNotHappenException; use Rector\FileSystem\JsonFileSystem; use RectorPrefix202401\Symfony\Component\Console\Command\Command; @@ -11,6 +12,7 @@ use RectorPrefix202401\Symfony\Component\Console\Input\InputInterface; use RectorPrefix202401\Symfony\Component\Console\Output\OutputInterface; use RectorPrefix202401\Symfony\Component\Console\Style\SymfonyStyle; use RectorPrefix202401\Symfony\Component\Finder\Finder; +use RectorPrefix202401\Symfony\Component\Finder\SplFileInfo; final class CustomRuleCommand extends Command { /** @@ -18,6 +20,11 @@ final class CustomRuleCommand extends Command * @var \Symfony\Component\Console\Style\SymfonyStyle */ private $symfonyStyle; + /** + * @see https://regex101.com/r/2eP4rw/1 + * @var string + */ + private const START_WITH_10_REGEX = '#(\\^10\\.|>=10\\.|10\\.)#'; public function __construct(SymfonyStyle $symfonyStyle) { $this->symfonyStyle = $symfonyStyle; @@ -43,12 +50,22 @@ final class CustomRuleCommand extends Command } $rectorName = \ucfirst((string) $rectorName); // find all files in templates directory - $fileInfos = Finder::create()->files()->in(__DIR__ . '/../../../templates/custom-rule')->getIterator(); + $finder = Finder::create()->files()->in(__DIR__ . '/../../../templates/custom-rule')->notName('__Name__Test.php'); + // 0. resolve if local phpunit is at least PHPUnit 10 (which supports #[DataProvider]) + // to provide annotation if not + $arePHPUnitAttributesSupported = $this->detectPHPUnitAttributeSupport(); + if ($arePHPUnitAttributesSupported) { + $finder->append([new SplFileInfo(__DIR__ . '/../../../templates/custom-rule/utils/rector/tests/Rector/__Name__/__Name__Test.php', 'utils/rector/tests/Rector/__Name__', 'utils/rector/tests/Rector/__Name__/__Name__Test.php')]); + } else { + // use @annotations for PHPUnit 9 and bellow + $finder->append([new SplFileInfo(__DIR__ . '/../../../templates/custom-rules-annotations/utils/rector/tests/Rector/__Name__/__Name__Test.php', 'utils/rector/tests/Rector/__Name__', 'utils/rector/tests/Rector/__Name__/__Name__Test.php')]); + } $generatedFilePaths = []; + $fileInfos = \iterator_to_array($finder->getIterator()); foreach ($fileInfos as $fileInfo) { // replace __Name__ with $rectorName - $newContent = \str_replace('__Name__', $rectorName, $fileInfo->getContents()); - $newFilePath = \str_replace('__Name__', $rectorName, $fileInfo->getRelativePathname()); + $newContent = $this->replaceNameVariable($rectorName, $fileInfo->getContents()); + $newFilePath = $this->replaceNameVariable($rectorName, $fileInfo->getRelativePathname()); FileSystem::write(\getcwd() . '/' . $newFilePath, $newContent); $generatedFilePaths[] = $newFilePath; } @@ -72,4 +89,22 @@ final class CustomRuleCommand extends Command } return Command::SUCCESS; } + private function replaceNameVariable(string $rectorName, string $contents) : string + { + return \str_replace('__Name__', $rectorName, $contents); + } + private function detectPHPUnitAttributeSupport() : bool + { + $composerJsonFilePath = \getcwd() . '/composer.json'; + if (!\file_exists($composerJsonFilePath)) { + // be safe + return \false; + } + $composerJson = JsonFileSystem::readFilePath($composerJsonFilePath); + $phpunitVersion = $composerJson['require-dev']['phpunit/phpunit'] ?? null; + if ($phpunitVersion === null) { + return \false; + } + return (bool) Strings::match($phpunitVersion, self::START_WITH_10_REGEX); + } } diff --git a/templates/custom-rule/utils/rector/src/Rector/__Name__.php b/templates/custom-rule/utils/rector/src/Rector/__Name__.php index 5aafd3ba085..bb209149a54 100644 --- a/templates/custom-rule/utils/rector/src/Rector/__Name__.php +++ b/templates/custom-rule/utils/rector/src/Rector/__Name__.php @@ -10,7 +10,7 @@ use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Utils\Rector\Tests\Rector\__Name__\__Name__Test + * @see \rector\tests\Rector\__Name__\__Name__Test */ final class __Name__ extends AbstractRector { diff --git a/templates/custom-rules-annotations/utils/rector/tests/Rector/__Name__/__Name__Test.php b/templates/custom-rules-annotations/utils/rector/tests/Rector/__Name__/__Name__Test.php new file mode 100644 index 00000000000..3e252b5f7cf --- /dev/null +++ b/templates/custom-rules-annotations/utils/rector/tests/Rector/__Name__/__Name__Test.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/templates/rector.php.dist b/templates/rector.php.dist index 9dcfa922c2a..ae09f219555 100644 --- a/templates/rector.php.dist +++ b/templates/rector.php.dist @@ -6,16 +6,15 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; use Rector\Set\ValueObject\LevelSetList; -return static function (RectorConfig $rectorConfig): void { - $rectorConfig->paths([ +return RectorConfig::configure() + ->withPaths([ __PATHS__ + ]) + ->withRules([ + InlineConstructorDefaultToPropertyRector::class, + ]) + ->withSets([ + // define sets of rules + // LevelSetList::UP_TO_PHP_XY ]); - - // register a single rule - $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); - - // define sets of rules - // $rectorConfig->sets([ - // LevelSetList::UP_TO_PHP_XY - // ]); };