rector/rector-recipe.php.dist

83 lines
2.5 KiB
Plaintext
Raw Normal View History

2020-07-18 18:26:57 +00:00
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\MethodCall;
use Rector\RectorGenerator\Provider\RectorRecipeProvider;
use Rector\RectorGenerator\ValueObject\RectorRecipe;
2020-07-18 18:26:57 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Rector\SymfonyPhpConfig\inline_single_object;
2020-07-18 18:26:57 +00:00
// run "bin/rector generate" to a new Rector basic schema + tests from this config
2020-07-18 18:26:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
// [REQUIRED]
2020-07-18 18:26:57 +00:00
$rectorRecipe = new RectorRecipe(
// name, basically short class name; use PascalCase
'RenameMethodCallRector',
// particular node types to change
// the best practise is to have just 1 type here if possible, and make separated rule for other node types
[MethodCall::class],
// describe what the rule does
'"something()" will be renamed to "somethingElse()"',
// code before change
// this is used for documentation and first test fixture
<<<'CODE_SAMPLE'
2020-07-18 18:26:57 +00:00
<?php
class SomeClass
2020-07-18 18:26:57 +00:00
{
2020-08-07 09:35:11 +00:00
public function run()
2020-07-18 18:26:57 +00:00
{
$this->something();
2020-07-18 18:26:57 +00:00
}
}
CODE_SAMPLE
// code after change
, <<<'CODE_SAMPLE'
2020-07-18 18:26:57 +00:00
<?php
class SomeClass
2020-07-18 18:26:57 +00:00
{
public function run()
{
$this->somethingElse();
}
2020-07-18 18:26:57 +00:00
}
CODE_SAMPLE
);
2020-08-07 09:35:11 +00:00
// [OPTIONAL - UNCOMMENT TO USE]
2020-08-07 09:35:11 +00:00
// links to useful websites, that explain why the change is needed
// $rectorRecipe->setResources([
// 'https://github.com/symfony/symfony/blob/704c648ba53be38ef2b0105c97c6497744fef8d8/UPGRADE-6.0.md'
// ]);
// do you need to check for extra generated file?
// $rectorRecipe->addExtraFile('SomeExtraFile.php', '<?php ... SomeExtraFileContent');
// 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/<package>/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);
$services = $containerConfigurator->services();
$services->set(RectorRecipeProvider::class)
->arg('$rectorRecipe', inline_single_object($rectorRecipe, $services));
2020-07-18 18:26:57 +00:00
};