Skip to content

Commit

Permalink
Do not let string subtypes subsume class-strings (#5497)
Browse files Browse the repository at this point in the history
Previously, Psalm would treat unions like `class-string|numeric-string`
as `numeric-string`, while the only case when string should subsume
`class-string` is when we're combining `class-string` with non-specific
`string`.

Fixes #5491
  • Loading branch information
weirdan authored Mar 29, 2021
1 parent 9a714b7 commit ec7de89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Type/TypeCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Psalm\Type\Atomic\TTraitString;
use Psalm\Type\Atomic\TTrue;
use Psalm\Type\Union;

use function array_filter;
use function array_intersect_key;
use function array_keys;
Expand Down Expand Up @@ -280,7 +281,10 @@ public static function combine(
}
}

if (!isset($combination->value_types['string'])) {
$has_non_specific_string = isset($combination->value_types['string'])
&& get_class($combination->value_types['string']) === Type\Atomic\TString::class;

if (!$has_non_specific_string) {
$object_type = self::combine(
array_values($combination->class_string_types),
$codebase
Expand Down
28 changes: 28 additions & 0 deletions tests/TypeCombinationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,34 @@ public function providerTestValidTypeCombination(): array
'Exception::class',
],
],
'combineClassStringWithNumericString' => [
'class-string|numeric-string',
[
'class-string',
'numeric-string',
],
],
'combineRefinedClassStringWithNumericString' => [
'class-string<Exception>|numeric-string',
[
'class-string<Exception>',
'numeric-string',
],
],
'combineClassStringWithTraitString' => [
'class-string|trait-string',
[
'class-string',
'trait-string',
],
],
'combineRefinedClassStringWithTraitString' => [
'class-string<Exception>|trait-string',
[
'class-string<Exception>',
'trait-string',
],
],
'combineCallableAndCallableString' => [
'callable',
[
Expand Down

0 comments on commit ec7de89

Please sign in to comment.