|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\CodingStyle\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\Assign; |
| 9 | +use PhpParser\Node\Expr\BinaryOp; |
| 10 | +use PhpParser\Node\Expr\Closure; |
| 11 | +use PhpParser\Node\Expr\MethodCall; |
| 12 | +use PhpParser\Node\Expr\Variable; |
| 13 | +use PhpParser\Node\Stmt; |
| 14 | +use PhpParser\Node\Stmt\ClassMethod; |
| 15 | +use PhpParser\Node\Stmt\Expression; |
| 16 | +use PhpParser\Node\Stmt\Function_; |
| 17 | +use PhpParser\Node\Stmt\Return_; |
| 18 | +use Rector\CodingStyle\ValueObject\VariableAndExprAssign; |
| 19 | +use Rector\Rector\AbstractRector; |
| 20 | +use Rector\ValueObject\PhpVersionFeature; |
| 21 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | + |
| 25 | +/** |
| 26 | + * @see \Rector\Tests\CodingStyle\Rector\ClassMethod\BinaryOpStandaloneAssignsToDirectRector\BinaryOpStandaloneAssignsToDirectRectorTest |
| 27 | + */ |
| 28 | +final class BinaryOpStandaloneAssignsToDirectRector extends AbstractRector implements MinPhpVersionInterface |
| 29 | +{ |
| 30 | + public function getRuleDefinition(): RuleDefinition |
| 31 | + { |
| 32 | + return new RuleDefinition('Change 2 standalone assigns to variable then binary op to direct binary op', [ |
| 33 | + new CodeSample( |
| 34 | + <<<'CODE_SAMPLE' |
| 35 | +function run() |
| 36 | +{ |
| 37 | + $value = 100; |
| 38 | + $anotherValue = 200; |
| 39 | +
|
| 40 | + return 100 <=> 200; |
| 41 | +} |
| 42 | +CODE_SAMPLE |
| 43 | + |
| 44 | + , |
| 45 | + <<<'CODE_SAMPLE' |
| 46 | +function run() |
| 47 | +{ |
| 48 | + return 100 <=> 200; |
| 49 | +} |
| 50 | +CODE_SAMPLE |
| 51 | + ), |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return array<class-string<Node>> |
| 57 | + */ |
| 58 | + public function getNodeTypes(): array |
| 59 | + { |
| 60 | + return [ClassMethod::class, Function_::class, Closure::class]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param ClassMethod|Function_|Closure $node |
| 65 | + */ |
| 66 | + public function refactor(Node $node): ?Node |
| 67 | + { |
| 68 | + if ($node->stmts === null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + if (count($node->stmts) !== 3) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + $firstStmt = $node->stmts[0]; |
| 77 | + $secondStmt = $node->stmts[1]; |
| 78 | + $thirdStmt = $node->stmts[2]; |
| 79 | + |
| 80 | + if (! $thirdStmt instanceof Return_) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $firstVariableAndExprAssign = $this->matchToVariableAssignExpr($firstStmt); |
| 85 | + if (! $firstVariableAndExprAssign instanceof VariableAndExprAssign) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $secondVariableAndExprAssign = $this->matchToVariableAssignExpr($secondStmt); |
| 90 | + if (! $secondVariableAndExprAssign instanceof VariableAndExprAssign) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + if (! $thirdStmt->expr instanceof BinaryOp) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + $binaryOp = $thirdStmt->expr; |
| 99 | + |
| 100 | + if (! $this->nodeComparator->areNodesEqual($binaryOp->left, $firstVariableAndExprAssign->getVariable())) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if (! $this->nodeComparator->areNodesEqual($binaryOp->right, $secondVariableAndExprAssign->getVariable())) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + $binaryOp->left = $firstVariableAndExprAssign->getExpr(); |
| 109 | + $binaryOp->right = $secondVariableAndExprAssign->getExpr(); |
| 110 | + |
| 111 | + $node->stmts = [$thirdStmt]; |
| 112 | + return $node; |
| 113 | + } |
| 114 | + |
| 115 | + public function provideMinPhpVersion(): int |
| 116 | + { |
| 117 | + return PhpVersionFeature::VARIADIC_PARAM; |
| 118 | + } |
| 119 | + |
| 120 | + private function matchToVariableAssignExpr(Stmt $stmt): ?VariableAndExprAssign |
| 121 | + { |
| 122 | + if (! $stmt instanceof Expression) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + if (! $stmt->expr instanceof Assign) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + $assign = $stmt->expr; |
| 131 | + if (! $assign->var instanceof Variable) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + // skip complex cases |
| 136 | + if ($assign->expr instanceof MethodCall) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + return new VariableAndExprAssign($assign->var, $assign->expr); |
| 141 | + } |
| 142 | +} |
0 commit comments