You are here

protected function Drupal_Sniffs_WhiteSpace_ScopeIndentSniff::calculateExpectedIndent in Coder 7.2

Calculates the expected indent of a token.

Returns the column at which the token should be indented to, so 1 means that the token should not be indented at all.

Parameters

array $tokens The stack of tokens for this file.:

int $stackPtr The position of the token to get indent for.:

Return value

int

1 call to Drupal_Sniffs_WhiteSpace_ScopeIndentSniff::calculateExpectedIndent()
Drupal_Sniffs_WhiteSpace_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 318

Class

Drupal_Sniffs_WhiteSpace_ScopeIndentSniff
Largely copied from Generic_Sniffs_Whitespace_ScopeIndentSniff, modified to make the exact mode working with comments and multi line statements.

Code

protected function calculateExpectedIndent(array $tokens, $stackPtr) {
  $conditionStack = array();
  $inParenthesis = false;
  if (isset($tokens[$stackPtr]['nested_parenthesis']) === true && empty($tokens[$stackPtr]['nested_parenthesis']) === false) {
    $inParenthesis = true;
  }

  // Empty conditions array (top level structure).
  if (empty($tokens[$stackPtr]['conditions']) === true) {
    if ($inParenthesis === true) {

      // Wrapped in parenthesis means it is probably in a
      // function call (like a closure) so we have to assume indent
      // is correct here and someone else will check it more
      // carefully in another sniff.
      return $tokens[$stackPtr]['column'];
    }
    else {
      return 1;
    }
  }
  $indent = 0;
  $tokenConditions = $tokens[$stackPtr]['conditions'];
  foreach ($tokenConditions as $id => $condition) {

    // If it's an indenting scope ie. it's not in our array of
    // scopes that don't indent, increase indent.
    if (in_array($condition, $this->nonIndentingScopes) === false) {
      if ($condition === T_CLOSURE && $inParenthesis === true) {

        // Closures cause problems with indents when they are
        // used as function arguments because the code inside them
        // is not technically inside the function yet, so the indent
        // is always off by one. So instead, use the
        // indent of the closure as the base value.
        $indent = $tokens[$id]['column'] - 1;
      }
      $indent += $this->indent;
    }
  }

  // Increase by 1 to indiciate that the code should start at a specific column.
  // E.g., code indented 4 spaces should start at column 5.
  $indent++;
  return $indent;
}