|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RectorLaravel\Rector\If_; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Arg; |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Expr\Assign; |
| 9 | +use PhpParser\Node\Expr\BooleanNot; |
| 10 | +use PhpParser\Node\Expr\FuncCall; |
| 11 | +use PhpParser\Node\Expr\Variable; |
| 12 | +use PhpParser\Node\Name; |
| 13 | +use PhpParser\Node\Stmt\Expression; |
| 14 | +use PhpParser\Node\Stmt\If_; |
| 15 | +use PhpParser\NodeTraverser; |
| 16 | +use Rector\Rector\AbstractRector; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 19 | + |
| 20 | +/** |
| 21 | + * @see \RectorLaravel\Tests\Rector\If_\ReportIfRector\ReportIfRectorTest |
| 22 | + */ |
| 23 | +class ReportIfRector extends AbstractRector |
| 24 | +{ |
| 25 | + public function getRuleDefinition(): RuleDefinition |
| 26 | + { |
| 27 | + return new RuleDefinition('Change if report to report_if', [ |
| 28 | + new CodeSample( |
| 29 | + <<<'CODE_SAMPLE' |
| 30 | +if ($condition) { |
| 31 | + report(new Exception()); |
| 32 | +} |
| 33 | +if (!$condition) { |
| 34 | + report(new Exception()); |
| 35 | +} |
| 36 | +CODE_SAMPLE |
| 37 | + , |
| 38 | + <<<'CODE_SAMPLE' |
| 39 | +report_if($condition, new Exception()); |
| 40 | +report_unless($condition, new Exception()); |
| 41 | +CODE_SAMPLE |
| 42 | + ), |
| 43 | + ]); |
| 44 | + } |
| 45 | + |
| 46 | + public function getNodeTypes(): array |
| 47 | + { |
| 48 | + return [If_::class]; |
| 49 | + } |
| 50 | + |
| 51 | + public function refactor(Node $node): ?Node |
| 52 | + { |
| 53 | + if (! $node instanceof If_) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + $ifStmts = $node->stmts; |
| 58 | + |
| 59 | + // Check if there's a single statement inside the if block to call abort() |
| 60 | + if ( |
| 61 | + count($ifStmts) === 1 && |
| 62 | + $ifStmts[0] instanceof Expression && |
| 63 | + $ifStmts[0]->expr instanceof FuncCall && |
| 64 | + $ifStmts[0]->expr->name instanceof Name && |
| 65 | + $this->isName($ifStmts[0]->expr, 'report') |
| 66 | + ) { |
| 67 | + $condition = $node->cond; |
| 68 | + /** @var FuncCall $abortCall */ |
| 69 | + $abortCall = $ifStmts[0]->expr; |
| 70 | + |
| 71 | + if ($this->exceptionUsesVariablesAssignedByCondition($abortCall, $condition)) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + // Check if the condition is a negation |
| 76 | + if ($condition instanceof BooleanNot) { |
| 77 | + // Create a new throw_unless function call |
| 78 | + return new Expression(new FuncCall(new Name('report_unless'), [ |
| 79 | + new Arg($condition->expr), |
| 80 | + ...$abortCall->args, |
| 81 | + ])); |
| 82 | + } else { |
| 83 | + // Create a new throw_if function call |
| 84 | + return new Expression(new FuncCall(new Name('report_if'), [ |
| 85 | + new Arg($condition), |
| 86 | + ...$abortCall->args, |
| 87 | + ])); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Make sure the exception doesn't use variables assigned by the condition or this |
| 96 | + * will cause broken code to be generated |
| 97 | + */ |
| 98 | + private function exceptionUsesVariablesAssignedByCondition(Expr $throwExpr, Expr $condition): bool |
| 99 | + { |
| 100 | + $conditionVariables = []; |
| 101 | + $returnValue = false; |
| 102 | + |
| 103 | + $this->traverseNodesWithCallable($condition, function (Node $node) use (&$conditionVariables): null { |
| 104 | + if ($node instanceof Assign) { |
| 105 | + $conditionVariables[] = $this->getName($node->var); |
| 106 | + } |
| 107 | + |
| 108 | + return null; |
| 109 | + }); |
| 110 | + |
| 111 | + $this->traverseNodesWithCallable($throwExpr, function (Node $node) use ($conditionVariables, &$returnValue): ?int { |
| 112 | + if ($node instanceof Variable && in_array($this->getName($node), $conditionVariables, true)) { |
| 113 | + $returnValue = true; |
| 114 | + |
| 115 | + return NodeTraverser::STOP_TRAVERSAL; |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + }); |
| 120 | + |
| 121 | + return $returnValue; |
| 122 | + } |
| 123 | +} |
0 commit comments