Commit 4d110e46 authored by Klaus Purer's avatar Klaus Purer
Browse files

fix(MultipleStatementAlignment): Fix false positive when equal sign aligned...

fix(MultipleStatementAlignment): Fix false positive when equal sign aligned correctly with 2 spaces, synced sniff code from upstream (#2872220)
parent 79d8a5fa
Loading
Loading
Loading
Loading
+77 −11
Original line number Diff line number Diff line
@@ -56,18 +56,66 @@ class MultipleStatementAlignmentSniff extends GenericMultipleStatementAlignmentS
        $stopped     = null;
        $lastCode    = $stackPtr;
        $lastSemi    = null;
        $arrayEnd    = null;

        if ($end === null) {
            $end = $phpcsFile->numTokens;
        }

        $find = Tokens::$assignmentTokens;
        unset($find[T_DOUBLE_ARROW]);

        for ($assign = $stackPtr; $assign < $phpcsFile->numTokens; $assign++) {
        $scopes = Tokens::$scopeOpeners;
        unset($scopes[T_CLOSURE]);
        unset($scopes[T_ANON_CLASS]);
        unset($scopes[T_OBJECT]);

        for ($assign = $stackPtr; $assign < $end; $assign++) {
            if ($tokens[$assign]['level'] < $tokens[$stackPtr]['level']) {
                // Statement is in a different context, so the block is over.
                break;
            }

            if (isset($scopes[$tokens[$assign]['code']]) === true
                && isset($tokens[$assign]['scope_opener']) === true
                && $tokens[$assign]['level'] === $tokens[$stackPtr]['level']
            ) {
                break;
            }

            if ($assign === $arrayEnd) {
                $arrayEnd = null;
            }

            if (isset($find[$tokens[$assign]['code']]) === false) {
                // A blank line indicates that the assignment block has ended.
                if (isset(Tokens::$emptyTokens[$tokens[$assign]['code']]) === false) {
                    if (($tokens[$assign]['line'] - $tokens[$lastCode]['line']) > 1) {
                if (isset(Tokens::$emptyTokens[$tokens[$assign]['code']]) === false
                    && ($tokens[$assign]['line'] - $tokens[$lastCode]['line']) > 1
                    && $tokens[$assign]['level'] === $tokens[$stackPtr]['level']
                    && $arrayEnd === null
                ) {
                    break;
                }

                if ($tokens[$assign]['code'] === T_CLOSE_TAG) {
                    // Breaking out of PHP ends the assignment block.
                    break;
                }

                if ($tokens[$assign]['code'] === T_OPEN_SHORT_ARRAY
                    && isset($tokens[$assign]['bracket_closer']) === true
                ) {
                    $arrayEnd = $tokens[$assign]['bracket_closer'];
                }

                if ($tokens[$assign]['code'] === T_ARRAY
                    && isset($tokens[$assign]['parenthesis_opener']) === true
                    && isset($tokens[$tokens[$assign]['parenthesis_opener']]['parenthesis_closer']) === true
                ) {
                    $arrayEnd = $tokens[$tokens[$assign]['parenthesis_opener']]['parenthesis_closer'];
                }

                if (isset(Tokens::$emptyTokens[$tokens[$assign]['code']]) === false) {
                    $lastCode = $assign;

                    if ($tokens[$assign]['code'] === T_SEMICOLON) {
@@ -78,7 +126,7 @@ class MultipleStatementAlignmentSniff extends GenericMultipleStatementAlignmentS
                            } else {
                                $lastSemi = $assign;
                            }
                        } else {
                        } else if ($tokens[$assign]['level'] < $tokens[$stackPtr]['level']) {
                            // Statement is in a different context, so the block is over.
                            break;
                        }
@@ -93,9 +141,22 @@ class MultipleStatementAlignmentSniff extends GenericMultipleStatementAlignmentS
            }//end if

            if ($assign !== $stackPtr) {
                if ($tokens[$assign]['level'] > $tokens[$stackPtr]['level']) {
                    // Has to be nested inside the same conditions as the first assignment.
                if ($tokens[$assign]['conditions'] !== $tokens[$stackPtr]['conditions']) {
                    // We've gone one level down, so process this new block.
                    $assign   = $this->checkAlignment($phpcsFile, $assign);
                    $lastCode = $assign;
                    continue;
                } else if ($tokens[$assign]['level'] < $tokens[$stackPtr]['level']) {
                    // We've gone one level up, so the block we are processing is done.
                    break;
                } else if ($arrayEnd !== null) {
                    // Assignments inside arrays are not part of
                    // the original block, so process this new block.
                    $assign   = ($this->checkAlignment($phpcsFile, $assign, $arrayEnd) - 1);
                    $arrayEnd = null;
                    $lastCode = $assign;
                    continue;
                }

                // Make sure it is not assigned inside a condition (eg. IF, FOR).
@@ -121,12 +182,17 @@ class MultipleStatementAlignmentSniff extends GenericMultipleStatementAlignmentS
            $varEnd    = $tokens[($var + 1)]['column'];
            $assignLen = $tokens[$assign]['length'];
            if ($assign !== $stackPtr) {
                if ($prevAssign === null) {
                    // Processing an inner block but no assignments found.
                    break;
                }

                if (($varEnd + 1) > $assignments[$prevAssign]['assign_col']) {
                    $padding      = 1;
                    $assignColumn = ($varEnd + 1);
                } else {
                    $padding = ($assignments[$prevAssign]['assign_col'] - $varEnd + $assignments[$prevAssign]['assign_len'] - $assignLen);
                    if ($padding === 0) {
                    if ($padding <= 0) {
                        $padding = 1;
                    }

@@ -212,7 +278,7 @@ class MultipleStatementAlignmentSniff extends GenericMultipleStatementAlignmentS
                continue;
            }

            if ($alignRight === false) {
            if ($alignRight === false && $data['found'] !== $data['expected']) {
                $data['expected'] = 1;
            }

@@ -276,7 +342,7 @@ class MultipleStatementAlignmentSniff extends GenericMultipleStatementAlignmentS
        if ($stopped !== null) {
            return $this->checkAlignment($phpcsFile, $stopped);
        } else {
            return $assignment;
            return $assign;
        }

    }//end checkAlignment()
+6 −0
Original line number Diff line number Diff line
@@ -29,3 +29,9 @@ function example_form_submit_redirect_handler($form, &$form_state) {
  $_SESSION['file_upload_session'] = TRUE;
  $form_state['redirect']  = 'example/' . $selected_content . '/' . $form_state['nid'] . '/mapping';
}

$my_first_variable  = 'some value';
$my_second_variable = 'another value';

$one = 1;
$one_more = 2;
+8 −2
Original line number Diff line number Diff line
@@ -29,3 +29,9 @@ function example_form_submit_redirect_handler($form, &$form_state) {
  $_SESSION['file_upload_session'] = TRUE;
  $form_state['redirect'] = 'example/' . $selected_content . '/' . $form_state['nid'] . '/mapping';
}

$my_first_variable  = 'some value';
$my_second_variable = 'another value';

$one = 1;
$one_more = 2;
+0 −1
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ class MultipleStatementAlignmentUnitTest extends CoderSniffUnitTest
    {
        return [
            8  => 1,
            10 => 1,
            11 => 1,
            13 => 1,
            14 => 1,