phpstan
phpstan copied to clipboard
False positive: offset 1 on array does not exist
Bug report
PHPStan versions starting with 1.10.11 reports the error Offset 1 on array<0, array{name: string, lines: non-empty-array<int<0, max>, string>}> in isset() does not exist. for the example code snippet. Version 1.10.10 does not suffer from this issue.
Code snippet that reproduces the problem
https://phpstan.org/r/fca2eeda-2636-47d5-9ba5-4c501ec5e9b5
Expected output
No errors reported.
Did PHPStan help you today? Did it make you happy in any way?
It sure did! It caught a couple of other edge cases that would have slipped through otherwise.
@EreMaijala After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:
@@ @@
31: Offset 1 on array<0, array{name: string, lines: non-empty-array<int<0, max>, string>}> in isset() does not exist.
-PHP 7.2 – 7.4 (3 errors)
+PHP 7.2 – 7.4 (1 error)
==========
-14: Function str_starts_with not found.
-15: Function str_ends_with not found.
31: Offset 1 on array<0, array{name: (string|false), lines: non-empty-array<int<0, max>, string>}> in isset() does not exist.
Full report
PHP 8.0 – 8.2 (1 error)
| Line | Error |
|---|---|
| 31 | Offset 1 on array<0, array{name: string, lines: non-empty-array<int<0, max>, string>}> in isset() does not exist. |
PHP 7.2 – 7.4 (1 error)
| Line | Error |
|---|---|
| 31 | `Offset 1 on array<0, array{name: (string |
I get more or less the same error on this:
if (count($spanContents) == 4) {
$servings = (int) $spanContents[2];
}
my error is :
Offset 2 does not exist on non-empty-array<0, string>.
@rabol please provide a snippet on phpstan.org/try which shows your case, otherwise we cannot judge whether its a bug or not.
https://phpstan.org/r/d175b0b8-ce2e-4fd0-8a85-cce71467ce22
@rabol the by-reference stuff is hard to figure out for PHPStan. you could give it a /** @var list<string> */ hint and the error goes away:
https://phpstan.org/r/eb036b60-2ae9-4600-a41d-6e53e9d2458a
adding /** @var list<string> */ does not work
sorry, my mistake /** @var list<string> $spanContents */ works
@EreMaijala After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
@@ @@
PHP 8.0 – 8.2 (1 error)
==========
-31: Offset 1 on array<0, array{name: string, lines: non-empty-array<int<0, max>, string>}> in isset() does not exist.
+31: Offset 1 on array<0, array{name: string, lines: non-empty-list<string>}>&list in isset() does not exist.
-PHP 7.2 – 7.4 (3 errors)
+PHP 7.2 – 7.4 (1 error)
==========
-14: Function str_starts_with not found.
-15: Function str_ends_with not found.
-31: Offset 1 on array<0, array{name: (string|false), lines: non-empty-array<int<0, max>, string>}> in isset() does not exist.
+31: Offset 1 on array<0, array{name: (string|false), lines: non-empty-list<string>}>&list in isset() does not exist.
Full report
PHP 8.0 – 8.2 (1 error)
| Line | Error |
|---|---|
| 31 | Offset 1 on array<0, array{name: string, lines: non-empty-list<string>}>&list in isset() does not exist. |
PHP 7.2 – 7.4 (1 error)
| Line | Error |
|---|---|
| 31 | `Offset 1 on array<0, array{name: (string |
@rabol After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
@@ @@
10: Call to method filter() on an unknown class Response.
10: Parameter $span of anonymous function has invalid type Crawler.
12: Call to method text() on an unknown class Crawler.
-15: Offset 2 does not exist on non-empty-array<0, mixed>.
+15: Offset 2 does not exist on non-empty-array<0, mixed>&list.
Full report
| Line | Error |
|---|---|
| 5 | Parameter $response of method HelloWorld::sayHello() has invalid type Response. |
| 10 | Call to method filter() on an unknown class Response. |
| 10 | Parameter $span of anonymous function has invalid type Crawler. |
| 12 | Call to method text() on an unknown class Crawler. |
| 15 | Offset 2 does not exist on non-empty-array<0, mixed>&list. |
@EreMaijala After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
@@ @@
PHP 8.0 – 8.2 (1 error)
==========
-31: Offset 1 on array<0, array{name: string, lines: non-empty-array<int<0, max>, string>}> in isset() does not exist.
+31: Offset 1 on list<array{name: string, lines: non-empty-list<string>}> in isset() does not exist.
-PHP 7.2 – 7.4 (3 errors)
+PHP 7.2 – 7.4 (1 error)
==========
-14: Function str_starts_with not found.
-15: Function str_ends_with not found.
-31: Offset 1 on array<0, array{name: (string|false), lines: non-empty-array<int<0, max>, string>}> in isset() does not exist.
+31: Offset 1 on list<array{name: (string|false), lines: non-empty-list<string>}> in isset() does not exist.
Full report
PHP 8.0 – 8.2 (1 error)
| Line | Error |
|---|---|
| 31 | Offset 1 on list<array{name: string, lines: non-empty-list<string>}> in isset() does not exist. |
PHP 7.2 – 7.4 (1 error)
| Line | Error |
|---|---|
| 31 | `Offset 1 on list<array{name: (string |