|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Php80\Rector\Ternary; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Expr\Ternary; |
| 10 | +use Rector\Core\Rector\AbstractRector; |
| 11 | +use Rector\Core\RectorDefinition\CodeSample; |
| 12 | +use Rector\Core\RectorDefinition\RectorDefinition; |
| 13 | + |
| 14 | +/** |
| 15 | + * @see https://wiki.php.net/rfc/get_debug_type |
| 16 | + * |
| 17 | + * @see \Rector\Php80\Tests\Rector\Ternary\GetDebugTypeRector\GetDebugTypeRectorTest |
| 18 | + */ |
| 19 | +final class GetDebugTypeRector extends AbstractRector |
| 20 | +{ |
| 21 | + public function getDefinition(): RectorDefinition |
| 22 | + { |
| 23 | + return new RectorDefinition('Change ternary type resolve to get_debug_type()', [ |
| 24 | + new CodeSample( |
| 25 | + <<<'PHP' |
| 26 | +class SomeClass |
| 27 | +{ |
| 28 | + public function run($value) |
| 29 | + { |
| 30 | + return is_object($value) ? get_class($value) : gettype($value); |
| 31 | + } |
| 32 | +} |
| 33 | +PHP |
| 34 | +, |
| 35 | + <<<'PHP' |
| 36 | +class SomeClass |
| 37 | +{ |
| 38 | + public function run($value) |
| 39 | + { |
| 40 | + return get_debug_type($value); |
| 41 | + } |
| 42 | +} |
| 43 | +PHP |
| 44 | + |
| 45 | + ), |
| 46 | + ]); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string[] |
| 51 | + */ |
| 52 | + public function getNodeTypes(): array |
| 53 | + { |
| 54 | + return [Ternary::class]; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param Ternary $node |
| 59 | + */ |
| 60 | + public function refactor(Node $node): ?Node |
| 61 | + { |
| 62 | + if ($this->shouldSkip($node)) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + if (! $this->areValuesIdentical($node)) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + /** @var FuncCall $funcCall */ |
| 71 | + $funcCall = $node->if; |
| 72 | + $firstExpr = $funcCall->args[0]->value; |
| 73 | + |
| 74 | + return $this->createFuncCall('get_debug_type', [$firstExpr]); |
| 75 | + } |
| 76 | + |
| 77 | + private function shouldSkip(Ternary $ternary): bool |
| 78 | + { |
| 79 | + if (! $this->isFuncCallName($ternary->cond, 'is_object')) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + if ($ternary->if === null) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + if (! $this->isFuncCallName($ternary->if, 'get_class')) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + return ! $this->isFuncCallName($ternary->else, 'gettype'); |
| 92 | + } |
| 93 | + |
| 94 | + private function areValuesIdentical(Ternary $ternary): bool |
| 95 | + { |
| 96 | + /** @var FuncCall $isObjectFuncCall */ |
| 97 | + $isObjectFuncCall = $ternary->cond; |
| 98 | + $firstExpr = $isObjectFuncCall->args[0]->value; |
| 99 | + |
| 100 | + /** @var FuncCall $getClassFuncCall */ |
| 101 | + $getClassFuncCall = $ternary->if; |
| 102 | + $secondExpr = $getClassFuncCall->args[0]->value; |
| 103 | + |
| 104 | + /** @var FuncCall $gettypeFuncCall */ |
| 105 | + $gettypeFuncCall = $ternary->else; |
| 106 | + $thirdExpr = $gettypeFuncCall->args[0]->value; |
| 107 | + |
| 108 | + if (! $this->areNodesEqual($firstExpr, $secondExpr)) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + return $this->areNodesEqual($firstExpr, $thirdExpr); |
| 113 | + } |
| 114 | +} |
0 commit comments