PHP inspection examples
Unresolved Include
This inspection detects attempts to include not actually existing files and suggests to create a file with the specified name.
Dynamic method is called as static
This inspection checks whether a call to a static function is actually applied to a static function.
The function do_something()
is called as static while actually it is dynamic; the inspection provides a quick-fix suggesting to make the call dynamic.
Unimplemented abstract method in class
This inspection checks whether classes inherited from abstract superclasses are either explicitly declared as abstract or the methods inherited from the superclass are implemented and suggests a quick-fix for auto-generating the methods' stubs.
The ExampleClass
class is inherited from AbstractClass
and has not been explicitly declared as abstract. At the same time, the GetValue()
function, which is inherited from AbstractClass
, has not been implemented.
Parameter type
The inspection detects the parameters passed to a function call, whose types do not match the ones specified in the function definition (via type declaration or the PHPDoc @param tag).
The expectArray
function has the parameter of the array
type but is called with a string
.
Strict type checking rules violation
The inspection reports violations of the strict type checking rules. In strict mode, only variables exactly matching the declaration types are accepted.
Customize the Strict type checking rules violation inspection
By default, the inspection operates on a per-file basis: it is enabled for files with the declare(strict_types=1);
directive specified. You can also enable it for all files.
-
In the Settings/Preferences dialog (Ctrl+Alt+S), go to .
Select the Strict type checking rules violation inspection in the list.
In the Options area, select the Enable for all files checkbox.
Undefined class constant
This inspection detects references to constants that are not actually defined in the specified class, and suggests adding a missing constant.
The NonExistingConstant
constant is referenced the ExampleClass
class constant, while actually it is not defined in this class.
Undefined constant
This inspection detects references to constants that are not defined anywhere within the inspection scope.
The NonExistingConstant
is not defined anywhere within the inspection scope.
Undefined class
This inspection detects references to classes that are not defined anywhere within the inspection scope.
The MyClass
referenced class is not defined.
Undefined field
This inspection detects references to class fields that are not actually defined in this class and suggests adding them.
The $example
variable is an instance of ExampleClass
. The $var
declaration references the field
field of ExampleClass
, which is not defined in the class.
In the PHP context, the Undefined field and Undefined method inspections may erroneously report severe problems when actually no problems take place. This happens when you attempt to access or assign a value to a property, or to call a method that is not explicitly defined, while the referenced class contains the __get()
, __set()
, or __call()
magic methods. No errors should be reported in such cases because these methods are invoked every time an undefined property or method is referenced, however, PhpStorm still treats them as errors or warnings, depending on the severity level you have specified for the inspection.
Suppress reporting for Undefined field errors
-
In the Settings/Preferences dialog (Ctrl+Alt+S), go to .
Select the Undefined field inspection in the list.
In the Options area, clear the Notify about access to a field via magic method and Notify about PHP dynamic field declaration checkboxes.
When the checkboxes are selected, PhpStorm reports errors even if the class contains the __get()
and __set()
magic methods.
Adjust reporting for Undefined method errors
-
In the Settings/Preferences dialog (Ctrl+Alt+S), go to .
Select the Undefined method inspection in the list.
In the Options area, select the Downgrade severity if __magic methods are present in class checkbox.
Undefined methods will now be indicated with the severity level that is one step lower than specified for inspections in general (by default, Info instead of Warning).
Call of undefined function
This inspection detects references to functions that are not defined anywhere within the inspection scope and suggests importing them.
The undefined_function()
is not defined anywhere within the inspection scope.
Undefined variable
This inspection detects references to variables that are not declared and initialized anywhere within the inspection scope. PHP does not require that each variable should be declared and initialized. PHP can initialize such variable on the fly and assign it the zero value. However, this inspection allows you to detect discrepancies of this kind.
Configure reporting for Undefined variable errors
-
In the Settings/Preferences dialog (Ctrl+Alt+S), go to .
Select the Undefined field inspection in the list.
-
In the Options area, configure the inspection as follows:
-
Enable inspection in global space: select this checkbox to run the inspection against variables outside functions/methods, classes, and namespaces, that is, in the global space.
-
Report that variable might have not been defined: select this checkbox to have an error reported even if the definition of a variable is not definitely missing. Such situation may take place when a variable is used in several paths and some of them may never be reached, for example, in
if()
statements: Ignore 'include' and 'require' statements: Suppose the inspection scope contains an
include
orrequire
statement. If this checkbox is cleared, PhpStorm treats an undefined variable as defined in the classes referenced through these statements and no error is reported. If this checkbox is selected, an Undefined variable error is reported.
-