You are here

protected function Drupal_Sniffs_Commenting_InlineCommentSniff::processIndentation in Coder 7.2

Checks the indentation level of the comment contents.

Parameters

PHP_CodeSniffer_File $phpcsFile The file being scanned.:

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

Return value

void

1 call to Drupal_Sniffs_Commenting_InlineCommentSniff::processIndentation()
Drupal_Sniffs_Commenting_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 262

Class

Drupal_Sniffs_Commenting_InlineCommentSniff
PHP_CodeSniffer_Sniffs_Drupal_Commenting_InlineCommentSniff.

Code

protected function processIndentation(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $comment = rtrim($tokens[$stackPtr]['content']);
  $spaceCount = 0;
  for ($i = 2; $i < strlen($comment); $i++) {
    if ($comment[$i] !== ' ') {
      break;
    }
    $spaceCount++;
  }
  if ($spaceCount === 0 && strlen($comment) > 2) {
    $error = 'No space before comment text; expected "// %s" but found "%s"';
    $data = array(
      substr($comment, 2),
      $comment,
    );
    $phpcsFile
      ->addError($error, $stackPtr, 'NoSpaceBefore', $data);
  }
  if ($spaceCount > 1) {

    // Check if there is a comment on the previous line that justifies the
    // indentation.
    $prevComment = $phpcsFile
      ->findPrevious(array(
      T_COMMENT,
    ), $stackPtr - 1, null, false);
    if ($prevComment !== false && $tokens[$prevComment]['line'] === $tokens[$stackPtr]['line'] - 1) {
      $prevCommentText = rtrim($tokens[$prevComment]['content']);
      $prevSpaceCount = 0;
      for ($i = 2; $i < strlen($prevCommentText); $i++) {
        if ($prevCommentText[$i] !== ' ') {
          break;
        }
        $prevSpaceCount++;
      }
      if ($spaceCount > $prevSpaceCount && $prevSpaceCount > 0) {

        // A previous comment could be a list item or @todo.
        $indentationStarters = array(
          '-',
          '@todo',
        );
        $words = preg_split('/\\s+/', $prevCommentText);
        if (in_array($words[1], $indentationStarters) === true) {
          if ($spaceCount !== $prevSpaceCount + 2) {
            $error = 'Comment indentation error after %s element, expected %s spaces';
            $phpcsFile
              ->addError($error, $stackPtr, 'SpacingBefore', array(
              $words[1],
              $prevSpaceCount + 2,
            ));
          }
        }
        else {
          $error = 'Comment indentation error, expected only %s spaces';
          $phpcsFile
            ->addError($error, $stackPtr, 'SpacingBefore', array(
            $prevSpaceCount,
          ));
        }
      }
    }
    else {
      $error = '%s spaces found before inline comment; expected "// %s" but found "%s"';
      $data = array(
        $spaceCount,
        substr($comment, 2 + $spaceCount),
        $comment,
      );
      $phpcsFile
        ->addError($error, $stackPtr, 'SpacingBefore', $data);
    }

    //end if
  }

  //end if
}