|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Doctrine\DBAL; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Driver\Result as DriverResult; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Doctrine\Driver\DriverDetector; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 12 | +use PHPStan\Reflection\ReflectionProvider; |
| 13 | +use PHPStan\Type\Doctrine\ObjectMetadataResolver; |
| 14 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 15 | +use PHPStan\Type\Type; |
| 16 | + |
| 17 | +class RowCountMethodDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 18 | +{ |
| 19 | + |
| 20 | + /** @var string */ |
| 21 | + private $class; |
| 22 | + |
| 23 | + /** @var ObjectMetadataResolver */ |
| 24 | + private $objectMetadataResolver; |
| 25 | + |
| 26 | + /** @var DriverDetector */ |
| 27 | + private $driverDetector; |
| 28 | + |
| 29 | + /** @var ReflectionProvider */ |
| 30 | + private $reflectionProvider; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + string $class, |
| 34 | + ObjectMetadataResolver $objectMetadataResolver, |
| 35 | + DriverDetector $driverDetector, |
| 36 | + ReflectionProvider $reflectionProvider |
| 37 | + ) |
| 38 | + { |
| 39 | + $this->class = $class; |
| 40 | + $this->objectMetadataResolver = $objectMetadataResolver; |
| 41 | + $this->driverDetector = $driverDetector; |
| 42 | + $this->reflectionProvider = $reflectionProvider; |
| 43 | + } |
| 44 | + |
| 45 | + public function getClass(): string |
| 46 | + { |
| 47 | + return $this->class; |
| 48 | + } |
| 49 | + |
| 50 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 51 | + { |
| 52 | + return $methodReflection->getName() === 'rowCount'; |
| 53 | + } |
| 54 | + |
| 55 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type |
| 56 | + { |
| 57 | + $objectManager = $this->objectMetadataResolver->getObjectManager(); |
| 58 | + if (!$objectManager instanceof EntityManagerInterface) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + $connection = $objectManager->getConnection(); |
| 63 | + $driver = $this->driverDetector->detect($connection); |
| 64 | + if ($driver === null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + $resultClass = $this->getResultClass($driver); |
| 69 | + if ($resultClass === null) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + if (!$this->reflectionProvider->hasClass($resultClass)) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + $resultReflection = $this->reflectionProvider->getClass($resultClass); |
| 78 | + if (!$resultReflection->hasNativeMethod('rowCount')) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + $rowCountMethod = $resultReflection->getNativeMethod('rowCount'); |
| 83 | + $variant = ParametersAcceptorSelector::selectSingle($rowCountMethod->getVariants()); |
| 84 | + |
| 85 | + return $variant->getReturnType(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param DriverDetector::* $driver |
| 90 | + * @return class-string<DriverResult>|null |
| 91 | + */ |
| 92 | + private function getResultClass(string $driver): ?string |
| 93 | + { |
| 94 | + switch ($driver) { |
| 95 | + case DriverDetector::IBM_DB2: |
| 96 | + return 'Doctrine\DBAL\Driver\IBMDB2\Result'; |
| 97 | + case DriverDetector::MYSQLI: |
| 98 | + return 'Doctrine\DBAL\Driver\Mysqli\Result'; |
| 99 | + case DriverDetector::OCI8: |
| 100 | + return 'Doctrine\DBAL\Driver\OCI8\Result'; |
| 101 | + case DriverDetector::PDO_MYSQL: |
| 102 | + case DriverDetector::PDO_OCI: |
| 103 | + case DriverDetector::PDO_PGSQL: |
| 104 | + case DriverDetector::PDO_SQLITE: |
| 105 | + case DriverDetector::PDO_SQLSRV: |
| 106 | + return 'Doctrine\DBAL\Driver\PDO\Result'; |
| 107 | + case DriverDetector::PGSQL: |
| 108 | + return 'Doctrine\DBAL\Driver\PgSQL\Result'; |
| 109 | + case DriverDetector::SQLITE3: |
| 110 | + return 'Doctrine\DBAL\Driver\SQLite3\Result'; |
| 111 | + case DriverDetector::SQLSRV: |
| 112 | + return 'Doctrine\DBAL\Driver\SQLSrv\Result'; |
| 113 | + } |
| 114 | + |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments