|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace staabm\PHPStanDba\Extensions; |
| 6 | + |
| 7 | +use mysqli; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Reflection\FunctionReflection; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 14 | +use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
| 15 | +use PHPStan\Type\Accessory\AccessoryNumericStringType; |
| 16 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 17 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 18 | +use PHPStan\Type\IntersectionType; |
| 19 | +use PHPStan\Type\StringType; |
| 20 | +use PHPStan\Type\Type; |
| 21 | + |
| 22 | +final class MysqliEscapeStringDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, DynamicFunctionReturnTypeExtension |
| 23 | +{ |
| 24 | + public function getClass(): string |
| 25 | + { |
| 26 | + return mysqli::class; |
| 27 | + } |
| 28 | + |
| 29 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 30 | + { |
| 31 | + return 'real_escape_string' === $methodReflection->getName(); |
| 32 | + } |
| 33 | + |
| 34 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 35 | + { |
| 36 | + return 'mysqli_real_escape_string' === $functionReflection->getName(); |
| 37 | + } |
| 38 | + |
| 39 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type |
| 40 | + { |
| 41 | + $args = $functionCall->getArgs(); |
| 42 | + if (\count($args) < 2) { |
| 43 | + return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
| 44 | + } |
| 45 | + |
| 46 | + $argType = $scope->getType($args[1]->value); |
| 47 | + |
| 48 | + return $this->inferType($argType); |
| 49 | + } |
| 50 | + |
| 51 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 52 | + { |
| 53 | + $args = $methodCall->getArgs(); |
| 54 | + if (0 === \count($args)) { |
| 55 | + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 56 | + } |
| 57 | + |
| 58 | + $argType = $scope->getType($args[0]->value); |
| 59 | + |
| 60 | + return $this->inferType($argType); |
| 61 | + } |
| 62 | + |
| 63 | + private function inferType(Type $argType): Type |
| 64 | + { |
| 65 | + $intersection = [new StringType()]; |
| 66 | + |
| 67 | + if ($argType->isNumericString()->yes()) { |
| 68 | + // a numeric string is by definition non-empty. therefore don't combine the e accessories |
| 69 | + $intersection[] = new AccessoryNumericStringType(); |
| 70 | + } elseif ($argType->isNonEmptyString()->yes()) { |
| 71 | + $intersection[] = new AccessoryNonEmptyStringType(); |
| 72 | + } |
| 73 | + |
| 74 | + if (\count($intersection) > 1) { |
| 75 | + return new IntersectionType($intersection); |
| 76 | + } |
| 77 | + |
| 78 | + return new StringType(); |
| 79 | + } |
| 80 | +} |
0 commit comments