|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Cake\Upgrade\Rector\Rector\MethodCall; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PHPStan\Type\ObjectType; |
| 10 | +use Rector\Rector\AbstractRector; |
| 11 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 12 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 13 | + |
| 14 | +/** |
| 15 | + * Transforms Table::find()->disableHydration() to Table::unhydratedFind(). |
| 16 | + * |
| 17 | + * @see https://book.cakephp.org/5/en/appendices/5-4-migration-guide.html |
| 18 | + */ |
| 19 | +final class DisableHydrationToUnhydratedFindRector extends AbstractRector |
| 20 | +{ |
| 21 | + public function getRuleDefinition(): RuleDefinition |
| 22 | + { |
| 23 | + return new RuleDefinition( |
| 24 | + 'Change Table::find()->disableHydration() to Table::unhydratedFind()', |
| 25 | + [ |
| 26 | + new CodeSample( |
| 27 | + <<<'CODE_SAMPLE' |
| 28 | +$articles->find('all')->disableHydration(); |
| 29 | +CODE_SAMPLE |
| 30 | + , |
| 31 | + <<<'CODE_SAMPLE' |
| 32 | +$articles->unhydratedFind('all'); |
| 33 | +CODE_SAMPLE, |
| 34 | + ), |
| 35 | + ], |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public function getNodeTypes(): array |
| 40 | + { |
| 41 | + return [MethodCall::class]; |
| 42 | + } |
| 43 | + |
| 44 | + public function refactor(Node $node): ?Node |
| 45 | + { |
| 46 | + if (!$node instanceof MethodCall) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + if (!$node->name instanceof Identifier || $node->name->toString() !== 'disableHydration') { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + if (count($node->args) !== 0) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + $current = $node->var; |
| 59 | + while ($current instanceof MethodCall) { |
| 60 | + if ($current->name instanceof Identifier && $current->name->toString() === 'find') { |
| 61 | + if (!(new ObjectType('Cake\ORM\Table'))->isSuperTypeOf($this->getType($current->var))->yes()) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + $current->name = new Identifier('unhydratedFind'); |
| 66 | + |
| 67 | + return $node->var; |
| 68 | + } |
| 69 | + |
| 70 | + $current = $current->var; |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + } |
| 75 | +} |
0 commit comments