-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Improve context detection for PSR12.Operators.OperatorSpacing
/ Squiz.WhiteSpace.OperatorSpacing
- avoids fixer conflict, bow out on parse error
#289
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @fredden Thanks for this PR!
I agree with your analysis of the fixer conflict and the solution direction.
I do have some remarks and questions though about the implemented fixes:
- For both "rename test file" commits: the
switch
statement is missing thedefault
case returning an empty array. - Again both sniffs: if no fixes are expected, there should not be a
.fixed
file. - For the
OperatorSpacing
sniff - which extends the SquizOperatorSpacing
sniff:- Is there a particular reason why you put the fix in the
PSR12
sniff ? (instead of in theSquiz
sniff)
I can imagine the same issue would also be troublesome for the Squiz sniff. - Is there a particular reason why you do the live coding check after the
isOperator()
check ? - Why are you checking for live coding using
isset($tokens[($stackPtr + 2)])
instead of doing afindNext(T_WHITESPACE, ($stackPtr + 1), null, true)
?
If there would be multiple new lines after the unfinished=
your check would pass and the fixer conflict would still come into play.
- Is there a particular reason why you put the fix in the
- For the
DeclareStatement
sniff:- Aside from the same question about the
isset()
, I see you've put the "bow out" in the section of the sniff strictly dealing with the assignment operator.
Have you considered that the sniff should maybe not run at all if thedeclare
statement is unfinished, i.e. does not have a close parenthesis ?
- Aside from the same question about the
Just FYI: I have a WIP branch locally to do a complete rewrite of the PSR12 DeclareStatement
sniff as there is a lot more wrong with that sniff. Hoping to at some point find the time to finish the rewrite. I won't let it block this PR though.
These files were very useful during debugging / development. I have removed them as per your preference.
I was focusing on the PSR12 standard. I have now moved the fix to the Squiz standard and added a test there to catch the same problem.
I didn't want to modify anything outside of the scope I was looking at: PSR12. Given the above, the check is now in the
I don't have a good justification for this. I've applied the suggested change.
This sounds like a good improvement that could be made to the sniff. I don't think it's in scope here, but I've make a code change to cover this to address this feedback. |
Questions for @jrfnl:
|
PSR12.Operators.OperatorSpacing
/ PSR12.Files.DeclareStatement
PSR12.Operators.OperatorSpacing
/ PSR12.Files.DeclareStatement
/ Squiz.WhiteSpace.OperatorSpacing
🤦🏻♀️ I'm sorry, I completely missed those
Not so much my preference, as it is a convention in this repo. Having said that, I didn't realize until your remark that if a I've had a look at this and prepared a fix, but I think this will need to wait until PHPCS 4.0 as there are quite a few external standards which use the PHPCS native test framework and changing the framework to flag a missing Aside from that, if I run the PHPCS native test suite with that extra failure, it flags up 5 missing I've opened issue #299 and #300 to address these.
Thanks for moving the check. Looking at it as it currently is - I still have a question:
👍🏻 Might be good to safeguard this by adding one or two extra new lines at the end of the file in the "live coding" test case files ?
Not all questions need to be addressed, sometimes they are just questions. 😉 I've looked at the fix you applied now and am going to ask you to remove that commit. As I said before, there is a lot wrong with that sniff and the extra code change you've applied now do not mitigate this. Not your fault, it's just a really bad sniff. Case in point: the I will address this (and a dozen or more other bugs) in that complete rewrite PR.
Yes please, that should be in a commit prior to the fix commit.
I would not squash merge this PR as it contains file renames and those don't always get tracked correctly when squashed in with other changes (and it is much more helpful when looking at the file history if they do get tracked correctly). I would prefer the actual "fix" commits to be squashed to one commit per fix. I'm happy to do this myself when the PR is ready for commit though, so you don't need to worry about this if you don't want to. If you do end up rebasing and reorganizing the commits yourself, I have one extra suggestion: prefix commits with the (short) name of the sniff they relate to, like "PSR12/DeclareStatement: rename test file", that makes the commit history much more easy to scan. Does that answer your questions ? |
The other checks in the function are less expensive. Calling a function is more expensive than using a built-in like
There are a lot of variations which could be covered by tests. These are likely to identify more problems with the sniff(s) though. For example,
I've added this commit to the pull request. I've put it at the end so that reviewing is kept easy. (GitHub has a nice feature to show what has changed since "my last review," which breaks when history is rewritten.) We can change the order of commits when we're ready to re-base. |
Agreed, but the other checks don't just do hash look-ups, they also include
Yes please, I think it makes sense to move it to the start.
That's totally fair. I will keep it in mind for the refactor PR. (and yes, I need to find time to finish that at some point, the reason I didn't finish it before is largely cause so much needs to change that it becomes very difficult to do this in clean atomic commits and to decide what commits are needed and in which order)
👍🏻 |
Thanks for all the feedback. I think we're at a position where the final delta in this pull request is acceptable. If that's the case, shall I now rewrite the history so that the commits are cleaner? |
PSR12.Operators.OperatorSpacing
/ PSR12.Files.DeclareStatement
/ Squiz.WhiteSpace.OperatorSpacing
PSR12.Operators.OperatorSpacing
/ Squiz.WhiteSpace.OperatorSpacing
- avoids fixer conflict, bow out on parse error
src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed
Outdated
Show resolved
Hide resolved
There are no functional code changes being applied here. This is a preparation step to allow adding an additional test case file for this sniff later.
There are no functional code changes being applied here. This is a preparation step to allow adding an additional test case file for this sniff later.
b66fefb
to
ae4228d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making those updates and reorganizing this branch @fredden, all looks good now.
I'll fix up that alignment issue and will merge it now.
* @return void|int Optionally returns a stack pointer. The sniff will not be | ||
* called again on the current file until the returned stack | ||
* pointer is reached. Return `$phpcsFile->numTokens` to skip | ||
* the rest of the file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: subsequent line alignment is off-by-one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I pasted this in, my editor prefixed the lines with *
, so it ended up with * * [...] called again on the [...]
. I removed the *
, but it seems I missed the extra space. This was not identified when I ran phpcs
over the codebase (which I do before committing). Perhaps there's a bug / oversight in the rule-set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I seem to remember working on a fix for this years ago and that there was some discussion on what the fix should look like... Pff.. I probably still have that branch somewhere, though it may have gotten lost on an old laptop (I have a feeling I worked on this while travelling).
Let's park it for when the docs sniffs get revisited in their entirety.
…skip over declare() statements entirely These sniffs intentionally do not handle declare statements, as these are handled by other sniffs. The logic previously used was not complete enough to bow out in all circumstances. These changes skip over the whole declare statement by registering for its token, and skipping over the statement when found. This ensures that nothing inside it is detected and processed by mistake.
ae4228d
to
73ca839
Compare
@michalbundyra A search showed me the WebImpress standard extends the Squiz/OperatorSpacing sniff. You may want to have a look at this PR and update the sniff if needed. |
Thanks, @jrfnl, will check it out |
…e statement since PHP_CodeSniffer 3.9.1 PR PHPCSStandards/PHP_CodeSniffer#289 changes a lot around operator sniff, and now declare statement must be omitted (as it was done also for PSR-12 rule there). There is another sniff to verify declare statement, and we do not want to check it here.
Description
I have investigated one of the issues identified in #152
PSR12
src/Standards/Generic/Tests/PHP/RequireStrictTypesUnitTest.13.inc
PSR12.Operators.OperatorSpacing
PSR12.Files.DeclareStatement
Verbose output from
phpcbf
runIn order to resolve / fix this conflict, I have taken the following steps:
Suggested changelog entry
Updated
PSR12.Operators.OperatorSpacing
andPSR12.Files.DeclareStatement
to bow out when a parse error is detected.Related issues/external references
Types of changes
PR checklist