-
Notifications
You must be signed in to change notification settings - Fork 555
Fix phpstan/phpstan#8031: Union of constant string type lost when used with generics. #5167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.1.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
|
|
||
| $arr = new ArrayObject(['a' => 1, 'b' => 2]); | ||
|
|
||
| assertType('ArrayObject<string, int>', $arr); // correctly inferred as `ArrayObject<string, int>` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dunno if this will create issues...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure either. related issue was comment to be blocked on bidrectional narrowing: |
||
| assertType("ArrayObject<'a'|'b', int>", $arr); | ||
|
|
||
| $a = $arr['a']; // ok | ||
| $b = $arr['b']; // ok | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug8031; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @template TKey of array-key | ||
| * @template TValue of mixed | ||
| */ | ||
| class Collection | ||
| { | ||
| /** | ||
| * @param array<TKey, TValue> $val | ||
| */ | ||
| public function __construct(protected array $val) {} | ||
| } | ||
|
|
||
| /** | ||
| * @return Collection<'one'|'two', int> | ||
| */ | ||
| function test(): Collection | ||
| { | ||
| $c = new Collection([ | ||
| 'one' => 1, | ||
| 'two' => 2, | ||
| ]); | ||
| assertType("Bug8031\Collection<'one'|'two', int>", $c); | ||
| return $c; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont see a reason to have a different behavior for int and string