3

With PHP, static method can be used in both static method and non-static method, and non-static method can only used in non-static method. That's why calling a dynamic method statically generates the E_STRICT error.

For example:

<?php

class Example
{
    public function foo() {
        return "Foo";
    }

    public static function bar() {
        return "Bar";
    }
}

$ex = new Example();

// Non-static call
echo $ex->bar();

// Static call on a non-static method
// PHP Error "Strict standards: Non-static method should not be called statically"
// ERROR NOT DETECTED BY PHPSTORM!
echo Example::foo();

The last line will generated this PHP error (it's logic): enter image description here

I am currently working on a large PHP application that calls, in some PHP files, non-static methods statically. It was not a problem with an very old version of PHP but we have decided to migrate to the latest PHP version.

Manually check all the project files to identify this bad syntax will be too long (+ 1000 files)!

The built-in code inspection features of PhpStorm doesn't detect this type of error within the analyzed source code. Why? Should I configure something? How?

Below, my PHP code inspection configuration in PhpStorm:

enter image description here

Thanks!

Guicara
  • 1,668
  • 2
  • 20
  • 34
  • 1
    Works fine: http://postimg.org/image/y82q2zzuj/ 1) Try `Code | Inspect Code...` on this file; 2) or even `File | Invalidate Caches...` and restart IDE. – LazyOne Nov 27 '15 at 10:11
  • To run this inspection only -- use `Code | Run Inspection by Name...` (it's faster that doing full `Inspect Code` for each file). – LazyOne Nov 27 '15 at 10:14
  • I think that `Invalidate Caches` and a full restart of PhpStorm did the trick. Inspection of this specific error seems to be buggy, I don't understand why (totally OK with other PHP errors/warnings). @LazyOne : in any case, thanks! http://s28.postimg.org/gr2aoquyl/phpstorm_php_inspection_3.png – Guicara Nov 27 '15 at 10:19

3 Answers3

5

That inspection works fine here (proof).

  1. Please try Code | Inspect Code... on this file -- it will force re-analysing of this file from scratch. Any better?

  2. If nothing -- please do File | Invalidate Caches... and restart IDE


P.S.
If you are interested of running this inspection only on whole project -- use Code | Run Inspection by Name... -- it's much faster that doing full Inspect Code for each file.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • I think that `Invalidate Caches` and a full restart of PhpStorm did the trick. Inspection of this specific error seems to be buggy, I don't understand why (totally OK with other PHP errors/warnings). @LazyOne : in any case, thanks! http://s28.postimg.org/gr2aoquyl/phpstorm_php_inspection_3.png – Guicara Nov 27 '15 at 10:37
1

Static code analysis may hint some potential errors. It never guarantees there are no errors, and one really shouldn't rely on it.

As a practical advice, you can search for all static calls with something like

grep -roh "\w\+::.\+\?("

and analyse the list yourself.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
0

Change the error reporting in your php.ini file

error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED

Sharmila Juliet
  • 33
  • 1
  • 1
  • 3