Phpinspectionsea: [Bug] Suggested fix in "General '\Exception' is thrown" is wrong if Exception is imported via namespace

Created on 7 Oct 2018  路  2Comments  路  Source: kalessil/phpinspectionsea

| Subject | Details |
| :------------- | :---------------------------------------------------------------------------- |
| Issue type | Bug |
| Plugin | Php Inspections (EA Extended), 3.0.6.1 |
| Language level | PHP 7.2 |

Current behaviour:

Here is some example code:

namespace Demo\Test;

use Exception;

class Demo
{
    public function illustrateBugReport()
    {
        throw new Exception('Invalid context specified.');
    }
}

When using the suggested fix from the "General '\Exception' is thrown" inspection, the line that throws the exception gets replaced by this:

throw new RuntimeException('Invalid context specified.');

But obviously this will break the code, as Demo\Test\RuntimeException does not exist (class with a name in that namespace).

Expected behaviour:

The correct fix would be to either use the fully qualified name for RuntimeException here, that would be:

throw new \RuntimeException('Invalid context specified.');

OR alternatively import RuntimeException as well. Like this:

namespace Demo\Test;

use RuntimeException;

class Demo
{
    public function illustrateBugReport()
    {
        throw new RuntimeException('Invalid context specified.');
    }
}
bug / false-positive fixed

All 2 comments

Confirmed, the easiest way will be to follow throw new \RuntimeException(...); approach. Thank you for reporting @andreasschroth.

Fixed!

Was this page helpful?
0 / 5 - 0 ratings