PHPStan: Fixed handling of union types in ConfigReturnTypeExtension (#11312)
Co-authored-by: Markus Staab <m.staab@complex-it.de>pull/11334/head
parent
933ca81d6b
commit
d8221bd443
|
@ -30,6 +30,7 @@ use PHPStan\Type\MixedType;
|
||||||
use PHPStan\Type\StringType;
|
use PHPStan\Type\StringType;
|
||||||
use PHPStan\Type\Type;
|
use PHPStan\Type\Type;
|
||||||
use PHPStan\Type\TypeCombinator;
|
use PHPStan\Type\TypeCombinator;
|
||||||
|
use PHPStan\Type\TypeUtils;
|
||||||
use PHPStan\Type\UnionType;
|
use PHPStan\Type\UnionType;
|
||||||
|
|
||||||
final class ConfigReturnTypeExtension implements DynamicMethodReturnTypeExtension
|
final class ConfigReturnTypeExtension implements DynamicMethodReturnTypeExtension
|
||||||
|
@ -64,18 +65,32 @@ final class ConfigReturnTypeExtension implements DynamicMethodReturnTypeExtensio
|
||||||
{
|
{
|
||||||
$args = $methodCall->getArgs();
|
$args = $methodCall->getArgs();
|
||||||
|
|
||||||
|
$defaultReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
|
||||||
|
|
||||||
if (count($args) < 1) {
|
if (count($args) < 1) {
|
||||||
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
|
return $defaultReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
$keyType = $scope->getType($args[0]->value);
|
$keyType = $scope->getType($args[0]->value);
|
||||||
if ($keyType instanceof ConstantStringType) {
|
if (method_exists($keyType, 'getConstantStrings')) { // @phpstan-ignore-line - depending on PHPStan version, this method will always exist, or not.
|
||||||
if (isset($this->properties[$keyType->getValue()])) {
|
$strings = $keyType->getConstantStrings();
|
||||||
return $this->properties[$keyType->getValue()];
|
} 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -398,7 +398,7 @@ class ConfigTest extends TestCase
|
||||||
];
|
];
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
$value = $config->get($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);
|
$this->assertCount(0, $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue