|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace staabm\PHPStanDba\Extensions; |
| 6 | + |
| 7 | +use PDO; |
| 8 | +use PDOStatement; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Reflection\MethodReflection; |
| 12 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 13 | +use PHPStan\Type\ArrayType; |
| 14 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 15 | +use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
| 16 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 17 | +use PHPStan\Type\Constant\ConstantStringType; |
| 18 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 19 | +use PHPStan\Type\Generic\GenericObjectType; |
| 20 | +use PHPStan\Type\IntegerType; |
| 21 | +use PHPStan\Type\Type; |
| 22 | + |
| 23 | +final class PdoStatementFetchDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 24 | +{ |
| 25 | + public function getClass(): string |
| 26 | + { |
| 27 | + return PDOStatement::class; |
| 28 | + } |
| 29 | + |
| 30 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 31 | + { |
| 32 | + return 'fetchAll' === $methodReflection->getName(); |
| 33 | + } |
| 34 | + |
| 35 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 36 | + { |
| 37 | + $args = $methodCall->getArgs(); |
| 38 | + $defaultReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 39 | + |
| 40 | + $statementType = $scope->getType($methodCall->var); |
| 41 | + |
| 42 | + if ($statementType instanceof GenericObjectType) { |
| 43 | + $genericTypes = $statementType->getTypes(); |
| 44 | + |
| 45 | + if (1 !== \count($genericTypes)) { |
| 46 | + return $defaultReturn; |
| 47 | + } |
| 48 | + |
| 49 | + $fetchType = PDO::FETCH_BOTH; |
| 50 | + if (\count($args) > 0) { |
| 51 | + $fetchModeType = $scope->getType($args[0]->value); |
| 52 | + if (!$fetchModeType instanceof ConstantIntegerType) { |
| 53 | + return $defaultReturn; |
| 54 | + } |
| 55 | + $fetchType = $fetchModeType->getValue(); |
| 56 | + |
| 57 | + if (!\in_array($fetchType, [PDO::FETCH_ASSOC, PDO::FETCH_NUM, PDO::FETCH_BOTH])) { |
| 58 | + return $defaultReturn; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + $resultType = $genericTypes[0]; |
| 63 | + |
| 64 | + if ((PDO::FETCH_NUM === $fetchType || PDO::FETCH_ASSOC === $fetchType) && $resultType instanceof ConstantArrayType) { |
| 65 | + $builder = ConstantArrayTypeBuilder::createEmpty(); |
| 66 | + |
| 67 | + $keyTypes = $resultType->getKeyTypes(); |
| 68 | + $valueTypes = $resultType->getValueTypes(); |
| 69 | + |
| 70 | + foreach ($keyTypes as $i => $keyType) { |
| 71 | + if (PDO::FETCH_NUM === $fetchType && $keyType instanceof ConstantIntegerType) { |
| 72 | + $builder->setOffsetValueType($keyType, $valueTypes[$i]); |
| 73 | + } elseif (PDO::FETCH_ASSOC === $fetchType && $keyType instanceof ConstantStringType) { |
| 74 | + $builder->setOffsetValueType($keyType, $valueTypes[$i]); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return new ArrayType(new IntegerType(), $builder->getArray()); |
| 79 | + } |
| 80 | + |
| 81 | + return new ArrayType(new IntegerType(), $resultType); |
| 82 | + } |
| 83 | + |
| 84 | + return $defaultReturn; |
| 85 | + } |
| 86 | +} |
0 commit comments