Created a new Generic.WhiteSpace.LanguageConstructSpacingSniff#1337
Created a new Generic.WhiteSpace.LanguageConstructSpacingSniff#1337gsherwood merged 15 commits intosquizlabs:masterfrom
Conversation
| T_ECHO, | ||
| T_PRINT, | ||
| T_RETURN, | ||
| T_INCLUDE, |
There was a problem hiding this comment.
Not sure about include, require, include_once and require_once because they can be written in 2 ways:
- as keyword:
include_once 'file.php'; - as function call:
include_once('file.php');
In later case it will be auto-fixed as include_once ('file.php'); which will trigger warning in another sniff checking function calls.
There was a problem hiding this comment.
These TOKENS already existed in the deprecated one. I just copied and added more tokens. The sniff already used to handle such cases.
There was a problem hiding this comment.
Then probably I reported that issue for deprecated sniff as well. Then don't change anything here.
|
Now that I think about it I must add also tests. I forgot about it. |
ccffbbb to
91386fe
Compare
| return($blah); | ||
|
|
||
| yield $blah; | ||
| yield $blah; |
There was a problem hiding this comment.
Not sure how to make this test pass for version 5.3 and 5.
|
Hello... I moved this PR to use the new version of codesniffer but still I don't know how to pass PHP 5.4 Same as here |
For most versions it is a case of cleaning up the added code (docblock at the top) - see: https://travis-ci.org/squizlabs/PHP_CodeSniffer/jobs/230469601#L224 As for PHP 5.4, see my remark in the other issue |
|
Since this #1513 PR was merged the tests of the current one are passing and it can now be merged I think. |
| T_REQUIRE, | ||
| T_REQUIRE_ONCE, | ||
| T_NEW, | ||
| T_YIELD, |
There was a problem hiding this comment.
What about also adding T_YIELD_FROM ?
There was a problem hiding this comment.
I saw the conversation on the other PR and I also thought about it but I need to understand something here is the T_YIELD_FROM equals to just from or yield from?
There was a problem hiding this comment.
OK... I will need to check it out if this sniff has the same behaviour with two word tokens.. I am curious about it.
There was a problem hiding this comment.
hello @jrfnl
As I suspected this sniff is not working as expected with two word tokens (check the travis)
Do you have any suggestion about this?
My suggestion would be:
- Remove these test cases but add comments on the sniff and inside test and fix this on a different PR.
- Remove the T_YIELD_FROM add comments on the sniff that T_YIELD_FROM needs to be added and solve this on a different PR.
@gsherwood what do you think?
There was a problem hiding this comment.
@gmponos I would go with something like that:
if ($tokens[$stackPtr]['code'] === T_YIELD_FROM) {
$space = substr($tokens[$stackPtr]['content'], -5, 4);
if ($space !== ' ') {
// error and fix
}
}I know, it is only specific for that one token, but in future if we have another 'two words' token we can prepare something more generic.
There was a problem hiding this comment.
I second @webimpress' suggestion here, though I would change the second line as that would still not get the right info:
$space = stri_replace(array('yield', 'from'), '', $tokens[$stackPtr]['content']);
// Or maybe even better (simpler), just do:
if (strtolower($tokens[$stackPtr]['content']) !== 'yield from') {
// error and replace content
}There was a problem hiding this comment.
// Or maybe even better (simpler), just do: if (strtolower($tokens[$stackPtr]['content']) !== 'yield from') { // error and replace content }
Oh, yeah, it even better and much clearer! Thanks @jrfnl
|
Just wondering: should the "old" sniff be removed/excluded from PHPCS native rulesets which contain it and the "new" sniff be added to them instead ? |
Let's say that someone has a custom ruleset and includes all the Squiz ruleset. If he had in his custom ruleset |
|
Seems it still can't be merged because of this blocking issue #1572 |
| * Ensures all language constructs contain a single space between themselves and their content. | ||
| * | ||
| * @author Greg Sherwood <gsherwood@squiz.net> | ||
| * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
There was a problem hiding this comment.
I think, as it is new file year here should be 2017, not 2015.
| { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| if ($tokens[($stackPtr + 1)]['code'] === T_SEMICOLON) { |
There was a problem hiding this comment.
We need check first if $tokens[($stackPtr + 1)] is set.
There was a problem hiding this comment.
Please have in mind that these are copy pasted from the deprecated one.
| if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { | ||
| $content = $tokens[($stackPtr + 1)]['content']; | ||
| $contentLength = strlen($content); | ||
| if ($contentLength !== 1) { |
There was a problem hiding this comment.
Not sure about this check. What if the character after keyword is single tab (\t) or just new line (\n)?
There was a problem hiding this comment.
Please have in mind that these are copy pasted from the deprecated one.
same here.
There was a problem hiding this comment.
Then I would improve it, as it is a new sniff. Seems to be wrong for me, in cases I've mentioned.
There was a problem hiding this comment.
First of all there is an issue risen here. If one of these two sniffs has changes/fixes then the sync between them can be easily forgotten.
I wonder if it would be better instead of copy-pasting the code from the Squiz sniff (as I did) the new Generic sniff to extend the Squiz sniff and only override the register function with more tokens.
About the corrections that you are saying I would suggest you to create a PR for the Squiz sniff and have the current PR pending until these changes are merged. Once your PR is merged we can proceed here either with the way I suggest above or by copy-pasting again the code along with the fixes. The reason is that
- I like to keep my PR focused on the main target and the goal of this PR was adding more tokens. It is just a way of how I prefer to work.
- Also these fixes need to be made also on the Squiz sniff.
If for those two suggestion above @gsherwood has another opinion I don't mind to go by that.
There was a problem hiding this comment.
I wonder if it would be better instead of copy-pasting the code from the Squiz sniff (as I did) the new Generic sniff to extend the Squiz sniff and only override the register function with more tokens.
I personally would do it the other around: have the Generic sniff contain the code and let the Squiz version extend and overload the register() method if so desired.
There was a problem hiding this comment.
Or maybe we should just update list of keywords in Squiz... ? The same I've proposed in #1492 and it looks like it will be merged for 3.1.0 release.
There was a problem hiding this comment.
Or maybe we should just update list of keywords in Squiz... ?
As I said I like to keep my PR focused and that was my main goal but if you read the related issue at the top I had to go by what @gsherwood said.
I personally would do it the other around: have the Generic sniff contain the code and let the Squiz version extend and overload the register() method if so desired.
I find it reasonable.
There was a problem hiding this comment.
This is resolved in another PR and I will follow the suggestion of @jrfnl about Squiz extending the Generic one.
…ore language constructs. - Squiz.Whitespace.LanguageConstructSpacing is deprecated and will extend the Generic one.
|
I believe it's fine now. Hope you can check it. |
|
Thanks a lot for the PR. I've merged it in now, with a couple of small changes:
The sniff will be available from version 3.3.0. |
|
...and I forgot to fix the tests to support older PHP version. Will do that now. |
|
@gsherwood As you say, removing the sniff is a BC-break. More than that, it is a problematic BC break for external standards which support more PHPCS versions than just the latest & greatest. Would you please reconsider deprecating the old sniff (and letting it extend the new sniff for now) and only removing the old sniff in v 4.0.0 ? |
…lder PHP versions (ref #1337)
Fair point. I've restored the Squiz sniff to its old behaviour. I don't think it should implement any of the new checks, so it does not extend the new sniff. I've also fixed the multi-line T_YIELD_FROM issue that was causing the unit tests to fail on old PHP versions. |
|
Thanks for merging and sorry for any inconvenience caused. |
|
@gsherwood Much appreciated! |
Common::prepareForOutput() replaces spaces with a middle dot character to make whitespace visible in error messages. It was added to this sniff in commit 6ef2856 ("Better support for showing spaces and newlines"), probably as a follow-up to the multi-line `yield from` support added in squizlabs/PHP_CodeSniffer 1337 (commits ce60083 and d1a9163) as all three commits are by the same author on the same day. For most PHP keywords prepareForOutput() has no effect since they don't contain whitespace. The one exception is `yield from` (T_YIELD_FROM), which, in some cases, is tokenized as a single token containing a space. In that case, prepareForOutput() would display `yield·from`, but I would argue that showing the keyword as-is is preferable even in this case. This changes the error output for `yield from` from: ``` expected "yield·from" but found "Yield·From" ``` To: ``` expected "yield from" but found "Yield From" ``` Suggested in: squizlabs/PHP_CodeSniffer 2652 It was added to this sniff in commit 6ef2856 ("Better support for showing spaces and newlines"), as a follow-up to the multi-line yield from support added in squizlabs/PHP_CodeSniffer#1337
Closes #1317