Skip to content
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

MySource.PHP.AjaxNullComparison throws error when first function has no doc comment #2368

Merged
merged 1 commit into from
Apr 3, 2019

Conversation

MariusLab
Copy link

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.

Copy link
Contributor

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@@ -47,7 +47,12 @@ public function process(File $phpcsFile, $stackPtr)
// Make sure it is an API function. We know this by the doc comment.
$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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gsherwood gsherwood changed the title Fix AjaxNullComparisonSniff to ignore files with no doc comments MySource.PHP.AjaxNullComparison throws error when first function has no doc comment Apr 3, 2019
@gsherwood gsherwood added this to Backlog in PHPCS v3 Development via automation Apr 3, 2019
@gsherwood gsherwood added this to the 3.4.2 milestone Apr 3, 2019
@gsherwood gsherwood merged commit 5cbef5e into squizlabs:master Apr 3, 2019
PHPCS v3 Development automation moved this from Backlog to Ready for Release Apr 3, 2019
gsherwood added a commit that referenced this pull request Apr 3, 2019
@gsherwood
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging this pull request may close these issues.

None yet

3 participants