diff --git a/src/Composer/PHPStan/ConfigReturnTypeExtension.php b/src/Composer/PHPStan/ConfigReturnTypeExtension.php index a1e6359bb..b28d3ebd2 100644 --- a/src/Composer/PHPStan/ConfigReturnTypeExtension.php +++ b/src/Composer/PHPStan/ConfigReturnTypeExtension.php @@ -30,6 +30,7 @@ use PHPStan\Type\MixedType; use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; +use PHPStan\Type\TypeUtils; use PHPStan\Type\UnionType; final class ConfigReturnTypeExtension implements DynamicMethodReturnTypeExtension @@ -64,18 +65,32 @@ final class ConfigReturnTypeExtension implements DynamicMethodReturnTypeExtensio { $args = $methodCall->getArgs(); + $defaultReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + if (count($args) < 1) { - return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + return $defaultReturn; } $keyType = $scope->getType($args[0]->value); - if ($keyType instanceof ConstantStringType) { - if (isset($this->properties[$keyType->getValue()])) { - return $this->properties[$keyType->getValue()]; + if (method_exists($keyType, 'getConstantStrings')) { // @phpstan-ignore-line - depending on PHPStan version, this method will always exist, or not. + $strings = $keyType->getConstantStrings(); + } else { + // for compat with old phpstan versions, we use a deprecated phpstan method. + $strings = TypeUtils::getConstantStrings($keyType); // @phpstan-ignore-line ignore deprecation + } + if ($strings !== []) { + $types = []; + foreach($strings as $string) { + if (!isset($this->properties[$string->getValue()])) { + return $defaultReturn; + } + $types[] = $this->properties[$string->getValue()]; } + + return TypeCombinator::union(...$types); } - return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + return $defaultReturn; } /** diff --git a/tests/Composer/Test/ConfigTest.php b/tests/Composer/Test/ConfigTest.php index e7341d2a1..2a2690765 100644 --- a/tests/Composer/Test/ConfigTest.php +++ b/tests/Composer/Test/ConfigTest.php @@ -398,7 +398,7 @@ class ConfigTest extends TestCase ]; foreach ($keys as $key) { $value = $config->get($key); - $this->assertIsArray($value); + $this->assertIsArray($value); // @phpstan-ignore-line - PHPStan knows that its an array for all given keys $this->assertCount(0, $value); } }