|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SaschaEgerer\PhpstanTypo3\Type; |
| 4 | + |
| 5 | +use PhpParser\Node\Arg; |
| 6 | +use PhpParser\Node\Expr\Assign; |
| 7 | +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual; |
| 10 | +use PhpParser\Node\Expr\BooleanNot; |
| 11 | +use PhpParser\Node\Expr\FuncCall; |
| 12 | +use PhpParser\Node\Expr\StaticCall; |
| 13 | +use PhpParser\Node\Name; |
| 14 | +use PhpParser\Node\Scalar\LNumber; |
| 15 | +use PHPStan\Analyser\Scope; |
| 16 | +use PHPStan\Analyser\SpecifiedTypes; |
| 17 | +use PHPStan\Analyser\TypeSpecifier; |
| 18 | +use PHPStan\Analyser\TypeSpecifierAwareExtension; |
| 19 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 20 | +use PHPStan\Reflection\MethodReflection; |
| 21 | +use PHPStan\Type\StaticMethodTypeSpecifyingExtension; |
| 22 | +use TYPO3\CMS\Core\Utility\MathUtility; |
| 23 | + |
| 24 | +final class MathUtilityTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension |
| 25 | +{ |
| 26 | + |
| 27 | + private const METHOD_FORCE_INTEGER_IN_RANGE = 'forceIntegerInRange'; |
| 28 | + private const METHOD_CONVERT_TO_POSITIVE_INTEGER = 'convertToPositiveInteger'; |
| 29 | + private const METHOD_CAN_BE_INTERPRETED_AS_INTEGER = 'canBeInterpretedAsInteger'; |
| 30 | + private const METHOD_CAN_BE_INTERPRETED_AS_FLOAT = 'canBeInterpretedAsFloat'; |
| 31 | + private const METHOD_IS_INTEGER_IN_RANGE = 'isIntegerInRange'; |
| 32 | + |
| 33 | + /** @var TypeSpecifier */ |
| 34 | + private $typeSpecifier; |
| 35 | + |
| 36 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void |
| 37 | + { |
| 38 | + $this->typeSpecifier = $typeSpecifier; |
| 39 | + } |
| 40 | + |
| 41 | + public function getClass(): string |
| 42 | + { |
| 43 | + return MathUtility::class; |
| 44 | + } |
| 45 | + |
| 46 | + public function isStaticMethodSupported(MethodReflection $staticMethodReflection, StaticCall $node, TypeSpecifierContext $context): bool |
| 47 | + { |
| 48 | + return in_array( |
| 49 | + $staticMethodReflection->getName(), |
| 50 | + [ |
| 51 | + self::METHOD_FORCE_INTEGER_IN_RANGE, |
| 52 | + self::METHOD_CONVERT_TO_POSITIVE_INTEGER, |
| 53 | + self::METHOD_CAN_BE_INTERPRETED_AS_INTEGER, |
| 54 | + self::METHOD_CAN_BE_INTERPRETED_AS_FLOAT, |
| 55 | + self::METHOD_IS_INTEGER_IN_RANGE, |
| 56 | + ], |
| 57 | + true |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function specifyTypes(MethodReflection $staticMethodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes |
| 62 | + { |
| 63 | + if ($staticMethodReflection->getName() === self::METHOD_FORCE_INTEGER_IN_RANGE) { |
| 64 | + return $this->specifyTypesForForceIntegerInRange($node, $scope); |
| 65 | + } |
| 66 | + |
| 67 | + if ($staticMethodReflection->getName() === self::METHOD_IS_INTEGER_IN_RANGE) { |
| 68 | + return $this->specifyTypesForIsIntegerInRange($node, $scope); |
| 69 | + } |
| 70 | + |
| 71 | + if ($staticMethodReflection->getName() === self::METHOD_CONVERT_TO_POSITIVE_INTEGER) { |
| 72 | + return $this->specifyTypesForConvertToPositiveInteger($node, $scope); |
| 73 | + } |
| 74 | + |
| 75 | + if ($staticMethodReflection->getName() === self::METHOD_CAN_BE_INTERPRETED_AS_INTEGER) { |
| 76 | + return $this->specifyTypesForCanBeInterpretedAsInteger($node, $scope); |
| 77 | + } |
| 78 | + |
| 79 | + return $this->specifyTypesForCanBeInterpretedAsFloat($node, $scope); |
| 80 | + } |
| 81 | + |
| 82 | + private function specifyTypesForForceIntegerInRange(StaticCall $node, Scope $scope): SpecifiedTypes |
| 83 | + { |
| 84 | + $parentNode = $node->getAttribute('parent'); |
| 85 | + |
| 86 | + if (!$parentNode instanceof Assign) { |
| 87 | + return new SpecifiedTypes(); |
| 88 | + } |
| 89 | + |
| 90 | + $min = isset($node->getArgs()[1]) ? $node->getArgs()[1]->value : new LNumber(0); |
| 91 | + $max = isset($node->getArgs()[2]) ? $node->getArgs()[2]->value : new LNumber(2000000000); |
| 92 | + |
| 93 | + return $this->typeSpecifier->specifyTypesInCondition( |
| 94 | + $scope, |
| 95 | + new BooleanAnd( |
| 96 | + new FuncCall( |
| 97 | + new Name('is_int'), |
| 98 | + [new Arg($parentNode->var)] |
| 99 | + ), |
| 100 | + new BooleanAnd( |
| 101 | + new GreaterOrEqual( |
| 102 | + $parentNode->var, |
| 103 | + $min |
| 104 | + ), |
| 105 | + new SmallerOrEqual( |
| 106 | + $parentNode->var, |
| 107 | + $max |
| 108 | + ) |
| 109 | + ) |
| 110 | + ), |
| 111 | + TypeSpecifierContext::createTruthy() |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + private function specifyTypesForIsIntegerInRange(StaticCall $node, Scope $scope): SpecifiedTypes |
| 116 | + { |
| 117 | + $firstArgument = $node->getArgs()[0]; |
| 118 | + $firstArgumentType = $scope->getType($firstArgument->value); |
| 119 | + |
| 120 | + $min = $node->getArgs()[1]->value; |
| 121 | + $max = $node->getArgs()[2]->value; |
| 122 | + |
| 123 | + if ($firstArgumentType->isString()->no()) { |
| 124 | + $typeCheckFuncCall = new FuncCall( |
| 125 | + new Name('is_int'), |
| 126 | + [$firstArgument] |
| 127 | + ); |
| 128 | + } else { |
| 129 | + $typeCheckFuncCall = new BooleanAnd( |
| 130 | + new FuncCall( |
| 131 | + new Name('is_numeric'), |
| 132 | + [$firstArgument] |
| 133 | + ), |
| 134 | + new BooleanNot( |
| 135 | + new FuncCall( |
| 136 | + new Name('is_float'), |
| 137 | + [$firstArgument] |
| 138 | + ) |
| 139 | + ) |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + return $this->typeSpecifier->specifyTypesInCondition( |
| 144 | + $scope, |
| 145 | + new BooleanAnd( |
| 146 | + $typeCheckFuncCall, |
| 147 | + new BooleanAnd( |
| 148 | + new GreaterOrEqual( |
| 149 | + $firstArgument->value, |
| 150 | + $min |
| 151 | + ), |
| 152 | + new SmallerOrEqual( |
| 153 | + $firstArgument->value, |
| 154 | + $max |
| 155 | + ) |
| 156 | + ) |
| 157 | + ), |
| 158 | + TypeSpecifierContext::createTruthy() |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + private function specifyTypesForConvertToPositiveInteger(StaticCall $node, Scope $scope): SpecifiedTypes |
| 163 | + { |
| 164 | + $parentNode = $node->getAttribute('parent'); |
| 165 | + |
| 166 | + if (!$parentNode instanceof Assign) { |
| 167 | + return new SpecifiedTypes(); |
| 168 | + } |
| 169 | + |
| 170 | + return $this->typeSpecifier->specifyTypesInCondition( |
| 171 | + $scope, |
| 172 | + new BooleanAnd( |
| 173 | + new FuncCall( |
| 174 | + new Name('is_int'), |
| 175 | + [new Arg($parentNode)] |
| 176 | + ), |
| 177 | + new BooleanAnd( |
| 178 | + new GreaterOrEqual( |
| 179 | + $parentNode->var, |
| 180 | + new LNumber(0) |
| 181 | + ), |
| 182 | + new SmallerOrEqual( |
| 183 | + $parentNode->var, |
| 184 | + new LNumber(PHP_INT_MAX) |
| 185 | + ) |
| 186 | + ) |
| 187 | + ), |
| 188 | + TypeSpecifierContext::createTruthy() |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + private function specifyTypesForCanBeInterpretedAsInteger(StaticCall $node, Scope $scope): SpecifiedTypes |
| 193 | + { |
| 194 | + $firstArgument = $node->getArgs()[0]; |
| 195 | + |
| 196 | + return $this->typeSpecifier->specifyTypesInCondition( |
| 197 | + $scope, |
| 198 | + new FuncCall( |
| 199 | + new Name('is_numeric'), |
| 200 | + [$firstArgument] |
| 201 | + ), |
| 202 | + TypeSpecifierContext::createTruthy() |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + private function specifyTypesForCanBeInterpretedAsFloat(StaticCall $node, Scope $scope): SpecifiedTypes |
| 207 | + { |
| 208 | + $firstArgument = $node->getArgs()[0]; |
| 209 | + |
| 210 | + return $this->typeSpecifier->specifyTypesInCondition( |
| 211 | + $scope, |
| 212 | + new FuncCall( |
| 213 | + new Name('is_float'), |
| 214 | + [$firstArgument] |
| 215 | + ), |
| 216 | + TypeSpecifierContext::createTruthy() |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | +} |
0 commit comments