vimeo / psalm

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

"false cannot be cast to int" with truthy-string (Can't repro in sandbox) #11080

Open jnvsor opened 3 weeks ago

jnvsor commented 3 weeks ago

Tested with both 5.x-dev and dev-master (Psalm dev-master@16b24bdc94e052b5ce69fd232a77416a1f6ec3e6)

I have an error that I can't reproduce in the sandbox. Inside an if that checks a false|string I cast it to int and it fails:

ERROR: Trace - src/Renderer/CliRenderer.php:99:21 - $tput: false|string (see https://psalm.dev/224)
                    /** @psalm-trace $tput */
                    if ((bool) $tput) {
                        self::$terminal_width = (int) $tput;
                    }

ERROR: InvalidCast - src/Renderer/CliRenderer.php:100:55 - false cannot be cast to int (see https://psalm.dev/103)
                        self::$terminal_width = (int) $tput;

Two issues with this:

  1. At the time of the cast it must be a truthy-string
  2. You absolutely can cast false to an int, and I'm not sure why you'd want to dissuade this

The code in question is here with the CI failure here

Not sure if you can see the output from GHA but it's just the false cannot be cast to int. All other errors are suppressed or baselined

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

Hey @jnvsor, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.