Skip to content

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

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

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

if ($commentEnd === false || $commentStart === false) {
return;
}

$comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart));
if (strpos($comment, '* @api') === false) {
return;
}
Expand Down