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(); + } +}