MySource.PHP.AjaxNullComparison throws error when first function has no doc comment#2368
MySource.PHP.AjaxNullComparison throws error when first function has no doc comment#2368gsherwood merged 1 commit intosquizlabs:masterfrom MariusLab:fix-ajaxnullcomparison-sniff
Conversation
jrfnl
left a comment
There was a problem hiding this comment.
The change in itself looks reasonable, though you may want to add a unit test to make sure future changes don't undo this fix.
| $commentEnd = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr); | ||
| $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, ($commentEnd - 1)); | ||
| $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart)); | ||
| // If function doesn't contain any doc comments - skip it. |
There was a problem hiding this comment.
Based on the above lines, you could already short-circuit the sniff after the $commentEnd assignment as $commentStart will always be false if no $commentEnd could be found.
Also, the way the findPrevious() for $commentEnd is done now, it could accidentally pick up an unrelated docblock when there are two functions, the first with a docblock, the second without one.
When the sniff is triggered for the second function, the docblock of the first function will be examined.
|
Thanks for this fix. If you'd like me to credit you in the changelog, please let me know what name you'd like me to use. |
A RuntimeException is thrown in File::getTokensAsString() if the first parameter is not an int or if the supplied int does not exist as a position in the token stack.
AjaxNullComparisonSniff ends up throwing this exception on every file that contains functions with no comment blocks.