squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

How to set/reference standard location in the configuration? #1960

Closed JanisE closed 6 years ago

JanisE commented 6 years ago

I have composer-installed Code Sniffer as a development dependency and would like to define a project-specific phpcs.xml file that references various standards (standard ones and additionally composer-installed third party ones), and then specifies options (like, excludes some sniffs, sets properties to other sniffs etc.).

However, currently, even if I can set all the other options I need in phpcs.xml, I cannot define references of 3rd party standards, namely, installed_paths option is not available there. Instead, it is set in CodeSniffer.conf, which is located in the vendor folder tree somewhere.

Could that option be added to phpcs.xml as well? Or if it already is possible, how to do it? Or is my approach as such not correct?

jrfnl commented 6 years ago

@JanisE You already can: <config name="installed_paths" value="/path/to/standard1,/path/to/standard2"/>

For more info, also have a look at: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

jrfnl commented 6 years ago

Oh and as you are using Composer - you may not even need that. The dealerdirect/phpcodesniffer-composer-installer package can sort installed paths out for you, just add it as a (dev) dependency.

See: https://github.com/Dealerdirect/phpcodesniffer-composer-installer

JanisE commented 6 years ago

Thanks! I had tried the <config> element but due to some misunderstanding, decided that it didn't work for my case. But I tried again, and this worked:

    <!-- The paths are relative to "vendor/squizlabs/php_codesniffer/" -->
    <config name="installed_paths" value="./../../../vendor/magento-ecg/coding-standard,./../../../vendor/slevomat/coding-standard"/>

Only I had to manually remove vendor/squizlabs/php_codesniffer/CodeSniffer.conf file (see issue #1972)

dealerdirect/phpcodesniffer-composer-installer seems a bit too magical for me, so I'll better stick to the <config> option for now. :)

jrfnl commented 6 years ago

dealerdirect/phpcodesniffer-composer-installer seems a bit too magical for me,

For what it's worth: I've seen extremely good results with that plugin, both myself as well as from users of standards I contribute to, and no significant bugs. When there are issues, I've found the team behind the plugin to be very responsive and helpful.

JanisE commented 6 years ago

Yes, thanks! What I meant by "magical" is that it's not clear to me how it works. I like the straightforward config approach – I explicitly set the standards I use and I don't need to second-guess what is loaded, what is cached where for which developers etc. I'll keep in mind there is such an option.

jrfnl commented 6 years ago

@JanisE Ah! It's not that magical. It's meant for Composer installs, so it will check your (local/global based on run context) vendor directory for any folder which contains a ruleset.xml and will register the appropriate paths with PHPCS. The paths will end up in the CodeSniffer.conf file, just like if you'd manually run --installed-paths. The advantage is of course that you don't need to sort this out yourself, nor need to worry about differences between paths in different OS-es which your users may be running on.

If you want to know more, here's the repo: https://github.com/Dealerdirect/phpcodesniffer-composer-installer

JanisE commented 6 years ago

@jrfnl But it is. For example, I'm using magento-ecg coding standards, but their composer.json does not include type: "phpcodesniffer-standard".

phpcodesniffer-composer-installer says:

The package type must be phpcodesniffer-standard. Without this, the plugin will not trigger.

So, it wouldn't "just work" (magically), and I would have yet another problem to investigate. :)

jrfnl commented 6 years ago

@JanisE I get where you're coming, though for the Magento standard, that should be easy enough to solve with a pull request ;-)

kristijorgji commented 3 years ago

What is the config attribute doing ?

mialdi98 commented 11 months ago

Thanks! I had tried the <config> element but due to some misunderstanding, decided that it didn't work for my case. But I tried again, and this worked:

    <!-- The paths are relative to "vendor/squizlabs/php_codesniffer/" -->
    <config name="installed_paths" value="./../../../vendor/magento-ecg/coding-standard,./../../../vendor/slevomat/coding-standard"/>

Only I had to manually remove vendor/squizlabs/php_codesniffer/CodeSniffer.conf file (see issue #1972)

dealerdirect/phpcodesniffer-composer-installer seems a bit too magical for me, so I'll better stick to the <config> option for now. :)

For those who are looking for solution of ERROR: Referenced sniff "SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" does not exist for Drupal 9

I created file .phpcs.xml in a root folder with these lines of code:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="custom_rule_set">
  <description>PHP CodeSniffer configuration for development.</description>
  <file>.</file>
  <arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,md,yml"/>
  <!-- The paths are relative to "vendor/squizlabs/php_codesniffer/" -->
  <config name="installed_paths" value="./../../../vendor/drupal/coder/coder_sniffer,./../../../vendor/slevomat/coding-standard"/>
  <config name="drupal_core_version" value="9"/>
  <rule ref="./vendor/drupal/coder/coder_sniffer/Drupal"/>
  <rule ref="./vendor/drupal/coder/coder_sniffer/DrupalPractice"/>
  <exclude-pattern>./vendor/*</exclude-pattern>
  <exclude-pattern>./web/modules/composer/*</exclude-pattern>
  <exclude-pattern>./web/themes/composer/*</exclude-pattern>
  <exclude-pattern>./web/sites/default/files/*</exclude-pattern>
</ruleset>

And after that, my VSCode saw everything.

Using https://marketplace.visualstudio.com/items?itemName=obliviousharmony.vscode-php-codesniffer on mac with /usr/local/Cellar/php@8.1/8.1.19/bin/php ./vendor/bin/phpcs (PHP version on your project choice) And Automatic standard