|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace MLL\RectorConfig\Rector; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\Assign; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\Coalesce; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\Equal; |
| 10 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 11 | +use PhpParser\Node\Expr\BooleanNot; |
| 12 | +use PhpParser\Node\Expr\Ternary; |
| 13 | +use PhpParser\Node\Expr\Throw_ as ExprThrow; |
| 14 | +use PhpParser\Node\Expr\Variable; |
| 15 | +use PhpParser\Node\Stmt\ClassMethod; |
| 16 | +use PhpParser\Node\Stmt\Expression; |
| 17 | +use PhpParser\Node\Stmt\Function_; |
| 18 | +use PhpParser\Node\Stmt\If_; |
| 19 | +use PhpParser\Node\Stmt\Throw_ as StmtThrow; |
| 20 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 21 | +use Rector\Rector\AbstractRector; |
| 22 | +use Rector\ValueObject\PhpVersion; |
| 23 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 24 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 25 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 26 | + |
| 27 | +/** |
| 28 | + * @see \MLL\RectorConfig\Tests\Rector\IfThrowToCoalesceThrowRector\IfThrowToCoalesceThrowRectorTest |
| 29 | + */ |
| 30 | +final class IfThrowToCoalesceThrowRector extends AbstractRector implements MinPhpVersionInterface |
| 31 | +{ |
| 32 | + use NullFalsyTypeTrait; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly ValueResolver $valueResolver, |
| 36 | + ) {} |
| 37 | + |
| 38 | + public function getRuleDefinition(): RuleDefinition |
| 39 | + { |
| 40 | + return new RuleDefinition('Transform if-null/falsy-throw patterns to coalesce throw expressions', [ |
| 41 | + new CodeSample( |
| 42 | + <<<'CODE_SAMPLE' |
| 43 | +$result = $this->fetchNullable(); |
| 44 | +if ($result === null) { |
| 45 | + throw new NullException; |
| 46 | +} |
| 47 | +CODE_SAMPLE, |
| 48 | + <<<'CODE_SAMPLE' |
| 49 | +$result = $this->fetchNullable() |
| 50 | + ?? throw new NullException; |
| 51 | +CODE_SAMPLE, |
| 52 | + ), |
| 53 | + new CodeSample( |
| 54 | + <<<'CODE_SAMPLE' |
| 55 | +$result = $this->fetchMaybeFalsy(); |
| 56 | +if (! $result) { |
| 57 | + throw new FalsyException; |
| 58 | +} |
| 59 | +CODE_SAMPLE, |
| 60 | + <<<'CODE_SAMPLE' |
| 61 | +$result = $this->fetchMaybeFalsy() |
| 62 | + ?: throw new FalsyException; |
| 63 | +CODE_SAMPLE, |
| 64 | + ), |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + /** @return array<class-string<Node>> */ |
| 69 | + public function getNodeTypes(): array |
| 70 | + { |
| 71 | + return [ClassMethod::class, Function_::class]; |
| 72 | + } |
| 73 | + |
| 74 | + /** @param ClassMethod|Function_ $node */ |
| 75 | + public function refactor(Node $node): ?Node |
| 76 | + { |
| 77 | + if ($node->stmts === null) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + $hasChanged = false; |
| 82 | + |
| 83 | + foreach ($node->stmts as $key => $stmt) { |
| 84 | + if (! $stmt instanceof Expression) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $expr = $stmt->expr; |
| 89 | + if (! $expr instanceof Assign) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (! $expr->var instanceof Variable) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + $nextStmt = $node->stmts[$key + 1] ?? null; |
| 98 | + if (! $nextStmt instanceof If_) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if ($nextStmt->else !== null || $nextStmt->elseifs !== []) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (count($nextStmt->stmts) !== 1) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + $ifStmt = $nextStmt->stmts[0]; |
| 111 | + |
| 112 | + $throwExpr = null; |
| 113 | + if ($ifStmt instanceof StmtThrow) { |
| 114 | + $throwExpr = new ExprThrow($ifStmt->expr); |
| 115 | + } elseif ($ifStmt instanceof Expression |
| 116 | + && $ifStmt->expr instanceof ExprThrow |
| 117 | + ) { |
| 118 | + $throwExpr = $ifStmt->expr; |
| 119 | + } |
| 120 | + |
| 121 | + if ($throwExpr === null) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $result = $this->matchCoalescePattern($expr, $nextStmt, $throwExpr); |
| 126 | + if ($result === null) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + $expr->expr = $result; |
| 131 | + unset($node->stmts[$key + 1]); |
| 132 | + $hasChanged = true; |
| 133 | + } |
| 134 | + |
| 135 | + return $hasChanged |
| 136 | + ? $node |
| 137 | + : null; |
| 138 | + } |
| 139 | + |
| 140 | + public function provideMinPhpVersion(): int |
| 141 | + { |
| 142 | + return PhpVersion::PHP_80; |
| 143 | + } |
| 144 | + |
| 145 | + private function matchCoalescePattern(Assign $assign, If_ $if, ExprThrow $throw): ?Expr |
| 146 | + { |
| 147 | + $variable = $assign->var; |
| 148 | + $condition = $if->cond; |
| 149 | + |
| 150 | + if (($condition instanceof Identical || $condition instanceof Equal) |
| 151 | + && $this->isNullComparison($condition, $variable) |
| 152 | + ) { |
| 153 | + return new Coalesce($assign->expr, $throw); |
| 154 | + } |
| 155 | + |
| 156 | + if ($condition instanceof BooleanNot |
| 157 | + && $this->nodeComparator->areNodesEqual($condition->expr, $variable) |
| 158 | + ) { |
| 159 | + if ($this->isOnlyNullFalsy($assign->expr)) { |
| 160 | + return new Coalesce($assign->expr, $throw); |
| 161 | + } |
| 162 | + |
| 163 | + return new Ternary($assign->expr, null, $throw); |
| 164 | + } |
| 165 | + |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + private function isNullComparison(Identical|Equal $comparison, Variable $variable): bool |
| 170 | + { |
| 171 | + $left = $comparison->left; |
| 172 | + $right = $comparison->right; |
| 173 | + |
| 174 | + if ($this->nodeComparator->areNodesEqual($left, $variable) |
| 175 | + && $this->valueResolver->isNull($right) |
| 176 | + ) { |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + return $this->valueResolver->isNull($left) |
| 181 | + && $this->nodeComparator->areNodesEqual($right, $variable); |
| 182 | + } |
| 183 | +} |
0 commit comments