Php_codesniffer: Custom sniff outside vendor dir

Created on 27 May 2019  路  6Comments  路  Source: squizlabs/PHP_CodeSniffer

According to your docs:

If you want your standard to be shown as installed, create the MyStandard directory 
inside the PHP_CodeSniffer install

As I'm using Composer to manage dependencies, this is not an option.
Can we define the location in the XML?

All 6 comments

If you are creating a custom sniff, shouldn't you be creating a custom standard? Easier to maintain it that way imo.

@delboy1978uk The DealerDirect PHPCS Composer plugin can sort all that out for you, have a look and see if that works for you: https://github.com/Dealerdirect/phpcodesniffer-composer-installer/

If you want to reference a sniff by path, you can do that in an XML ruleset. Some examples from the wiki:

 <!--
    Include all sniffs in an external standard directory. Note
    that we have to specify the full path to the standard's
    directory because it does not exist inside the PHP_CodeSniffer
    install directory.
 -->
 <rule ref="/home/username/standards/mystandard"/>

 <!--
    Include everything in another ruleset.xml file. This is
    really handy if you want to customise another developer's
    custom standard. They just need to distribute their single
    ruleset file to allow this.
 -->
 <rule ref="/home/username/standards/custom.xml"/>

 <!--
    Relative paths can also be used everywhere absolute paths are used.
    Make sure the reference starts with ./ or ../ so PHP_CodeSniffer
    knows it is a relative path.
 -->
 <rule ref="./standards/mystandard"/>
 <rule ref="../username/custom.xml"/>

If you want a custom standard to appears as "installed", which means you can reference it by name in the XML rather than by path, you can use the composer installer plugin that @jrfnl linked to, or you can set a config option in PHPCS, but you can't set anything in the XML itself as you'd be using absolute or relative paths anyway.

If you are creating a custom sniff, shouldn't you be creating a custom standard? Easier to maintain it that way imo.

yes that's what we are now doing

@delboy1978uk The DealerDirect PHPCS Composer plugin can sort all that out for you, have a look and see if that works for you: https://github.com/Dealerdirect/phpcodesniffer-composer-installer/

thanks i'll have a look next time i'm back on this task :-)

If you want to reference a sniff by path, you can do that in an XML ruleset. Some examples from the wiki:

 <!--
    Include all sniffs in an external standard directory. Note
    that we have to specify the full path to the standard's
    directory because it does not exist inside the PHP_CodeSniffer
    install directory.
 -->
 <rule ref="/home/username/standards/mystandard"/>

 <!--
    Include everything in another ruleset.xml file. This is
    really handy if you want to customise another developer's
    custom standard. They just need to distribute their single
    ruleset file to allow this.
 -->
 <rule ref="/home/username/standards/custom.xml"/>

 <!--
    Relative paths can also be used everywhere absolute paths are used.
    Make sure the reference starts with ./ or ../ so PHP_CodeSniffer
    knows it is a relative path.
 -->
 <rule ref="./standards/mystandard"/>
 <rule ref="../username/custom.xml"/>

If you want a custom standard to appears as "installed", which means you can reference it by name in the XML rather than by path, you can use the composer installer plugin that @jrfnl linked to, or you can set a config option in PHPCS, but you can't set anything in the XML itself as you'd be using absolute or relative paths anyway.

I didn't realise we could specify a path, I'll see if we can use that for now before looking at the composer solution.

Thanks everyone!

Was this page helpful?
0 / 5 - 0 ratings