Load CodeSniffer.conf also from current dir, make it confgurable
Is your feature request related to a problem?
Path to the CodeSniffer.conf does not appear to be configurable, or at least I have not found how to do it.
The docs refer to "user specific configuration file" but I have not found documentation where that might be.
The only path I have found working for a composer install is vendor/squizlabs/php_codesniffer/CodeSniffer.conf, but that is problematic because if it exists before phpcs is installed (e.g. from a git checkout), it gets somehow removed by composer install. So it's not nicely shareable via git.
Describe the solution you'd like
Would be nice to load the configuration file (in addition to the current location(s) used) from $CURRENT_DIR/.config/CodeSniffer.conf by default, no matter the install method. For rationale for the .config/ dir, see https://github.com/pi0/config-dir. Failing that, looking up from $CURRENT_DIR/CodeSniffer.conf or something like that would also be an improvement.
While at it, could be useful to also make path to the config file configurable via an command line option or an environment variable.
Alternatively, make it possible to set these values via phpcs.xml (see the comment below for more discussion on this).
Additional context (optional)
- [x] I have read the Contribution Guidelines and this is not a support question.
- [ ] I intend to create a pull request to implement this feature.
Worth noting is that .phpcs.xml config files don't have those path lookup problems. But unfortunately it cannot be used to set these options that I can see, <arg/> doesn't work with --runtime-set, e.g.
<arg name="runtime-set" value="ignore_non_auto_fixable_on_exit 1"/>
...yields
ERROR: option "--runtime-set=ignore_non_auto_fixable_on_exit 1" not known
This does not seem to work either (no errors, but the setting does not take effect):
<arg name="runtime-set"/>
<arg value="ignore_non_auto_fixable_on_exit"/>
<arg value="1"/>
@scop I have a feeling that your feature request is mostly related to your last point:
Alternatively, make it possible to set these values via
phpcs.xml(see the comment below for more discussion on this).
... and I'm happy to say this is already perfectly possible for nearly everything you can do via the CodeSniffer.conf file.
This doesn't work:
<arg name="runtime-set" value="ignore_non_auto_fixable_on_exit 1"/>
... but this does:
<config name="ignore_non_auto_fixable_on_exit" value="1"/>
Please have a look at the Configuration Options documentation and see the examples in the Annotated Ruleset page.
Based on your question, I imagine there may improvements which can be made to those pages. Happy to receive a pull request with suggestions for this via the documentation repo, where the contents of the wiki is managed.
Does that solve your problem ?
As for making the path to the CodeSniffer.conf file configurable:
First off, I'm not sure it is actually needed as the ruleset should be leading and all config options can be set in one way or another via the ruleset.
Secondly, it is not as easy as it sounds as how would PHPCS know where to look ? It currently looks in the root of its own installation. It used to also look in a PEAR user directory. I imagine, a user root directory could possibly be added to that list of locations and there is an older feature request already open for that: https://github.com/squizlabs/PHP_CodeSniffer/issues/2062
While at it, could be useful to also make path to the config file configurable via an command line option or an environment variable.
But other than that, all other solutions would still require the user to point to the file from the command-line, in which case they may as well pass the settings via the command-line.
Thirdly, the CodeSniffer.conf should not be confused with a ruleset. The conf file contains user-specific settings and may contain system paths specific to the user's computer. This also means the file should not be committed to a repo, while a ruleset file is recommended to be committed to a repo.
The only path I have found working for a composer install is
vendor/squizlabs/php_codesniffer/CodeSniffer.conf, but that is problematic because if it exists before phpcs is installed (e.g. from a git checkout), it gets somehow removed bycomposer install. So it's not nicely shareable via git.
As mentioned above, it shouldn't be shareable via git. It is user specific.
In the typical situation of a Composer setup, the CodeSniffer.conf should rarely be used and if it's used, it should mostly be used for managing the installed_paths setting and there is a Composer plugin available which will handle that and update that setting whenever your vendor directory changes via Composer.