ObliviousHarmony / vscode-php-codesniffer

A VS Code extension for integrating PHP_CodeSniffer.
https://marketplace.visualstudio.com/items?itemName=obliviousharmony.vscode-php-codesniffer
Other
37 stars 2 forks source link

Support files with different file extensions than .php (like .module .theme .install) #87

Closed schmitz-progressive closed 7 months ago

schmitz-progressive commented 7 months ago

Problem Description

Not sure if I should call this a bug or a feature request. I'm using phpcs to check my project for Drupal,DrupalPractice coding standards. For files using the extension .php this is working fine. On other files, like .module .install .theme which contain php code too, linting and formatting doesn't work. If I rename a file and add .php as extension it works fine.

My settings in VS code that could be relevant: { "phpCodeSniffer.exec.osx": "/Users/USERNAME/.composer/vendor/bin/phpcs", "phpCodeSniffer.standard": "Custom", "phpCodeSniffer.standardCustom": "Drupal,DrupalPractice", "[php]": { "editor.defaultFormatter": "obliviousharmony.vscode-php-codesniffer" }, "editor.tabSize": 2, "editor.useTabStops": false, "editor.rulers": [ 80, ], "emmet.includeLanguages": { "module": "php", "theme": "php" }, "files.associations": { "*.module": "php", "*.theme": "php", "*.install": "php", "*.inc": "php", "*.test": "php", "*.profile": "php", "*.php": "php" } }

Proposed Solution

Support and check all files that are associated with php.

Alternative Solutions

Provide settings for file types that should be checked

Additional Context

Standards I'm using: https://www.drupal.org/docs/contributed-modules/code-review-module/installing-coder composer global require drupal/coder

phpcs commands suggested by drupal: https://www.drupal.org/docs/contributed-modules/code-review-module/php-codesniffer-command-line-usage#s-check-drupal-coding-standards

ObliviousHarmony commented 7 months ago

Hi @schmitz-progressive,

I see from your config that you've already set up the formatter and files.associations setting correctly and so the files are being passed to phpcs. The problem is that phpcs is filtering the file out due to the file extension. What's neat here is that the extension will pass options to the executable in phpCodeSniffer.exec.osx if you define them. You can set it to /Users/USERNAME/.composer/vendor/bin/phpcs --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml and it should work just fine. I've gone ahead and documented this behavior in the readme.

However, I would suggest taking a look at some of the other solutions presented in the Drupal documentation. My recommendation would be for you to add a phpcs.xml file to the project. Generally speaking, this is the best practice when it comes to configuring PHPCS. This has the benefit of allowing you to run phpcs in the directory without any additional arguments as well as specify directories and other options to customize the behavior of PHPCS for a specific project. This is documented and they also link to an example.

Please let me know if this works for you! I'm going to avoid adding any additional configuration around this because I'd like to rely on the standard behavior of phpcs.

schmitz-progressive commented 7 months ago

Thanks for the hint! I didn't know that I can append options in those settings and I'll have a deeper look in the phpcs.xml file approach. Didn't think things like this would change the behavior of the extension, regarding file types that are checked.

ObliviousHarmony commented 7 months ago

Didn't think things like this would change the behavior of the extension, regarding file types that are checked.

To me it makes the most sense for the extension to behave the same as running phpcs from your command line. In this case you're running an extended phpcs command with special arguments so it feels appropriate that you would have to add those in the configuration here as well. This helps us avoid problems that are hard to track down that might be caused by overriding the configuration they've set in phpcs.xml files.