You are here

protected function InlineCommentSniff::processIndentation in Coder 8.2

Checks the indentation level of the comment contents.

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

void

1 call to InlineCommentSniff::processIndentation()
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 374

Class

InlineCommentSniff
\Drupal\Sniffs\Commenting\InlineCommentSniff.

Namespace

Drupal\Sniffs\Commenting

Code

protected function processIndentation(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $comment = rtrim($tokens[$stackPtr]['content']);
  $spaceCount = 0;
  $tabFound = false;
  $commentLength = strlen($comment);
  for ($i = 2; $i < $commentLength; $i++) {
    if ($comment[$i] === "\t") {
      $tabFound = true;
      break;
    }
    if ($comment[$i] !== ' ') {
      break;
    }
    $spaceCount++;
  }
  $fix = false;
  if ($tabFound === true) {
    $error = 'Tab found before comment text; expected "// %s" but found "%s"';
    $data = array(
      ltrim(substr($comment, 2)),
      $comment,
    );
    $fix = $phpcsFile
      ->addFixableError($error, $stackPtr, 'TabBefore', $data);
  }
  else {
    if ($spaceCount === 0 && strlen($comment) > 2) {
      $error = 'No space found before comment text; expected "// %s" but found "%s"';
      $data = array(
        substr($comment, 2),
        $comment,
      );
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr, 'NoSpaceBefore', $data);
    }
  }

  //end if
  if ($fix === true) {
    $newComment = '// ' . ltrim($tokens[$stackPtr]['content'], "/\t ");
    $phpcsFile->fixer
      ->replaceToken($stackPtr, $newComment);
  }
  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);
        $numberedList = (bool) preg_match('/^[0-9]+\\./', $words[1]);
        if (in_array($words[1], $indentationStarters) === true) {
          if ($spaceCount !== $prevSpaceCount + 2) {
            $error = 'Comment indentation error after %s element, expected %s spaces';
            $fix = $phpcsFile
              ->addFixableError($error, $stackPtr, 'SpacingBefore', array(
              $words[1],
              $prevSpaceCount + 2,
            ));
            if ($fix === true) {
              $newComment = '//' . str_repeat(' ', $prevSpaceCount + 2) . ltrim($tokens[$stackPtr]['content'], "/\t ");
              $phpcsFile->fixer
                ->replaceToken($stackPtr, $newComment);
            }
          }
        }
        else {
          if ($numberedList === true) {
            $expectedSpaceCount = $prevSpaceCount + strlen($words[1]) + 1;
            if ($spaceCount !== $expectedSpaceCount) {
              $error = 'Comment indentation error, expected %s spaces';
              $fix = $phpcsFile
                ->addFixableError($error, $stackPtr, 'SpacingBefore', array(
                $expectedSpaceCount,
              ));
              if ($fix === true) {
                $newComment = '//' . str_repeat(' ', $expectedSpaceCount) . ltrim($tokens[$stackPtr]['content'], "/\t ");
                $phpcsFile->fixer
                  ->replaceToken($stackPtr, $newComment);
              }
            }
          }
          else {
            $error = 'Comment indentation error, expected only %s spaces';
            $phpcsFile
              ->addError($error, $stackPtr, 'SpacingBefore', array(
              $prevSpaceCount,
            ));
          }
        }

        //end if
      }

      //end if
    }
    else {
      $error = '%s spaces found before inline comment; expected "// %s" but found "%s"';
      $data = array(
        $spaceCount,
        substr($comment, 2 + $spaceCount),
        $comment,
      );
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr, 'SpacingBefore', $data);
      if ($fix === true) {
        $phpcsFile->fixer
          ->replaceToken($stackPtr, '// ' . substr($comment, 2 + $spaceCount) . $phpcsFile->eolChar);
      }
    }

    //end if
  }

  //end if
}