Skip to content

Commit

Permalink
add rector to transform set _serialize to viewBuilder setOption (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal authored Sep 6, 2024
1 parent c2f65ec commit 4ad8ea0
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/rector/sets/cakephp50.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
use Cake\Upgrade\Rector\Rector\MethodCall\OptionsArrayToNamedParametersRector;
use Cake\Upgrade\Rector\Rector\MethodCall\RemoveMethodCallRector;
use Cake\Upgrade\Rector\Rector\MethodCall\TableRegistryLocatorRector;
use Cake\Upgrade\Rector\Rector\MethodCall\SetSerializeToViewBuilderRector;
use Cake\Upgrade\Rector\ValueObject\OptionsArrayToNamedParameters;
use Cake\Upgrade\Rector\ValueObject\RemoveMethodCall;
use Cake\Upgrade\Rector\ValueObject\SetSerializeToView;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -116,4 +118,9 @@
]);

$rectorConfig->rule(TableRegistryLocatorRector::class);

$rectorConfig->ruleWithConfiguration(SetSerializeToViewBuilderRector::class, [
new SetSerializeToView('Cake\Controller\Controller'),
new SetSerializeToView('Cake\View\Cell'),
]);
};
73 changes: 73 additions & 0 deletions src/Rector/Rector/MethodCall/SetSerializeToViewBuilderRector.php
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);
};
24 changes: 24 additions & 0 deletions src/Rector/ValueObject/SetSerializeToView.php
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class ArticlesController extends Controller
protected $modelClass = null;

protected $defaultTable = null;

public function setSerialize(): void
{
$this->set('_serialize', 'result');
}
}

class CustomBehavior extends Behavior
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

class SomeCell extends \Cake\View\Cell {
protected $_validCellOptions;

public function setSerialize(): void
{
$this->set('_serialize', 'result');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class ArticlesController extends Controller
protected ?string $modelClass = null;

protected ?string $defaultTable = null;

public function setSerialize(): void
{
$this->viewBuilder()->setOption('serialize', 'result');
}
}

class CustomBehavior extends Behavior
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

class SomeCell extends \Cake\View\Cell {
protected array $_validCellOptions;

public function setSerialize(): void
{
$this->viewBuilder()->setOption('serialize', 'result');
}
}

0 comments on commit 4ad8ea0

Please sign in to comment.