You are here

public function ObjectOperatorIndentSniff::process in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php \Drupal\Sniffs\WhiteSpace\ObjectOperatorIndentSniff::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php \Drupal\Sniffs\WhiteSpace\ObjectOperatorIndentSniff::process()

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.

Return value

void

File

coder_sniffer/Drupal/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php, line 58

Class

ObjectOperatorIndentSniff
\Drupal\Sniffs\WhiteSpace\ObjectOperatorIndentSniff.

Namespace

Drupal\Sniffs\WhiteSpace

Code

public function process(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();

  // Check that there is only whitespace before the object operator and there
  // is nothing else on the line.
  if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE || $tokens[$stackPtr - 1]['column'] !== 1) {
    return;
  }
  $previousLine = $phpcsFile
    ->findPrevious(Tokens::$emptyTokens, $stackPtr - 2, null, true, null, true);
  if ($previousLine === false) {
    return;
  }

  // Check if the line before is in the same scope and go back if necessary.
  $scopeDiff = array(
    $previousLine => $previousLine,
  );
  while (empty($scopeDiff) === false) {

    // Find the first non whitespace character on the previous line.
    $startOfLine = $this
      ->findStartOfline($phpcsFile, $previousLine);
    $startParenthesis = array();
    if (isset($tokens[$startOfLine]['nested_parenthesis']) === true) {
      $startParenthesis = $tokens[$startOfLine]['nested_parenthesis'];
    }
    $operatorParenthesis = array();
    if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
      $operatorParenthesis = $tokens[$stackPtr]['nested_parenthesis'];
    }
    $scopeDiff = array_diff_assoc($startParenthesis, $operatorParenthesis);
    if (empty($scopeDiff) === false) {
      $previousLine = key($scopeDiff);
    }
  }

  // Closing parenthesis can be indented in several ways, so rather use the
  // line that opended the parenthesis.
  if ($tokens[$startOfLine]['code'] === T_CLOSE_PARENTHESIS) {
    $startOfLine = $this
      ->findStartOfline($phpcsFile, $tokens[$startOfLine]['parenthesis_opener']);
  }
  if ($tokens[$startOfLine]['code'] === T_OBJECT_OPERATOR) {

    // If there is some wrapping in function calls then there should be an
    // additional level of indentation.
    if (isset($tokens[$stackPtr]['nested_parenthesis']) === true && (empty($tokens[$startOfLine]['nested_parenthesis']) === true || $tokens[$startOfLine]['nested_parenthesis'] !== $tokens[$stackPtr]['nested_parenthesis'])) {
      $additionalIndent = 2;
    }
    else {
      $additionalIndent = 0;
    }
  }
  else {
    $additionalIndent = 2;
  }
  if ($tokens[$stackPtr]['column'] !== $tokens[$startOfLine]['column'] + $additionalIndent) {
    $error = 'Object operator not indented correctly; expected %s spaces but found %s';
    $expectedIndent = $tokens[$startOfLine]['column'] + $additionalIndent - 1;
    $data = array(
      $expectedIndent,
      $tokens[$stackPtr]['column'] - 1,
    );
    $fix = $phpcsFile
      ->addFixableError($error, $stackPtr, 'Indent', $data);
    if ($fix === true) {
      $phpcsFile->fixer
        ->replaceToken($stackPtr - 1, str_repeat(' ', $expectedIndent));
    }
  }
}