Search code examples
phpunitphpstormphpstorm-2016.3

How to ignore `static method called as dynamic` inspection in tests only


PHPUnit has all assertions defined as static functions, but you are supposed to call them with object context1. Apart from if this is a good practice or not, if you want to follow this practice you will get warnings in PhpStorm: static method called as dynamic

I like this inspection and do not want to turn it off for all files. I do want to turn it off for phpunit tests, but adding a noinspection comment to all my tests is annoying and repetitive.

How can I ignore this in tests, but have this inspection in my normal code?
I'm using PhpStorm 2016.3


1: see Sebastian's comment on this github issue.

A regular user of PHPUnit should use $this-> instead of self::

Also, all the examples in the manual use this method.


Solution

  • To ignore the "static method called as dynamic" errors in your tests, you can define a scope, and then change the inspections for that scope.

    The setting-names etc are for PhpStorm 2016.3.

    scope

    You can use the default Test scope, by setting your tests directory as "test sources root". You can also create your own scope (see below)

    inspection

    Now make your inspection work only for this scope

    1. Go to settings -> editor -> inspections (or search for inspections)
    2. Search (in inspections, not in all settings) for the 'static method called as dynamic' inspection
    3. Expand the scopes (default it say 'in all scopes'), and select your new scope "all but phpunit tests"
    4. Check the tests scope
    5. unselect the warning, but keep the 'all scopes' selected.

    If you have created a specific scope for your project with exclusions, you could also select only that scope here.

    You might need to restart phpstorm or re-run inspections.

    It looks like so:

    settings example for phpstorm

    Creating your own scope

    1. Go to settings -> appearance & behavior -> scopes (you can also search for 'scopes' in the setttings-search).
    2. Press '+' to add a new scope
    3. Select your directories
    4. Apply / OK