mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-25 12:14:02 +01:00
98 lines
2.1 KiB
Markdown
98 lines
2.1 KiB
Markdown
# Master Fluent API Builder
|
|
|
|
You can [add own Rector](/docs/HowToCreateOwnRector.md) that extends `Rector\Rector\AbstractRector`. But it takes lot of code and knowledge to do so.
|
|
|
|
How about nice fluent API like this?
|
|
|
|
```php
|
|
$rector = $this->builderRectorFactory->create()
|
|
->matchMethodCallByType('Nette\Application\UI\Control')
|
|
->matchMethodName('invalidateControl')
|
|
->changeMethodNameTo('redrawControl');
|
|
```
|
|
|
|
That can perform followin change:
|
|
|
|
```diff
|
|
-$control->invalidateControl();
|
|
+$control->redrawControl();
|
|
```
|
|
|
|
Nice and clear change in 4 lines, with autocomplete and typehinting of PHP.
|
|
|
|
|
|
## 3 Steps to Build Specific Rector
|
|
|
|
### 1. Implement `Rector\Contract\Rector\RectorInterface`
|
|
|
|
```php
|
|
namespace App\Rector;
|
|
|
|
use Rector\Contract\Rector\RectorInterface;
|
|
|
|
final class NetteRectorProvider implements RectorInterface
|
|
{
|
|
/**
|
|
* @return RectorInterface[]
|
|
*/
|
|
public function provide(): array
|
|
{
|
|
return [];
|
|
}
|
|
}
|
|
```
|
|
|
|
### 2. Builder the Rector
|
|
|
|
```diff
|
|
namespace App\Rector;
|
|
|
|
use Rector\Contract\Rector\RectorInterface;
|
|
+use Rector\RectorBuilder\BuilderRectorFactory;
|
|
|
|
final class NetteRectorProvider implements RectorInterface
|
|
{
|
|
+ /**
|
|
+ * @var BuilderRectorFactory
|
|
+ */
|
|
+ private $builderRectorFactory;
|
|
+
|
|
+ public function __construct(BuilderRectorFactory $builderRectorFactory)
|
|
+ {
|
|
+ $this->builderRectorFactory = $builderRectorFactory;
|
|
+ }
|
|
+
|
|
/**
|
|
* @return RectorInterface[]
|
|
*/
|
|
public function provide(): array
|
|
{
|
|
+ $redrawControlRector = $this->builderRectorFactory->create()
|
|
+ ->matchMethodCallByType('Nette\Application\UI\Control')
|
|
+ ->matchMethodName('invalidateControl')
|
|
+ ->changeMethodNameTo('redrawControl');
|
|
- return [];
|
|
+ return [$redrawControlRector];
|
|
|
|
}
|
|
}
|
|
```
|
|
|
|
### 3. Register as Service to `rector.yml`
|
|
|
|
```yml
|
|
services:
|
|
_defaults:
|
|
autowire: true
|
|
|
|
App\Rector\NetteRectorProvider: ~
|
|
```
|
|
|
|
That's it!
|
|
|
|
Now you can load the config and process your code with it:
|
|
|
|
```php
|
|
vendor/bin/rector process src --config rector.yml
|
|
```
|