You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type mismatch when explicitly setting an array key #11507
Which I am at least familiar with (although I have to admit it is a topic I revisit often...)
I don't (possibly naively) see how it could be relevant though, as non-empty-array<string, string> should be compatible with itself, even when checking for invariance (right?).
I'm also not convinced I've found the simplest example (do I even need the collection and map? could I recreate this with a simple array?), however it is as much progress I have made tonight. Will likely update later this week.
Did PHPStan help you today? Did it make you happy in any way?
Absolutely! The time I've spent debugging my code, and simplying it to create this example has been great fun. I'd love to get into contributing to PHPStan 🙂
@@ @@-PHP 8.0 – 8.3 (1 error)+PHP 8.0 – 8.3 (2 errors)
==========
+33: Method Collection::map() should return static(Collection<TKey of (int|string), TMapValue>) but returns Collection<TKey of (int|string), TMapValue>.
43: Function foo() should return Collection<int, non-empty-array<string, string>> but returns Collection<int, non-empty-array<string, string>>.
-PHP 7.2 – 7.4 (3 errors)+PHP 7.2 – 7.4 (4 errors)
==========
13: Promoted properties are supported only on PHP 8.0 and later.
+33: Method Collection::map() should return static(Collection<TKey of (int|string), TMapValue>) but returns Collection<TKey of (int|string), TMapValue>.
33: Parameter #1 $items of class Collection constructor expects array<TKey of (int|string), TMapValue>, array<TKey of (int|string), TMapValue>|false given.
43: Function foo() should return Collection<int, non-empty-array<string, string>> but returns Collection<int, non-empty-array<string, string>>.
Full report
PHP 8.0 – 8.3 (2 errors)
Line
Error
33
`Method Collection::map() should return static(Collection<TKey of (int
43
Function foo() should return Collection<int, non-empty-array<string, string>> but returns Collection<int, non-empty-array<string, string>>.
PHP 7.2 – 7.4 (4 errors)
Line
Error
13
Promoted properties are supported only on PHP 8.0 and later.
33
`Method Collection::map() should return static(Collection<TKey of (int
33
`Parameter #1 $items of class Collection constructor expects array<TKey of (int
43
Function foo() should return Collection<int, non-empty-array<string, string>> but returns Collection<int, non-empty-array<string, string>>.
Activity
liamduckett commentedon Aug 15, 2024
Hey, a slightly simpler example: https://phpstan.org/r/6e2b5d5a-67a1-47be-9632-ecdf7e4f09d1
phpstan-bot commentedon Sep 16, 2024
@liamduckett After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.0 – 8.3 (2 errors)
Parameter #1 $items of class Collection constructor expects list<TMapValue>, array<int<0, max>, TMapValue> given.Function foo() should return Collection<non-empty-array<string>> but returns Collection<non-empty-array<string>>.PHP 7.2 – 7.4 (3 errors)
Promoted properties are supported only on PHP 8.0 and later.Function foo() should return Collection<non-empty-array<string>> but returns Collection<non-empty-array<string>>.ondrejmirtes commentedon Dec 10, 2024
Deleted the phpstan-bot comments, they weren't correct.
phpstan-bot commentedon Feb 1, 2025
@liamduckett After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.0 – 8.3 (2 errors)
Function foo() should return Collection<int, non-empty-array<string, string>> but returns Collection<int, non-empty-array<string, string>>.PHP 7.2 – 7.4 (4 errors)
Promoted properties are supported only on PHP 8.0 and later.Function foo() should return Collection<int, non-empty-array<string, string>> but returns Collection<int, non-empty-array<string, string>>.phpstan-bot commentedon Feb 1, 2025
@liamduckett After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.0 – 8.3 (3 errors)
Method Collection::map() should return static(Collection<TMapValue>) but returns Collection<TMapValue>.Parameter #1 $items of class Collection constructor expects list<TMapValue>, array<int<0, max>, TMapValue> given.Function foo() should return Collection<non-empty-array<string>> but returns Collection<non-empty-array<string>>.PHP 7.2 – 7.4 (4 errors)
Promoted properties are supported only on PHP 8.0 and later.Method Collection::map() should return static(Collection<TMapValue>) but returns Collection<TMapValue>.Function foo() should return Collection<non-empty-array<string>> but returns Collection<non-empty-array<string>>.liamduckett commentedon Feb 1, 2025
Example that reproduces issue, without other (new) issues:
https://phpstan.org/r/576494fb-e4bc-4920-9b6d-5ee68ab4c167
Fix phpstan/phpstan#11507: false positive for invariant template with…
phpstan-bot commentedon Sep 7, 2026
@liamduckett After the latest push in 2.3.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.0 – 8.4
No errors
PHP 7.2 – 7.4 (2 errors)
Promoted properties are supported only on PHP 8.0 and later.Add regression test for #11507
Add regression test for #11507
Updated PHPStan to commit 87bbf778279a95229ba29b9f9f111d41fcd57742