Skip to content

False positive for trigger_error(E_USER_ERROR) throwing not considered #7799

@thg2k

Description

@thg2k

This is a bit of a nitpick, but should be an easy fix and I actually ran into this.

Please consider the following code:

https://phpstan.org/r/4cc8f7c3-c43f-478d-ad29-dfdb1f168c04

<?php

function foo(): void {
  try {
    trigger_error("hello", E_USER_ERROR);
    print "world\n";
  }
  catch (\Exception $e) {}
  print "reachable\n";
}

It gives the following errors:

  6      Unreachable statement - code above always terminates.
  8      Dead catch - Exception is never thrown in the try block.
  9      Unreachable statement - code above always terminates.

Of those, only the one at the line 6 print "world\n"; is correct, as this
line is indeed not reachable.

But the catch and the subsequent unreachable statement are false positive,
as demonstrated by this snippet:

https://3v4l.org/FERFO

set_error_handler(function($c, $m) {
  print "ERR: $m\n";
  throw new \Exception("foo");
});

foo();

So the idea is that E_USER_ERROR can either terminate or throw.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions