Search code examples
phpphpcs

PHPCS different ruleset for different directories


I am currently working on cleaning up an existing codebase and one of the aspects involve using PHPCS. This is my phpcs.xml file at the moment:

<?xml version='1.0'?>
<ruleset name='MyApplication'>
    <arg name='encoding' value='utf-8'/>
    <arg name='extensions' value='php'/>
    <arg name='warning-severity' value='0'/>
    <arg name='colors'/>
    <arg value='p'/>

    <exclude-pattern>vendor/*</exclude-pattern>
    <exclude-pattern>node_modules/*</exclude-pattern>

    <rule ref='PSR1'></rule>
</ruleset>

I'd like to modify it so that PSR-2 is applied to certain directories (e.g. foo/ and bar/) while the remaining directories are checked against PSR-1.

I found the <exclude-pattern> option in the manual that can be placed as child elements of the <rule> element but I need the opposite of that.


Solution

  • Create multiple configs and use --standard option to load them:

    vendor/bin/phpcs source-to-check --standard first-settings.xml 
    vendor/bin/phpcs another-to-check --standard second-settings.xml