psalm: UnusedVariable false positive on if-elseif-else with exception

See https://psalm.dev/r/5b6314611b

The throw line seems to be the culprit. If you comment out the line, the warning goes away.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 18

Most upvoted comments

I mean, /** @var string|null $foo */ is a variable (+type) declaration

It may look like one, but it actually is not (as in ‘Psalm doesn’t do this’). Psalm interprets it as ‘the value that variable $foo > holds at this point has the type string|null’.

I appreciate you comment. The above makes sense and help me to understand how the tool works. To me this issue can be closed.

True you are using foo on line 17 in your elseif example, and on line 12 in the other example.

However the variable definition of line 4 is overrwitten with an value or there is an exception thrown so execution stops.

So following the if else example.

line 4 assign null to foo

  • if executed, then foo is overwriten with ‘bar’ and the echo will be ‘bar’
  • else executed, then an exception is thrown en execution stops.

So the assignment of line 4 is not needed and can safely be removed