Code Inspection: Result of 'instanceof' is always 'true'
Configure inspections: Settings | Editor | Inspections
Show intention actions: AltEnter
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.
Before the quick-fix
class ParentClass {}class ChildClass extends ParentClass { public function doSmth($message) { if ($this instanceof ParentClass) { echo $message; } }}
After the quick-fix
class ParentClass {}class ChildClass extends ParentClass { public function doSmth($message) { echo $message; }}
Place the caret at the highlighted line and press AltEnter or click
.
Click the arrow next to the inspection you want to suppress and select the necessary suppress action.