vimeo / psalm

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

hrtime return type issue on PHP 8.0 if phpVersion is set to < 7.3 #5033

Open kelunik opened 3 years ago

kelunik commented 3 years ago

https://github.com/amphp/amp/runs/1714053018?check_suite_focus=true#step:9:14

When run on PHP 8.0, an error is reported, because Psalm thinks the return type of hrtime(false) is int instead of int[]. But this only happens if the phpVersion in psalm.xml is set below 7.3. Works fine on PHP 7.4.

Unrelated to the hrtime issue, I had to somehow add the function files to <stubs /> when upgrading to Psalm 4.x for some reason.

<stubs>
    <file name="lib/functions.php" />
    <file name="lib/Internal/functions.php" />
</stubs>
psalm-github-bot[bot] commented 3 years ago

Hey @kelunik, can you reproduce the issue on https://psalm.dev ?

orklah commented 3 years ago

@weirdan Can you take at look at this one?

https://psalm.dev/r/fb5c5555dc https://psalm.dev/r/fb5c5555dc?php=7.2

I think this could be fixed by conditional execution again. The error reported by psalm is not really relevant because the code only use the function when it's available

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

I found these snippets:

https://psalm.dev/r/fb5c5555dc ```php = 70300 ? \hrtime(false)[0] : \time(); /** @psalm-trace $_startTime */ ``` ``` Psalm output (using commit ef0d19e): INFO: Trace - 4:32 - $_startTime: int ```
https://psalm.dev/r/fb5c5555dc ```php = 70300 ? \hrtime(false)[0] : \time(); /** @psalm-trace $_startTime */ ``` ``` Psalm output (using commit ef0d19e): INFO: MixedArrayAccess - 3:42 - Cannot access array value on mixed variable INFO: Trace - 4:32 - $_startTime: int|mixed ```
weirdan commented 3 years ago

I think this could be fixed by conditional execution again.

Should be easy, just make the body of this if unconditional:

https://github.com/vimeo/psalm/blob/ef0d19ed9fc66152e48229d4a0d305f19d1a6aa2/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php#L243