You are here

protected function ScopeIndentSniff::adjustIndent in Coder 8.3.x

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeIndentSniff.php \Drupal\Sniffs\WhiteSpace\ScopeIndentSniff::adjustIndent()

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document.:

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

int $length The length of the new indent.:

int $change The difference in length between: the old and new indent.

Return value

bool

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

File

coder_sniffer/Drupal/Sniffs/WhiteSpace/ScopeIndentSniff.php, line 1483

Class

ScopeIndentSniff
Largely copied from \PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ScopeIndentSniff, modified to make the exact mode working with comments and multi line statements.

Namespace

Drupal\Sniffs\WhiteSpace

Code

protected function adjustIndent(File $phpcsFile, $stackPtr, $length, $change) {
  $tokens = $phpcsFile
    ->getTokens();

  // We don't adjust indents outside of PHP.
  if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) {
    return false;
  }
  $padding = '';
  if ($length > 0) {
    if ($this->tabIndent === true) {
      $numTabs = (int) floor($length / $this->tabWidth);
      if ($numTabs > 0) {
        $numSpaces = $length - $numTabs * $this->tabWidth;
        $padding = str_repeat("\t", $numTabs) . str_repeat(' ', $numSpaces);
      }
    }
    else {
      $padding = str_repeat(' ', $length);
    }
  }
  if ($tokens[$stackPtr]['column'] === 1) {
    $trimmed = ltrim($tokens[$stackPtr]['content']);
    $accepted = $phpcsFile->fixer
      ->replaceToken($stackPtr, $padding . $trimmed);
  }
  else {

    // Easier to just replace the entire indent.
    $accepted = $phpcsFile->fixer
      ->replaceToken($stackPtr - 1, $padding);
  }
  if ($accepted === false) {
    return false;
  }
  if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) {

    // We adjusted the start of a comment, so adjust the rest of it
    // as well so the alignment remains correct.
    for ($x = $stackPtr + 1; $x < $tokens[$stackPtr]['comment_closer']; $x++) {
      if ($tokens[$x]['column'] !== 1) {
        continue;
      }
      $length = 0;
      if ($tokens[$x]['code'] === T_DOC_COMMENT_WHITESPACE) {
        $length = $tokens[$x]['length'];
      }
      $padding = $length + $change;
      if ($padding > 0) {
        if ($this->tabIndent === true) {
          $numTabs = (int) floor($padding / $this->tabWidth);
          $numSpaces = $padding - $numTabs * $this->tabWidth;
          $padding = str_repeat("\t", $numTabs) . str_repeat(' ', $numSpaces);
        }
        else {
          $padding = str_repeat(' ', $padding);
        }
      }
      else {
        $padding = '';
      }
      $phpcsFile->fixer
        ->replaceToken($x, $padding);
      if ($this->debug === true) {
        $length = strlen($padding);
        $line = $tokens[$x]['line'];
        $type = $tokens[$x]['type'];
        echo "\t=> Indent adjusted to {$length} for {$type} on line {$line}" . PHP_EOL;
      }
    }

    //end for
  }

  //end if
  return true;
}