2020-07-18 18:26:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-24 21:25:26 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2020-08-15 22:49:15 +00:00
|
|
|
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;
|
2020-08-24 21:25:26 +00:00
|
|
|
use function Rector\SymfonyPhpConfig\inline_single_object;
|
2020-07-18 18:26:57 +00:00
|
|
|
|
2020-08-15 22:49:15 +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 {
|
2020-08-24 21:25:26 +00:00
|
|
|
// [REQUIRED]
|
2020-07-18 18:26:57 +00:00
|
|
|
|
2020-08-15 22:49:15 +00:00
|
|
|
$rectorRecipe = new RectorRecipe(
|
2020-08-24 21:25:26 +00:00
|
|
|
// 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
|
2020-08-15 22:49:15 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-07-18 18:26:57 +00:00
|
|
|
<?php
|
|
|
|
|
2020-08-15 22:49:15 +00:00
|
|
|
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
|
|
|
{
|
2020-08-24 21:25:26 +00:00
|
|
|
$this->something();
|
2020-07-18 18:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 21:25:26 +00:00
|
|
|
|
|
|
|
CODE_SAMPLE
|
|
|
|
|
|
|
|
// code after change
|
|
|
|
, <<<'CODE_SAMPLE'
|
2020-07-18 18:26:57 +00:00
|
|
|
<?php
|
|
|
|
|
2020-08-15 22:49:15 +00:00
|
|
|
class SomeClass
|
2020-07-18 18:26:57 +00:00
|
|
|
{
|
2020-08-24 21:25:26 +00:00
|
|
|
public function run()
|
2020-07-29 00:27:21 +00:00
|
|
|
{
|
2020-08-24 21:25:26 +00:00
|
|
|
$this->somethingElse();
|
2020-07-29 00:27:21 +00:00
|
|
|
}
|
2020-07-18 18:26:57 +00:00
|
|
|
}
|
2020-08-24 21:25:26 +00:00
|
|
|
CODE_SAMPLE
|
|
|
|
);
|
2020-08-07 09:35:11 +00:00
|
|
|
|
2020-08-24 21:25:26 +00:00
|
|
|
// [OPTIONAL - UNCOMMENT TO USE]
|
2020-08-07 09:35:11 +00:00
|
|
|
|
2020-08-24 21:25:26 +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'
|
|
|
|
// ]);
|
2020-08-15 22:49:15 +00:00
|
|
|
|
2020-08-24 21:25:26 +00:00
|
|
|
// do you need to check for extra generated file?
|
|
|
|
// $rectorRecipe->addExtraFile('SomeExtraFile.php', '<?php ... SomeExtraFileContent');
|
2020-07-29 00:27:21 +00:00
|
|
|
|
2020-08-24 21:25:26 +00:00
|
|
|
// is the rule configurable? add default configuration here
|
2020-09-13 10:36:29 +00:00
|
|
|
// $rectorRecipe->setConfiguration(['SOME_CONSTANT_KEY' => ['before' => 'after']]);
|
2020-08-24 21:25:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
// [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();
|
2020-07-29 00:27:21 +00:00
|
|
|
|
2020-08-15 22:49:15 +00:00
|
|
|
$services->set(RectorRecipeProvider::class)
|
2020-08-24 21:25:26 +00:00
|
|
|
->arg('$rectorRecipe', inline_single_object($rectorRecipe, $services));
|
2020-07-18 18:26:57 +00:00
|
|
|
};
|