From aa39145a99064358b81e852dc3c70dfaa21a747f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 13 Aug 2019 13:57:37 +0200 Subject: [PATCH 1/3] Implemented `Doctrine\DBAL\Types\Type::getType` handling --- extension.neon | 5 ++ .../GetDBALTypeDynamicReturnTypeExtension.php | 60 +++++++++++++++++++ .../data/dbalTypesTypeStaticGetTypeUsage.php | 20 +++++++ tests/DoctrineIntegration/DBAL/phpstan.neon | 3 + 4 files changed, 88 insertions(+) create mode 100644 src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php create mode 100644 tests/DoctrineIntegration/DBAL/data/dbalTypesTypeStaticGetTypeUsage.php create mode 100644 tests/DoctrineIntegration/DBAL/phpstan.neon diff --git a/extension.neon b/extension.neon index 79e73a3a..6bdfacc1 100644 --- a/extension.neon +++ b/extension.neon @@ -119,6 +119,11 @@ services: arguments: managerClass: Doctrine\Common\Persistence\ObjectManager + dbalTypeGetType: + class: PHPStan\Type\Doctrine\GetDBALTypeDynamicReturnTypeExtension + tags: + - phpstan.broker.dynamicStaticMethodReturnTypeExtension + ## NewExprDynamicReturnTypeExtensions - diff --git a/src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php b/src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php new file mode 100644 index 00000000..6cf94715 --- /dev/null +++ b/src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php @@ -0,0 +1,60 @@ +getName() === 'getType'; + } + + public function getTypeFromStaticMethodCall( + MethodReflection $methodReflection, + StaticCall $methodCall, + Scope $scope + ): Type + { + if (count($methodCall->args) === 0) { + return ParametersAcceptorSelector::selectSingle( + $methodReflection->getVariants() + )->getReturnType(); + } + + $dbalTypeArg = $methodCall->args[0]->value; + return $this->detectReturnType($methodReflection, $dbalTypeArg); + } + + private function detectReturnType(MethodReflection $methodReflection, Expr $dbalTypeArg): Type + { + if ($dbalTypeArg instanceof Expr\ClassConstFetch && $dbalTypeArg->class instanceof Name) { + return new ObjectType($dbalTypeArg->class->toString()); + } + + if ($dbalTypeArg instanceof Scalar\String_) { + return new ObjectType($dbalTypeArg->value); + } + + return ParametersAcceptorSelector::selectSingle( + $methodReflection->getVariants() + )->getReturnType(); + } + +} diff --git a/tests/DoctrineIntegration/DBAL/data/dbalTypesTypeStaticGetTypeUsage.php b/tests/DoctrineIntegration/DBAL/data/dbalTypesTypeStaticGetTypeUsage.php new file mode 100644 index 00000000..c1a00c3a --- /dev/null +++ b/tests/DoctrineIntegration/DBAL/data/dbalTypesTypeStaticGetTypeUsage.php @@ -0,0 +1,20 @@ + Date: Tue, 13 Aug 2019 13:59:08 +0200 Subject: [PATCH 2/3] Added return type support for `Doctrine\DBAL\Types\Type::getType` in `README.md` --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 59f04b3e..93e7ab34 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ This extension provides following features: * Provides correct return type for `Doctrine\ORM\EntityManager::find`, `getReference` and `getPartialReference` when `Foo::class` entity class name is provided as the first argument * Adds missing `matching` method on `Doctrine\Common\Collections\Collection`. This can be turned off by setting `parameters.doctrine.allCollectionsSelectable` to `false`. * Also supports Doctrine ODM. +* Adds `Doctrine\DBAL\Types\Type::getType` return type support. ## Installation From c01a1d6a525f3616d0c062c1d705b8fd9b5d964c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 13 Aug 2019 14:15:37 +0200 Subject: [PATCH 3/3] Removed `::class` usage as composer-require-checker will require the `doctrine/dbal` package then --- composer.json | 3 ++- src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index e2c7ba02..36a1bc4c 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "doctrine/common": "^2.7", "doctrine/orm": "^2.5", "doctrine/collections": "^1.0", - "doctrine/mongodb-odm": "^1.2" + "doctrine/mongodb-odm": "^1.2", + "doctrine/dbal": "^2.9" }, "conflict": { "doctrine/common": "<2.7", diff --git a/src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php b/src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php index 6cf94715..fa1224c1 100644 --- a/src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php +++ b/src/Type/Doctrine/GetDBALTypeDynamicReturnTypeExtension.php @@ -18,7 +18,7 @@ final class GetDBALTypeDynamicReturnTypeExtension implements DynamicStaticMethod public function getClass(): string { - return \Doctrine\DBAL\Types\Type::class; + return 'Doctrine\DBAL\Types\Type'; } public function isStaticMethodSupported(MethodReflection $methodReflection): bool