-
-
Notifications
You must be signed in to change notification settings - Fork 88
Avoid PHP error on parse error in PSR12.Functions.ReturnTypeDeclaration
#1354
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
base: 4.x
Are you sure you want to change the base?
Avoid PHP error on parse error in PSR12.Functions.ReturnTypeDeclaration
#1354
Conversation
jrfnl
left a comment
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 setting this up @fredden !
I've left some questions in-line.
I also wonder (but haven't looked in detail yet) whether the code block on (new) line 70-90 could be simplified a little what with the $colon variable being set and known ahead of that code block.
Let me know what you think.
src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php
Outdated
Show resolved
Hide resolved
src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php
Outdated
Show resolved
Hide resolved
I rewrote the block with this in mind, but don't find it more legible than the original. There is precedent in this sniff to check the 'code' rather than the pointer for the closing parenthesis in the following block (new line 92). - if ($tokens[($colon - 1)]['code'] !== T_CLOSE_PARENTHESIS) {
+ if (($colon - 1) !== $tokens[$stackPtr]['parenthesis_closer']) { if ($tokens[($returnType - 1)]['code'] !== T_WHITESPACE
|| $tokens[($returnType - 1)]['content'] !== ' '
- || $tokens[($returnType - 2)]['code'] !== T_COLON
+ || ($returnType - 2) !== $colon
) {I'm happy to adjust according to your steer if you'd like. |
@fredden Thanks for looking into this. I agree that the first one doesn't improve readability, though the second may and that condition also has a duplicate in two more places in that block. Not a blocker either way. Whatever you decide will work. |
Because PHP evaluates `false - 1` to `(int) -1`, the array look-up was looking for `$tokens[-1]` which does not exist. This code change avoids that case by detecting the parse error / live coding situation early. Includes test.
c8e974c to
4c89af7
Compare
Description
When attempting to process a file with a
gitmerge conflict marker with the PSR12 standard, there is a PHP notice (withphpcs) or PHP Fatal error (withphpcbf) due to an unvalidated assumption in thePSR12.Functions.ReturnTypeDeclarationsniff that there will be a colon in the token array whenFile::getMethodProperties()detects a return type.I initially opened #1350 to change the output of
File::getMethodProperties(). While some discussion continues in that pull request, this pull request should unblock anyone attempting to use this sniff during a merge conflict.Suggested changelog entry
Added defensive coding to
PSR12.Functions.ReturnTypeDeclarationto avoid PHP errors.Related issues/external references
Relates to #152
Types of changes
PR checklist