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.67k stars 1.48k forks source link

Rule override for each directory (feature request) #2551

Open top-master opened 5 years ago

top-master commented 5 years ago

Taking just one rule-set XML file from the command line (or something) is really not enough to cover our entire project, its unit test dir specific rules, its sub-module related rules, and so on; all this in a single XML file can get quite complex.

For example, when git add . command is executed it handles current and all sub-directories and:

I would like PHPCS to handle the .phpcs.xml files in a similar way, I mean:

westonruter commented 3 years ago

I believe this is the same issue as is being described in the JetBrains issue tracker (for PhpStorm): https://youtrack.jetbrains.com/issue/WI-36028

My use case is that I have a project (a WordPress site) which contains multiple subprojects (WordPress themes and plugins), each of which may have their own configurations for PHPCS, ESLint, etc. When editing a JS file in a subproject, the IDEA lints the file according to the nearest ESLint config in that file's directory tree. In order to get the same behavior with PHPCS, as noted by @MaXal:

So to support this properly we have to change the working directory for each file but that, in turn, will break a lot of configurations since they depend on the working directory being the project root so I suggest filing the feature request to PHPCS to add an option or change the default behavior inline with JSHint (it looks more than reasonable to me).

jrfnl commented 3 years ago

Just want to leave a note here that the workflow as described above presumes a Composer based project, while PHPCS is not limited to Composer supported projects.

anomiex commented 3 years ago

In #3378 I took the approach of pushing the logic for finding the actual configs into the filter, so that people who want to try out things like looking for a composer.json can do that. I took a different approach in my own filter's implementation.

3378 doesn't actually implement the feature requested here in PHPCS's own filters, it just lays the groundwork.