|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace staabm\PHPStanDba\Extensions; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Type\ArrayType; |
| 12 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 13 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 14 | +use PHPStan\Type\IntegerType; |
| 15 | +use PHPStan\Type\MixedType; |
| 16 | +use PHPStan\Type\NullType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\TypeCombinator; |
| 19 | +use PHPStan\Type\UnionType; |
| 20 | +use staabm\PHPStanDba\DibiReflection\DibiReflection; |
| 21 | +use staabm\PHPStanDba\QueryReflection\QueryReflection; |
| 22 | +use staabm\PHPStanDba\QueryReflection\QueryReflector; |
| 23 | +use staabm\PHPStanDba\UnresolvableQueryException; |
| 24 | + |
| 25 | +final class DibiConnectionFetchDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 26 | +{ |
| 27 | + public function getClass(): string |
| 28 | + { |
| 29 | + return \Dibi\Connection::class; |
| 30 | + } |
| 31 | + |
| 32 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 33 | + { |
| 34 | + return \in_array($methodReflection->getName(), [ |
| 35 | + 'fetch', |
| 36 | + 'fetchAll', |
| 37 | + 'fetchPairs', |
| 38 | + 'fetchSingle', |
| 39 | + ], true); |
| 40 | + } |
| 41 | + |
| 42 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type |
| 43 | + { |
| 44 | + $args = $methodCall->getArgs(); |
| 45 | + |
| 46 | + if (\count($args) < 1) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + if ($scope->getType($args[0]->value) instanceof MixedType) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + return $this->inferType($methodReflection, $args[0]->value, $scope); |
| 56 | + } catch (UnresolvableQueryException $exception) { |
| 57 | + // simulation not possible.. use default value |
| 58 | + } |
| 59 | + |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + private function inferType(MethodReflection $methodReflection, Expr $queryExpr, Scope $scope): ?Type |
| 64 | + { |
| 65 | + $queryReflection = new QueryReflection(); |
| 66 | + $queryStrings = $queryReflection->resolveQueryStrings($queryExpr, $scope); |
| 67 | + |
| 68 | + return $this->createFetchType($queryStrings, $methodReflection); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param iterable<string> $queryStrings |
| 73 | + */ |
| 74 | + private function createFetchType(iterable $queryStrings, MethodReflection $methodReflection): ?Type |
| 75 | + { |
| 76 | + $queryReflection = new QueryReflection(); |
| 77 | + $dibiReflection = new DibiReflection(); |
| 78 | + |
| 79 | + $fetchTypes = []; |
| 80 | + foreach ($queryStrings as $queryString) { |
| 81 | + $queryString = $dibiReflection->rewriteQuery($queryString); |
| 82 | + if (null === $queryString) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $resultType = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_ASSOC); |
| 87 | + |
| 88 | + if (null === $resultType) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + $fetchResultType = $this->reduceResultType($methodReflection, $resultType); |
| 93 | + if (null === $fetchResultType) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + $fetchTypes[] = $fetchResultType; |
| 98 | + } |
| 99 | + |
| 100 | + if (\count($fetchTypes) > 1) { |
| 101 | + return TypeCombinator::union(...$fetchTypes); |
| 102 | + } |
| 103 | + if (1 === \count($fetchTypes)) { |
| 104 | + return $fetchTypes[0]; |
| 105 | + } |
| 106 | + |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + private function reduceResultType(MethodReflection $methodReflection, Type $resultType): ?Type |
| 111 | + { |
| 112 | + $methodName = $methodReflection->getName(); |
| 113 | + |
| 114 | + if ('fetch' === $methodName) { |
| 115 | + return new UnionType([new NullType(), $resultType]); |
| 116 | + } elseif ('fetchAll' === $methodName) { |
| 117 | + return new ArrayType(new IntegerType(), $resultType); |
| 118 | + } elseif ('fetchPairs' === $methodName && $resultType instanceof ConstantArrayType && 2 === \count($resultType->getValueTypes())) { |
| 119 | + return new ArrayType($resultType->getValueTypes()[0], $resultType->getValueTypes()[1]); |
| 120 | + } elseif ('fetchSingle' === $methodName && $resultType instanceof ConstantArrayType && 1 === \count($resultType->getValueTypes())) { |
| 121 | + return new UnionType([new NullType(), $resultType->getValueTypes()[0]]); |
| 122 | + } |
| 123 | + |
| 124 | + return null; |
| 125 | + } |
| 126 | +} |
0 commit comments