New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sniff exclusions/restrictions dont work with custom sniffs unless they use the PHP_CodeSniffer NS #1451
Sniff exclusions/restrictions dont work with custom sniffs unless they use the PHP_CodeSniffer NS #1451
Conversation
I vaguely remember wanting to come back to this code... I don't like the idea of keeping a hard-coded array of included standards though. It feels very high-level yet used for something really specific. I'd prefer either being able to gather these from a helper function by looking at children of src/Standards or just checking with |
I agree, but wasn't sure what you'd prefer and considering the included standards haven't changed since 2012 and it is rare for a new standard to be added, it seemed the simplest & most efficient way to make the fix. Let me add a second commit with a function to check it instead. |
… standards in PHPCS 3.x When running PHPCS with a custom standard, the command-line `--sniffs` and `--exclude` options no longer work. This is caused by the sniffNames being prefixed with PHPCS's own `php_codesniffer\standards\` namespace which custom standard should not use in the first place. This PR fixes this by only prefixing the sniffname with the PHPCS native namespace when it is a sniff from one of the build-in standards.
Rebased & added the second commit. Feel free to squash the commits on merge. |
60d1042
to
aa4c665
Compare
Change the determination of whether something is a native standard over from a hard coded array to a function checking the standards directory. The set of standards is only retrieved once for efficiency.
I was just doing some testing of this and I don't think this change is actually going to fix the problem as it enforces that the namespace be exactly I'll take a deeper look into this and see if I can figure out where things are going wrong. |
I should have kept looking before typing... I just need to change the way the restriction is checked so it is looking at the end of the namespace instead of the entire thing. Bit more work, but it should work. |
That would be in another part of the code though, wouldn't it be ? and is an additional change which may be needed. Would you like me to add that to this PR or are you already fixing this yourself ? |
… sniffs unless they use the PHP_CodeSniffer NS
I've just committed a fix for this issue that was working in my testing. It basically enforces the rule that sniff namespaces must end with Interested to know if this works for your case as well. |
Sorry, but my first test run fails miserably with the following error:
|
I think I need the repo you are testing with so I can replicate your problems. Is that possible? |
Just a guess (haven't tested it), but this may fix it: $end = (strlen($newName) - (strrpos($newName, '\sniffs\\') + 1)); (= brackets around the strrpos + 1) |
I don't think so. I think the problem here is that the sniff namespaces are not in the allowed format. As in, they don't even include But that's just a guess based off some basic testing. |
Sorry, but no, the namespace declaration which I'm using in this case is |
Also - the original code would be cutting off too soon - see some test code I was just trying out: https://3v4l.org/0f83v |
Thanks. I completely forgot the case where nothing needs cleaning. |
No worries, keeping my fingers crossed for a reliable fix ;-) |
Just tested with the latest commit and it seems to work fine now. |
When will this be available in a new release? Once all 3.0.1 tasks are done? |
Yep. |
When running PHPCS with a custom standard, the command-line
--sniffs
and--exclude
options no longer work.This is caused by the sniffNames being prefixed with PHPCS's own
php_codesniffer\standards\
namespace which custom standard should not use in the first place.This PR fixes this by only prefixing the sniffname with the PHPCS native namespace when it is a sniff from one of the build-in standards.