From 4db5f968bbdf06316436c6aa6321d60f8d5af614 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 25 Jan 2024 00:00:12 +0100 Subject: [PATCH] Use directly (#45) --- src/Provider/RectorRecipeProvider.php | 37 ++++++----------- src/ValueObject/RectorRecipe.php | 6 +-- templates/rector-recipe.php | 57 +++++++++++++-------------- 3 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/Provider/RectorRecipeProvider.php b/src/Provider/RectorRecipeProvider.php index edf8b4c..fbbf4ae 100644 --- a/src/Provider/RectorRecipeProvider.php +++ b/src/Provider/RectorRecipeProvider.php @@ -4,44 +4,29 @@ namespace Rector\RectorGenerator\Provider; -use Rector\RectorGenerator\Exception\ConfigurationException; -use Rector\RectorGenerator\ValueObject\Option; use Rector\RectorGenerator\ValueObject\RectorRecipe; final class RectorRecipeProvider { - private RectorRecipe|null $rectorRecipe = null; + private RectorRecipe $rectorRecipe; /** * Configure in the rector-recipe.php config - * - * @param array $rectorRecipeConfiguration + * @param string[] $nodeTypes */ - public function __construct(array $rectorRecipeConfiguration = []) - { - // no configuration provided - due to autowiring - if ($rectorRecipeConfiguration === []) { - return; - } - - $this->rectorRecipe = new RectorRecipe( - $rectorRecipeConfiguration[Option::PACKAGE], - $rectorRecipeConfiguration[Option::NAME], - $rectorRecipeConfiguration[Option::NODE_TYPES], - $rectorRecipeConfiguration[Option::DESCRIPTION], - $rectorRecipeConfiguration[Option::CODE_BEFORE], - $rectorRecipeConfiguration[Option::CODE_AFTER], - ); + public function __construct( + string $package, + string $name, + array $nodeTypes, + string $description, + string $codeBefore, + string $codeAfter, + ) { + $this->rectorRecipe = new RectorRecipe($package, $name, $nodeTypes, $description, $codeBefore, $codeAfter); } public function provide(): RectorRecipe { - if (! $this->rectorRecipe instanceof RectorRecipe) { - throw new ConfigurationException( - 'Make sure the "rector-recipe.php" config file is imported and parameter set. Are you sure its in your main config?' - ); - } - return $this->rectorRecipe; } } diff --git a/src/ValueObject/RectorRecipe.php b/src/ValueObject/RectorRecipe.php index 014efae..663c408 100644 --- a/src/ValueObject/RectorRecipe.php +++ b/src/ValueObject/RectorRecipe.php @@ -30,7 +30,7 @@ final class RectorRecipe private ?string $category = null; /** - * @var class-string[] + * @var string[] */ private array $nodeTypes = []; @@ -40,7 +40,7 @@ final class RectorRecipe private string $package = self::PACKAGE_UTILS; /** - * @param array> $nodeTypes + * @param string[] $nodeTypes */ public function __construct( string $package, @@ -150,7 +150,7 @@ private function setName(string $name): void } /** - * @param class-string[] $nodeTypes + * @param string[] $nodeTypes */ private function setNodeTypes(array $nodeTypes): void { diff --git a/templates/rector-recipe.php b/templates/rector-recipe.php index 386a0b3..e6daee0 100644 --- a/templates/rector-recipe.php +++ b/templates/rector-recipe.php @@ -3,31 +3,12 @@ declare(strict_types=1); use PhpParser\Node\Expr\MethodCall; +use Rector\Config\RectorConfig; use Rector\RectorGenerator\Provider\RectorRecipeProvider; -use Rector\RectorGenerator\ValueObject\Option; -// run "bin/rector generate" to a new Rector basic schema + tests from this config -return static function (\Rector\Config\RectorConfig $rectorConfig): void { - // [REQUIRED] - - $rectorRecipeConfiguration = [ - // [RECTOR CORE CONTRIBUTION - REQUIRED] - // package name, basically namespace part in `rules//src`, use PascalCase - Option::PACKAGE => 'Naming', - - // name, basically short class name; use PascalCase - Option::NAME => 'RenameMethodCallRector', - - // 1+ node types to change, pick from classes here https://github.com/nikic/PHP-Parser/tree/master/lib/PhpParser/Node - // the best practise is to have just 1 type here if possible, and make separated rule for other node types - Option::NODE_TYPES => [MethodCall::class], - - // describe what the rule does - Option::DESCRIPTION => '"something()" will be renamed to "somethingElse()"', - - // code before change - // this is used for documentation and first test fixture - Option::CODE_BEFORE => <<<'CODE_SAMPLE' +return static function (RectorConfig $rectorConfig): void { + // code before change - used for docs and a test fixture + $codeBefore = <<<'CODE_SAMPLE' class SomeClass { public function run() @@ -36,9 +17,10 @@ public function run() } } CODE_SAMPLE - , - // code after change - Option::CODE_AFTER => <<<'CODE_SAMPLE' + ; + + // code after change + $codeAfter = <<<'CODE_SAMPLE' class SomeClass { public function run() @@ -47,12 +29,27 @@ public function run() } } CODE_SAMPLE - , - ]; + ; $rectorConfig->singleton(RectorRecipeProvider::class, function () use ( - $rectorRecipeConfiguration + $codeBefore, + $codeAfter ): RectorRecipeProvider { - return new RectorRecipeProvider($rectorRecipeConfiguration); + return new RectorRecipeProvider( + // package name, basically namespace part in `rules//src`, use PascalCase + package: 'Naming', + + // name, basically short class name; use PascalCase + name: 'RenameMethodCallRector', + + // 1+ node types to change, pick from classes here https://github.com/nikic/PHP-Parser/tree/master/lib/PhpParser/Node + // the best practise is to have just 1 type here if possible, and make separated rule for other node types + nodeTypes: [MethodCall::class], + + // describe what the rule does + description: '"something()" will be renamed to "somethingElse()"', + codeBefore: $codeBefore, + codeAfter: $codeAfter, + ); }); };