Updated Rector to commit a69e1ff013720727f052437d051ca5ab2c11b89e

a69e1ff013 Add PHPUnit 9 annotations support detectoin in "custom-rule" command, add RectorConfig::withPreparedSets() (#5506)
This commit is contained in:
Tomas Votruba 2024-01-26 21:43:08 +00:00
parent 7a20211779
commit 9204913593
6 changed files with 110 additions and 16 deletions

View File

@ -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
*/

View File

@ -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<class-string<RectorInterface>> $rules
*/

View File

@ -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);
}
}

View File

@ -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
{

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Utils\Rector\Tests\Rector\__Name__;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class __Name__Test extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -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
// ]);
};