Avoid PHP error on parse error in PSR12.Functions.ReturnTypeDeclaration#1354
Conversation
jrfnl
left a comment
There was a problem hiding this comment.
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. |
c8e974c to
4c89af7
Compare
src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php
Outdated
Show resolved
Hide resolved
| for ($i = ($returnType - 1); $i > $colon; $i--) { | ||
| $phpcsFile->fixer->replaceToken($i, ''); | ||
| } |
There was a problem hiding this comment.
Just curious: any particular reason to walk backwards instead of forwards ?
There was a problem hiding this comment.
I don't know why this is going down versus up. There might have been a good reason, but I can't think of it currently. Would you prefer it to go up instead?
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.
4c89af7 to
124b61f
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