|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Php86\Rector\FuncCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr; |
| 10 | +use PhpParser\Node\Expr\ConstFetch; |
| 11 | +use PhpParser\Node\Expr\FuncCall; |
| 12 | +use PhpParser\Node\Expr\UnaryMinus; |
| 13 | +use PhpParser\Node\Expr\UnaryPlus; |
| 14 | +use PhpParser\Node\Scalar; |
| 15 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 16 | +use Rector\Rector\AbstractRector; |
| 17 | +use Rector\ValueObject\PhpVersionFeature; |
| 18 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 21 | + |
| 22 | +/** |
| 23 | + * @see \Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\MinMaxToClampRectorTest |
| 24 | + */ |
| 25 | +final class MinMaxToClampRector extends AbstractRector implements MinPhpVersionInterface |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private readonly ValueResolver $valueResolver |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition('Convert nested min()/max() calls to clamp()', [ |
| 35 | + new CodeSample( |
| 36 | + <<<'CODE_SAMPLE' |
| 37 | +$result = max(0, min(100, $value)); |
| 38 | +CODE_SAMPLE |
| 39 | + , |
| 40 | + <<<'CODE_SAMPLE' |
| 41 | +$result = clamp($value, 0, 100); |
| 42 | +CODE_SAMPLE |
| 43 | + ), |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public function getNodeTypes(): array |
| 49 | + { |
| 50 | + return [FuncCall::class]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param FuncCall $node |
| 55 | + */ |
| 56 | + public function refactor(Node $node): ?Node |
| 57 | + { |
| 58 | + if ($node->isFirstClassCallable()) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + if ($this->isName($node, 'max')) { |
| 63 | + return $this->matchClampFuncCall($node, 'min'); |
| 64 | + } |
| 65 | + |
| 66 | + if ($this->isName($node, 'min')) { |
| 67 | + return $this->matchClampFuncCall($node, 'max'); |
| 68 | + } |
| 69 | + |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + public function provideMinPhpVersion(): int |
| 74 | + { |
| 75 | + return PhpVersionFeature::CLAMP; |
| 76 | + } |
| 77 | + |
| 78 | + private function matchClampFuncCall(FuncCall $outerFuncCall, string $expectedInnerFuncName): ?FuncCall |
| 79 | + { |
| 80 | + $args = $outerFuncCall->getArgs(); |
| 81 | + |
| 82 | + if (count($args) !== 2) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + if (! $this->isSupportedArg($args[0]) || ! $this->isSupportedArg($args[1])) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + $leftValue = $args[0]->value; |
| 91 | + $rightValue = $args[1]->value; |
| 92 | + |
| 93 | + if ($leftValue instanceof FuncCall) { |
| 94 | + return $this->createClampFuncCall($outerFuncCall, $leftValue, $rightValue, $expectedInnerFuncName); |
| 95 | + } |
| 96 | + |
| 97 | + if ($rightValue instanceof FuncCall) { |
| 98 | + return $this->createClampFuncCall($outerFuncCall, $rightValue, $leftValue, $expectedInnerFuncName); |
| 99 | + } |
| 100 | + |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + private function createClampFuncCall( |
| 105 | + FuncCall $outerFuncCall, |
| 106 | + FuncCall $innerFuncCall, |
| 107 | + Expr $outerBoundExpr, |
| 108 | + string $expectedInnerFuncName |
| 109 | + ): ?FuncCall { |
| 110 | + if ($innerFuncCall->isFirstClassCallable()) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + if (! $this->isName($innerFuncCall, $expectedInnerFuncName)) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + $args = $innerFuncCall->getArgs(); |
| 119 | + if (count($args) !== 2) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + if (! $this->isSupportedArg($args[0]) || ! $this->isSupportedArg($args[1])) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + $valueAndBound = $this->matchValueAndKnownBound($args[0]->value, $args[1]->value); |
| 128 | + if ($valueAndBound === null) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + [$valueExpr, $innerBoundExpr] = $valueAndBound; |
| 133 | + |
| 134 | + if ($this->isName($outerFuncCall, 'max')) { |
| 135 | + return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $outerBoundExpr, $innerBoundExpr]); |
| 136 | + } |
| 137 | + |
| 138 | + return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $innerBoundExpr, $outerBoundExpr]); |
| 139 | + } |
| 140 | + |
| 141 | + private function isSupportedArg(Arg $arg): bool |
| 142 | + { |
| 143 | + return ! $arg->unpack && $arg->name === null; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @return array{Expr, Expr}|null |
| 148 | + */ |
| 149 | + private function matchValueAndKnownBound(Expr $firstExpr, Expr $secondExpr): ?array |
| 150 | + { |
| 151 | + $isFirstKnownBound = $this->isKnownBound($firstExpr); |
| 152 | + $isSecondKnownBound = $this->isKnownBound($secondExpr); |
| 153 | + |
| 154 | + if ($isFirstKnownBound === $isSecondKnownBound) { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + if ($isFirstKnownBound) { |
| 159 | + return [$secondExpr, $firstExpr]; |
| 160 | + } |
| 161 | + |
| 162 | + return [$firstExpr, $secondExpr]; |
| 163 | + } |
| 164 | + |
| 165 | + private function isKnownBound(Expr $expr): bool |
| 166 | + { |
| 167 | + return $this->getType($expr)->isConstantScalarValue()->yes(); |
| 168 | + } |
| 169 | +} |
0 commit comments