vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.55k stars 660 forks source link

False positive DocblockTypeContradiction on a key saying that the key is always non-empty #7927

Open white43 opened 2 years ago

white43 commented 2 years ago

I've found several cases of the same false positive error. Pay attention to the last part with isset instead of empty, since somehow it works asexpected.

https://psalm.dev/r/24b8f380fb

psalm-github-bot[bot] commented 2 years ago

I found these snippets:

https://psalm.dev/r/24b8f380fb ```php ERROR: DocblockTypeContradiction - 8:11 - Docblock-defined type stdClass for $a['b'] is always non-empty INFO: Trace - 9:27 - $a: array{a: stdClass} ERROR: DocblockTypeContradiction - 15:24 - Docblock-defined type stdClass for $a1['b'] is always non-empty INFO: Trace - 16:28 - $a1: array INFO: Trace - 23:28 - $a2: array INFO: Trace - 27:24 - $a2: array{a: stdClass} ERROR: DocblockTypeContradiction - 29:5 - Docblock-defined type stdClass for $a2['b'] is always non-empty INFO: Trace - 30:28 - $a2: array{a: stdClass} INFO: Trace - 37:27 - $b: array INFO: Trace - 39:27 - $b: array{a: stdClass} ```
white43 commented 2 years ago

git bisect point to this commit 5f17ea9b63785d12af05363f3148ec4e8e660aa2

white43 commented 2 years ago

I'm not sure if it's a good solution, but setting possibly_undefined to true for non-existent array properties in getValueForKey does help; all tests are still green. If that's ok, I can open a PR. :smile:

https://github.com/vimeo/psalm/blob/9d225086e9294cd311d032f8715e27319883614f/src/Psalm/Type/Reconciler.php#L685-L688

orklah commented 2 years ago

Please do :)