-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rector to transform set _serialize to viewBuilder setOption (#294)
- Loading branch information
Showing
7 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/Rector/Rector/MethodCall/SetSerializeToViewBuilderRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cake\Upgrade\Rector\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Config\RectorConfig; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class SetSerializeToViewBuilderRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition { | ||
return new RuleDefinition('Change `$this->set(\'_serialize\', \'result\')` to `$this->viewBuilder()->setOption(\'serialize\', \'result\')`.', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
$this->set('_serialize', 'result'); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$this->viewBuilder()->setOption('serialize', 'result'); | ||
CODE_SAMPLE | ||
) | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
if(! $node instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
// Ensure it's the method call we're looking for: $this->set('_serialize', ...) | ||
if (! $this->isMethodCallMatch($node, 'set', '_serialize')) { | ||
return null; | ||
} | ||
|
||
// Create the new method call | ||
return $this->nodeFactory->createMethodCall( | ||
$this->nodeFactory->createMethodCall($node->var, 'viewBuilder'), | ||
'setOption', | ||
['serialize', $node->args[1]->value] | ||
); | ||
} | ||
|
||
private function isMethodCallMatch(MethodCall $methodCall, string $methodName, string $firstArgumentValue): bool | ||
{ | ||
// Check if the method is 'set' | ||
if (! $this->isName($methodCall->name, $methodName)) { | ||
return false; | ||
} | ||
|
||
// Check if the first argument is '_serialize'w | ||
return isset($methodCall->args[0]) && $methodCall->args[0]->value->value === $firstArgumentValue; | ||
} | ||
|
||
public function configure(array $configuration): void { | ||
// No configuration options | ||
} | ||
} | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(SetSerializeToViewBuilderRector::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cake\Upgrade\Rector\ValueObject; | ||
|
||
use PHPStan\Type\ObjectType; | ||
|
||
final class SetSerializeToView | ||
{ | ||
public function __construct( | ||
private string $class | ||
) { | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return $this->class; | ||
} | ||
|
||
public function getObjectType(): ObjectType | ||
{ | ||
return new ObjectType($this->class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters