|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RectorLaravel\Rector\Coalesce; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Arg; |
| 7 | +use PhpParser\Node\Expr\BinaryOp\Coalesce; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Expr\StaticCall; |
| 11 | +use PHPStan\Type\ObjectType; |
| 12 | +use Rector\Contract\Rector\ConfigurableRectorInterface; |
| 13 | +use Rector\Rector\AbstractRector; |
| 14 | +use RectorLaravel\ValueObject\ApplyDefaultInsteadOfNullCoalesce; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 17 | +use Webmozart\Assert\Assert; |
| 18 | + |
| 19 | +/** |
| 20 | + * @see \RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\ApplyDefaultInsteadOfNullCoalesceRectorTest |
| 21 | + */ |
| 22 | +final class ApplyDefaultInsteadOfNullCoalesceRector extends AbstractRector implements ConfigurableRectorInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ApplyDefaultInsteadOfNullCoalesce[] |
| 26 | + */ |
| 27 | + private array $applyDefaultWith; |
| 28 | + |
| 29 | + public function __construct() |
| 30 | + { |
| 31 | + $this->applyDefaultWith = self::defaultLaravelMethods(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return ApplyDefaultInsteadOfNullCoalesce[] |
| 36 | + */ |
| 37 | + public static function defaultLaravelMethods(): array |
| 38 | + { |
| 39 | + return [ |
| 40 | + new ApplyDefaultInsteadOfNullCoalesce('config'), |
| 41 | + new ApplyDefaultInsteadOfNullCoalesce('env'), |
| 42 | + new ApplyDefaultInsteadOfNullCoalesce('data_get', argumentPosition: 2), |
| 43 | + new ApplyDefaultInsteadOfNullCoalesce('input', new ObjectType('Illuminate\Http\Request')), |
| 44 | + new ApplyDefaultInsteadOfNullCoalesce('get', new ObjectType('Illuminate\Support\Env')), |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + public function getRuleDefinition(): RuleDefinition |
| 49 | + { |
| 50 | + return new RuleDefinition( |
| 51 | + 'Apply default instead of null coalesce', |
| 52 | + [ |
| 53 | + new ConfiguredCodeSample( |
| 54 | + <<<'CODE_SAMPLE' |
| 55 | +custom_helper('app.name') ?? 'Laravel'; |
| 56 | +CODE_SAMPLE, |
| 57 | + <<<'CODE_SAMPLE' |
| 58 | +custom_helper('app.name', 'Laravel'); |
| 59 | +CODE_SAMPLE |
| 60 | + , |
| 61 | + [ |
| 62 | + ...self::defaultLaravelMethods(), |
| 63 | + new ApplyDefaultInsteadOfNullCoalesce('custom_helper'), |
| 64 | + ], |
| 65 | + ), |
| 66 | + ] |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function getNodeTypes(): array |
| 71 | + { |
| 72 | + return [Coalesce::class]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param Coalesce $node |
| 77 | + */ |
| 78 | + public function refactor(Node $node): MethodCall|StaticCall|FuncCall|null |
| 79 | + { |
| 80 | + if (! $node->left instanceof FuncCall && |
| 81 | + ! $node->left instanceof MethodCall && |
| 82 | + ! $node->left instanceof StaticCall |
| 83 | + ) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + if ($node->left->isFirstClassCallable()) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + $call = $node->left; |
| 92 | + |
| 93 | + foreach ($this->applyDefaultWith as $applyDefaultWith) { |
| 94 | + $valid = false; |
| 95 | + |
| 96 | + $objectType = $call->var ?? $call->class ?? null; |
| 97 | + |
| 98 | + if ( |
| 99 | + $applyDefaultWith->getObjectType() instanceof ObjectType && |
| 100 | + $objectType !== null && |
| 101 | + $this->isObjectType( |
| 102 | + $objectType, |
| 103 | + $applyDefaultWith->getObjectType()) && |
| 104 | + $this->isName($call->name, $applyDefaultWith->getMethodName()) |
| 105 | + ) { |
| 106 | + $valid = true; |
| 107 | + } elseif ( |
| 108 | + $applyDefaultWith->getObjectType() === null && |
| 109 | + $this->isName($call->name, $applyDefaultWith->getMethodName()) |
| 110 | + ) { |
| 111 | + $valid = true; |
| 112 | + } |
| 113 | + |
| 114 | + if (! $valid) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + if (count($call->args) === $applyDefaultWith->getArgumentPosition()) { |
| 119 | + if ($this->getType($node->right)->isNull()->yes()) { |
| 120 | + return $call; |
| 121 | + } |
| 122 | + $call->args[count($call->args)] = new Arg($node->right); |
| 123 | + |
| 124 | + return $call; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + public function configure(array $configuration): void |
| 132 | + { |
| 133 | + Assert::allIsInstanceOf($configuration, ApplyDefaultInsteadOfNullCoalesce::class); |
| 134 | + $this->applyDefaultWith = $configuration; |
| 135 | + } |
| 136 | +} |
0 commit comments