PhpStorm 2024.1 Help

Code Inspection: Result of 'instanceof' is always 'true'

Reports the instanceof expressions whose argument is within the hierarchy of the checked variable. Such expressions will always evaluate to true.

In the following example, the $this variable is in the context of ChildClass, which is a direct subclass of ParentClass. As a result, the instanceof check always evaluates to true and the conditional statement becomes redundant. After the quick-fix is applied, the conditional statement is removed.

class ParentClass {} class ChildClass extends ParentClass { public function doSmth($message) { if ($this instanceof ParentClass) { echo $message; } } }
class ParentClass {} class ChildClass extends ParentClass { public function doSmth($message) { echo $message; } }

Suppress an inspection in the editor

  1. Place the caret at the highlighted line and press Alt+Enter or click the Intention action icon.

  2. Click the arrow next to the inspection you want to suppress and select the necessary suppress action.

Last modified: 11 February 2024