From 803682ab1e5175b2efdf427236713c54d00af454 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 13 Oct 2019 01:10:36 +0200 Subject: [PATCH] phpstan - add getContainer() after boot() --- .../config/phpstan/type-extensions.neon | 4 ++ ...tContainerAfterBootReturnTypeExtension.php | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 packages/NodeTypeResolver/src/Type/TypeExtension/KernelGetContainerAfterBootReturnTypeExtension.php diff --git a/packages/NodeTypeResolver/config/phpstan/type-extensions.neon b/packages/NodeTypeResolver/config/phpstan/type-extensions.neon index f62eb1df9c8..292ee8925ee 100644 --- a/packages/NodeTypeResolver/config/phpstan/type-extensions.neon +++ b/packages/NodeTypeResolver/config/phpstan/type-extensions.neon @@ -2,3 +2,7 @@ services: - class: Rector\NodeTypeResolver\Type\TypeExtension\StaticContainerGetDynamicMethodReturnTypeExtension tags: [phpstan.broker.dynamicMethodReturnTypeExtension] + + - + class: Rector\NodeTypeResolver\Type\TypeExtension\KernelGetContainerAfterBootReturnTypeExtension + tags: [phpstan.broker.dynamicMethodReturnTypeExtension] diff --git a/packages/NodeTypeResolver/src/Type/TypeExtension/KernelGetContainerAfterBootReturnTypeExtension.php b/packages/NodeTypeResolver/src/Type/TypeExtension/KernelGetContainerAfterBootReturnTypeExtension.php new file mode 100644 index 00000000000..6dbaffdbe8d --- /dev/null +++ b/packages/NodeTypeResolver/src/Type/TypeExtension/KernelGetContainerAfterBootReturnTypeExtension.php @@ -0,0 +1,56 @@ +getName() === 'getContainer'; + } + + public function getTypeFromMethodCall( + MethodReflection $methodReflection, + MethodCall $methodCall, + Scope $scope + ): Type { + $returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + + if ($this->isCalledAfterBoot($scope, $methodCall) === false) { + return $returnType; + } + + if ($returnType instanceof UnionType) { + foreach ($returnType->getTypes() as $singleType) { + if ($singleType instanceof ObjectType) { + return $singleType; + } + } + } + + return $returnType; + } + + private function isCalledAfterBoot(Scope $scope, MethodCall $methodCall): bool + { + $kernelBootMethodCall = new MethodCall($methodCall->var, 'boot'); + + return ! $scope->getType($kernelBootMethodCall) instanceof ErrorType; + } +}