If you are using PHPStorm for any significant amount of time you must have run into this problem:

You copy/paste a snippet of code into a new class and a perfectly working piece of code throws exceptions when you run it. Of course, you have once again forgotten to import classes that snippet of code uses.

PHPStorm gives us the brilliant ability to import the class just by putting our text cursor on it and pressing ALT + Enter. However, for some reason, it doesn’t underline the classes that are not imported even if it’s certain they will throw an error.

You will just see perfectly correct looking code and find out about the error only after testing it. This makes little sense for such a small avoidable error. PHPStorm’s extensive configuration allows us to fix this unintuitive behavior.

Enabling Errors for Undefined Classes

To make PHPStorm show errors on undefined classes by underlining faulty pieces of code we need to follow these steps:

  1.  Open up Preferences
  2. Navigate to Inspections in the sidebar
  3. Select the Default profile up top to make your changes work across all projects
  4. Open the PHP menu and select the Undefined item
  5. Set Severity on the right to Error

You will end up with settings looking like this: 

That’s it!  Every time you forget to import a class, it will be underlined in red making you handle the error before you even get to the testing phase.

Leave a Reply