You are here

protected function InlineCommentSniff::isInCodeExample in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Commenting/InlineCommentSniff.php \Drupal\Sniffs\Commenting\InlineCommentSniff::isInCodeExample()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Commenting/InlineCommentSniff.php \Drupal\Sniffs\Commenting\InlineCommentSniff::isInCodeExample()

Determines if a comment line is part of an

?>
<?php/?><?php

example.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

boolean Returns true if the comment line is within a @code block, false otherwise.

1 call to InlineCommentSniff::isInCodeExample()
InlineCommentSniff::process in coder_sniffer/Drupal/Sniffs/Commenting/InlineCommentSniff.php
Processes this test, when one of its tokens is encountered.

File

coder_sniffer/Drupal/Sniffs/Commenting/InlineCommentSniff.php, line 339

Class

InlineCommentSniff
\Drupal\Sniffs\Commenting\InlineCommentSniff.

Namespace

Drupal\Sniffs\Commenting

Code

protected function isInCodeExample(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $prevComment = $stackPtr;
  $lastComment = $stackPtr;
  while (($prevComment = $phpcsFile
    ->findPrevious(array(
    T_COMMENT,
  ), $lastComment - 1, null, false)) !== false) {
    if ($tokens[$prevComment]['line'] !== $tokens[$lastComment]['line'] - 1) {
      return false;
    }
    if ($tokens[$prevComment]['content'] === '// @code' . $phpcsFile->eolChar) {
      return true;
    }
    if ($tokens[$prevComment]['content'] === '// @endcode' . $phpcsFile->eolChar) {
      return false;
    }
    $lastComment = $prevComment;
  }
  return false;
}