From a8a0030bf923152261154f3f56a9fbbc790b4dda Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 14 Dec 2020 17:24:33 +0100 Subject: [PATCH] Move package to 1st position in recipe (#4882) * add alias * [recipe] enable set by default, most contributors are core * move package to 1st place, close to rule name where we think about package * [ci-review] Rector Rectify * fix recipe test Co-authored-by: rector-bot --- .../src/Command/InitRecipeCommand.php | 1 + .../src/ValueObject/RectorRecipe.php | 37 ++++---- .../Source/StaticRectorRecipeFactory.php | 1 + ...owngradeTrailingCommasInParamUseRector.php | 2 +- ...xtractorEnableMagicCallExtractorRector.php | 94 ++++++++++++------- templates/rector-recipe.php.dist | 10 +- 6 files changed, 85 insertions(+), 60 deletions(-) diff --git a/packages/rector-generator/src/Command/InitRecipeCommand.php b/packages/rector-generator/src/Command/InitRecipeCommand.php index 0fdc7dbd533..c92773a396d 100644 --- a/packages/rector-generator/src/Command/InitRecipeCommand.php +++ b/packages/rector-generator/src/Command/InitRecipeCommand.php @@ -27,6 +27,7 @@ final class InitRecipeCommand extends Command protected function configure(): void { $this->setDescription('[DEV] Initialize "rector-recipe.php" config'); + $this->setAliases(['recipe-init']); } protected function execute(InputInterface $input, OutputInterface $output): int diff --git a/packages/rector-generator/src/ValueObject/RectorRecipe.php b/packages/rector-generator/src/ValueObject/RectorRecipe.php index 38b1d8024b3..bf09f11174d 100644 --- a/packages/rector-generator/src/ValueObject/RectorRecipe.php +++ b/packages/rector-generator/src/ValueObject/RectorRecipe.php @@ -88,6 +88,7 @@ final class RectorRecipe * @param class-string[] $nodeTypes */ public function __construct( + string $package, string $name, array $nodeTypes, string $description, @@ -96,6 +97,7 @@ final class RectorRecipe ) { $this->isRectorRepository = file_exists(__DIR__ . '/../../../../vendor'); + $this->setPackage($package); $this->setName($name); $this->setNodeTypes($nodeTypes); @@ -204,23 +206,6 @@ final class RectorRecipe return StaticRectorStrings::camelCaseToDashes($this->getPackage()); } - /** - * @api - */ - public function setPackage(string $package): void - { - if (is_file($package)) { - $message = sprintf( - 'The "%s()" method only accepts package name, file path "%s" given', - __METHOD__, - $package - ); - throw new ShouldNotHappenException($message); - } - - $this->package = $package; - } - /** * @api */ @@ -257,6 +242,24 @@ final class RectorRecipe $this->isRectorRepository = $isRectorRepository; } + /** + * For tests + * @api + */ + public function setPackage(string $package): void + { + if (is_file($package)) { + $message = sprintf( + 'The "%s()" method only accepts package name, file path "%s" given', + __METHOD__, + $package + ); + throw new ShouldNotHappenException($message); + } + + $this->package = $package; + } + private function setName(string $name): void { if (! Strings::endsWith($name, 'Rector')) { diff --git a/packages/rector-generator/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php b/packages/rector-generator/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php index d8f8d357a0e..1110e49f2f4 100644 --- a/packages/rector-generator/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php +++ b/packages/rector-generator/tests/RectorGenerator/Source/StaticRectorRecipeFactory.php @@ -13,6 +13,7 @@ final class StaticRectorRecipeFactory public static function createRectorRecipe(bool $isRectorRepository): RectorRecipe { $rectorRecipe = new RectorRecipe( + 'Utils', 'WhateverRector', [MethodCall::class], 'Change $service->arg(...) to $service->call(...)', diff --git a/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php b/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php index 9c30ed2bc89..8b467711389 100644 --- a/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php +++ b/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php @@ -5,13 +5,13 @@ declare(strict_types=1); namespace Rector\DowngradePhp80\Rector\ClassMethod; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\ClosureUse; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Arg; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; diff --git a/rules/symfony5/src/Rector/MethodCall/ReflectionExtractorEnableMagicCallExtractorRector.php b/rules/symfony5/src/Rector/MethodCall/ReflectionExtractorEnableMagicCallExtractorRector.php index bcef3b2755a..a2bcf5001e3 100644 --- a/rules/symfony5/src/Rector/MethodCall/ReflectionExtractorEnableMagicCallExtractorRector.php +++ b/rules/symfony5/src/Rector/MethodCall/ReflectionExtractorEnableMagicCallExtractorRector.php @@ -20,15 +20,28 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; */ final class ReflectionExtractorEnableMagicCallExtractorRector extends AbstractRector { + /** + * @var string + */ private const OLD_OPTION_NAME = 'enable_magic_call_extraction'; + + /** + * @var string + */ private const NEW_OPTION_NAME = 'enable_magic_methods_extraction'; + + /** + * @var string[] + */ private const METHODS_WITH_OPTION = ['getWriteInfo', 'getReadInfo']; public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition('Migrates from deprecated enable_magic_call_extraction context option in ReflectionExtractor', [ - new CodeSample( - <<<'PHP' + return new RuleDefinition( + 'Migrates from deprecated enable_magic_call_extraction context option in ReflectionExtractor', + [ + new CodeSample( + <<<'CODE_SAMPLE' use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; class SomeClass @@ -41,9 +54,9 @@ class SomeClass ]); } } -PHP - , - <<<'PHP' +CODE_SAMPLE + , + <<<'CODE_SAMPLE' use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; class SomeClass @@ -56,9 +69,9 @@ class SomeClass ]); } } -PHP - ), - ]); +CODE_SAMPLE + ), + ]); } /** @@ -93,21 +106,24 @@ PHP return $node; } - private function prepareEnableMagicMethodsExtractionFlags(bool $enableMagicCallExtractionValue): BitwiseOr + private function shouldSkip(MethodCall $methodCall): bool { - $magicGet = $this->createClassConstFetch('Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor', 'MAGIC_GET'); - $magicSet = $this->createClassConstFetch('Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor', 'MAGIC_SET'); - if (! $enableMagicCallExtractionValue) { - return new BitwiseOr($magicGet, $magicSet); + if (! $this->isObjectType($methodCall, 'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor')) { + return true; } - return new BitwiseOr( - new BitwiseOr( - $this->createClassConstFetch('Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor', 'MAGIC_CALL'), - $magicGet, - ), - $magicSet, - ); + if (! $this->isNames($methodCall->name, self::METHODS_WITH_OPTION)) { + return true; + } + + if (count((array) $methodCall->args) < 3) { + return true; + } + + /** @var Array_ $contextOptions */ + $contextOptions = $methodCall->args[2]->value; + + return $contextOptions->items === []; } private function getContextOptionValue(MethodCall $methodCall): ?bool @@ -137,23 +153,29 @@ PHP return $contextOptionValue; } - private function shouldSkip(MethodCall $methodCall): bool + private function prepareEnableMagicMethodsExtractionFlags(bool $enableMagicCallExtractionValue): BitwiseOr { - if (! $this->isObjectType($methodCall, 'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor')) { - return true; + $classConstFetch = $this->createClassConstFetch( + 'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor', + 'MAGIC_GET' + ); + $magicSet = $this->createClassConstFetch( + 'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor', + 'MAGIC_SET' + ); + if (! $enableMagicCallExtractionValue) { + return new BitwiseOr($classConstFetch, $magicSet); } - if (! $this->isNames($methodCall->name, self::METHODS_WITH_OPTION)) { - return true; - } - - if (count((array) $methodCall->args) < 3) { - return true; - } - - /** @var Array_ $contextOptions */ - $contextOptions = $methodCall->args[2]->value; - - return count($contextOptions->items) === 0; + return new BitwiseOr( + new BitwiseOr( + $this->createClassConstFetch( + 'Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor', + 'MAGIC_CALL' + ), + $classConstFetch, + ), + $magicSet, + ); } } diff --git a/templates/rector-recipe.php.dist b/templates/rector-recipe.php.dist index f80789ebf73..1f8ffaac761 100644 --- a/templates/rector-recipe.php.dist +++ b/templates/rector-recipe.php.dist @@ -13,6 +13,10 @@ return static function (ContainerConfigurator $containerConfigurator): void { // [REQUIRED] $rectorRecipe = new RectorRecipe( + // [RECTOR CORE CONTRIBUTION - REQUIRED] + // package name, basically namespace part in `rules//src`, use PascalCase + 'Naming', + // name, basically short class name; use PascalCase 'RenameMethodCallRector', @@ -65,12 +69,6 @@ CODE_SAMPLE // is the rule configurable? add default configuration here // $rectorRecipe->setConfiguration(['SOME_CONSTANT_KEY' => ['before' => 'after']]); - - // [RECTOR CORE CONTRIBUTION] - // [RECTOR CORE CONTRIBUTION - REQUIRED] - // package name, basically part namespace in `rule//src`, use PascalCase - // $rectorRecipe->setPackage('Generic'); - // [RECTOR CORE CONTRIBUTION - OPTIONAL] // set the rule belongs to; is optional, because e.g. generic rules don't need a specific set to belong to // $rectorRecipe->setSet(\Rector\Set\ValueObject\SetList::NAMING);