2

I have a custom phpcs ruleset file, where I am including a couple of rulesets and also excluded files from certain directories like tests or vendor.

The full phpcs.xml config file is at https://github.com/sudar/bulk-move/blob/master/phpcs.xml Below I have mentioned the interesting bits

<file>./</file>

<!-- Exclude test directories -->
<exclude-pattern>tests/*</exclude-pattern>

<!-- PHP Compatibility -->
<config name="testVersion" value="5.2-"/>
<rule ref="PHPCompatibility">
</rule>

Now when I run phpcs command from the root of my project, it excluded the files from tests directory and runs phpcs only on the other files.

In PhpStorm I have selected this phpcs.xml file as the custom coding standard while configuring phpcs. But when I am editing a file from the tests directory (that is excluded) PhpStorm still calls phpcs with the custom coding standard and show warnings.

So my question is how to tell PhpStorm to exclude the files that are excluded in the phpcs.xml ruleset file?

Sudar
  • 18,954
  • 30
  • 85
  • 131

2 Answers2

6

<exclude-pattern> is not currently supported.

https://youtrack.jetbrains.com/issue/WI-22102 -- watch this ticket (star/vote/comment) to get notified on any progress.


Right now you can exclude files from PHPCS processing .. but it must be done manually in IDE.

PHP CodeSniffer integration (displaying the validation results) is done as an Inspection .. so you can configure it (enable/disable) on per scope basis:

  • Go to Settings/Preferences | Editor | Inspections
  • Locate CodeSniffer inspection (PHP | PHP Code Sniffer validation)
  • Adjust scopes as needed
    • If you have marked your tests folder as Tests Root in Project View panel (or Settings/Preferences | Directories .. or maybe it was auto-detected from composer.json file) then such folder is already a part of standard "Tests" scope)
    • If no -- either mark then as such ... or create custom Scope at Settings/Preferences | Appearance & Behavior | Scopes and then use it there.

Some links:

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • Thanks for pointing out the ticket. It's really unfortunate that Jetbrain has not fixed it even after 4 years. – Sudar Dec 20 '17 at 03:50
  • The scopes workaround will not work for me since I need to apply different phpcs code sniff for 'tests' and 'src' files. Scopes only allows me to either enable or disable phpcs inspection based on scope. – Sudar Dec 20 '17 at 03:51
  • I'm not sure how exactly CS integration works ... but **in theory** that should not be really affecting the rules themselves: nowhere in IDE you specify what rules to use and where -- you only specify what ruleset to use; how it will be handled internally should be up to PHPCS I would think. "Exclude" in this case is if should the PHPCS be run for that file at all or not. But if it does not work like that ... then my "in theory" is wrong/does not match the reality. – LazyOne Dec 20 '17 at 09:09
  • I have defined the rules in phpcs.xml such that the rulesets apply based on where the file is present. (Different ruleset for src and tests). It works perfectly when called from command line. But it doesn't work in PhpStorm. – Sudar Dec 20 '17 at 12:31
  • 1
    Possibly (just a though) that it depends on how IDE executes the actual command. It definitely uses the temp location .. so the relative path (even partial) to that file may not be the same as actual/original file has. See if you can check that. No other bright ideas. – LazyOne Dec 20 '17 at 13:53
1

It will be supported on 2020.2! See Youtrack link above.