Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ObjectOperatorIndentSniff.php

function ObjectOperatorIndentSniff::process

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php \PHP_CodeSniffer\Standards\PEAR\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

Overrides Sniff::process

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php, line 51

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 = [
        $previousLine => $previousLine,
    ];
    $startOfLine = $stackPtr;
    while (empty($scopeDiff) === false) {
        // Find the first non whitespace character on the previous line.
        $startOfLine = $this->findStartOfline($phpcsFile, $previousLine);
        $startParenthesis = [];
        if (isset($tokens[$startOfLine]['nested_parenthesis']) === true) {
            $startParenthesis = $tokens[$startOfLine]['nested_parenthesis'];
        }
        $operatorParenthesis = [];
        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 opened 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 = [
            $expectedIndent,
            $tokens[$stackPtr]['column'] - 1,
        ];
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Indent', $data);
        if ($fix === true) {
            $phpcsFile->fixer
                ->replaceToken($stackPtr - 1, str_repeat(' ', $expectedIndent));
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal