Skip to content

Commit

Permalink
Use directly (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jan 24, 2024
1 parent f9868c6 commit 4db5f96
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 59 deletions.
37 changes: 11 additions & 26 deletions src/Provider/RectorRecipeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option::*, mixed> $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;
}
}
6 changes: 3 additions & 3 deletions src/ValueObject/RectorRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class RectorRecipe
private ?string $category = null;

/**
* @var class-string[]
* @var string[]
*/
private array $nodeTypes = [];

Expand All @@ -40,7 +40,7 @@ final class RectorRecipe
private string $package = self::PACKAGE_UTILS;

/**
* @param array<class-string<Node>> $nodeTypes
* @param string[] $nodeTypes
*/
public function __construct(
string $package,
Expand Down Expand Up @@ -150,7 +150,7 @@ private function setName(string $name): void
}

/**
* @param class-string[] $nodeTypes
* @param string[] $nodeTypes
*/
private function setNodeTypes(array $nodeTypes): void
{
Expand Down
57 changes: 27 additions & 30 deletions templates/rector-recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/<package>/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()
Expand All @@ -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()
Expand All @@ -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/<package>/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,
);
});
};

0 comments on commit 4db5f96

Please sign in to comment.