From 0cd02f62575b167b01ab33a610e788a6f8b11353 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 09:26:31 +0100 Subject: [PATCH] added phpstan ConfigReturnTypeExtension --- phpstan/config.neon | 6 +++ .../PHPStan/ConfigReturnTypeExtension.php | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/Composer/PHPStan/ConfigReturnTypeExtension.php diff --git a/phpstan/config.neon b/phpstan/config.neon index 5ac2cd37b..f09a2e4b7 100644 --- a/phpstan/config.neon +++ b/phpstan/config.neon @@ -58,3 +58,9 @@ parameters: - Composer\Composer::VERSION - Composer\Composer::RELEASE_DATE - Composer\Composer::SOURCE_VERSION + +services: + - + class: Composer\PHPStan\ConfigReturnTypeExtension + tags: + - phpstan.broker.dynamicMethodReturnTypeExtension diff --git a/src/Composer/PHPStan/ConfigReturnTypeExtension.php b/src/Composer/PHPStan/ConfigReturnTypeExtension.php new file mode 100644 index 000000000..73f1764bc --- /dev/null +++ b/src/Composer/PHPStan/ConfigReturnTypeExtension.php @@ -0,0 +1,50 @@ +getName()) === 'get'; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type + { + $args = $methodCall->getArgs(); + + if (count($args) < 1) { + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + } + + $keyType = $scope->getType($args[0]->value); + if ($keyType instanceof ConstantStringType) { + if ($keyType->getValue() == 'allow-plugins') { + return TypeCombinator::addNull( + new ArrayType(new StringType(), new BooleanType()) + ); + } + } + + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + } +}