@param \PHP_CodeSniffer\Files\File $phpCsFile
@param int $methodIndex

@return void
491546
protected function assertValidDocBlock(File $phpCsFile, int $methodIndex): void
    {
        $docBlockEndIndex = $this->findRelatedDocBlock($phpCsFile, $methodIndex);
        if (!$docBlockEndIndex) {
            // Let another sniff take care of this
            return;
        }

        $tokens = $phpCsFile->getTokens();

        $docBlockStartIndex = $tokens[$docBlockEndIndex]['comment_opener'];

        for ($i = $docBlockStartIndex + 1; $i < $docBlockEndIndex; $i++) {
            if ($tokens[$i]['type'] !== 'T_DOC_COMMENT_TAG') {
                continue;
            }
            if (!in_array($tokens[$i]['content'], ['@param', '@return'], true)) {
                continue;
            }

            $classNameIndex = $i + 2;

            if ($tokens[$classNameIndex]['type'] !== 'T_DOC_COMMENT_STRING') {
                continue;
            }

            $content = $tokens[$classNameIndex]['content'];

            $spacePos = strpos($content, ' ');
            if ($spacePos) {
                $content = substr($content, 0, $spacePos);
            }
            if (substr($content, 0, 8) !== '\Spryker') {
                continue;
            }
            if (substr($content, -9) === 'Interface') {
                continue;
            }
            $pieces = explode('\\', $content);
            $lastPiece = array_pop($pieces);

            if (!$this->isRelevant($lastPiece)) {
                continue;
            }

            $error = 'Bridges should be annotated with interfaces only.';
            $phpCsFile->addError($error, $classNameIndex, 'WrongAnnotation');
        }
    }
861820
protected function checkAnnotations(File $phpCsFile, int $stackPointer): void
    {
        $tokens = $phpCsFile->getTokens();

        $docBlockEndIndex = $this->findRelatedDocBlock($phpCsFile, $stackPointer);
        if (!$docBlockEndIndex) {
            return;
        }
        $docBlockStartIndex = $tokens[$docBlockEndIndex]['comment_opener'];
        if (!$docBlockStartIndex) {
            return;
        }

        $methodAnnotations = $this->getFixableMethodAnnotations($phpCsFile, $docBlockStartIndex, $docBlockEndIndex);
        if (!$methodAnnotations) {
            return;
        }

        foreach ($methodAnnotations as $methodAnnotation) {
            $fixable = $phpCsFile->addFixableError(
                sprintf('Interface must be used for %s annotation of %s', $methodAnnotation['method'], $methodAnnotation['class']),
                $methodAnnotation['index'],
                'Annotation.Interface.Invalid'
            );
            if (!$fixable) {
                continue;
            }

            $content = $methodAnnotation['interface'] . ' ' . $methodAnnotation['method'];

            $phpCsFile->fixer->beginChangeset();
            $phpCsFile->fixer->replaceToken($methodAnnotation['index'] + 2, $content);
            $phpCsFile->fixer->endChangeset();
        }
    }
333709
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
        // Delegate the processing to specialised methods.
        $this->process_sql_as_keyword($phpcsFile, $stackPtr);
        $this->process_regexp_separator_e($phpcsFile, $stackPtr);
        $this->process_string_with_backticks($phpcsFile, $stackPtr);
    }
957083
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

        /**
         * We distinguish between the class, function and namespace names or the define statements
         */
        if ($tokens[$stackPtr]['type'] == 'T_STRING') {
            $this->processString($phpcsFile, $stackPtr, $tokens);
        } else {
            $this->processNonString($phpcsFile, $stackPtr, $tokens);
        }
    }
1202978
public function process(File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
            // Probably an interface method.
            return;
        }

        $closeBrace  = $tokens[$stackPtr]['scope_closer'];
        $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);

        // Special case for empty JS functions.
        if ($phpcsFile->tokenizerType === 'JS' && $prevContent === $tokens[$stackPtr]['scope_opener']) {
            // In this case, the opening and closing brace must be
            // right next to each other.
            if ($tokens[$stackPtr]['scope_closer'] !== ($tokens[$stackPtr]['scope_opener'] + 1)) {
                $error = 'The opening and closing braces of empty functions must be directly next to each other; e.g., function () {}';
                $fix   = $phpcsFile->addFixableError($error, $closeBrace, 'SpacingBetween');
                if ($fix === true) {
                    $phpcsFile->fixer->beginChangeset();
                    for ($i = ($tokens[$stackPtr]['scope_opener'] + 1); $i < $closeBrace; $i++) {
                        $phpcsFile->fixer->replaceToken($i, '');
                    }

                    $phpcsFile->fixer->endChangeset();
                }
            }

            return;
        }

        $nestedFunction = false;
        if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true
            || $phpcsFile->hasCondition($stackPtr, T_CLOSURE) === true
            || isset($tokens[$stackPtr]['nested_parenthesis']) === true
        ) {
            $nestedFunction = true;
        }

        $braceLine = $tokens[$closeBrace]['line'];
        $prevLine  = $tokens[$prevContent]['line'];
        $found     = ($braceLine - $prevLine - 1);

        $afterKeyword  = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
        $beforeKeyword = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
        if ($nestedFunction === true) {
            if ($found < 0) {
                $error = 'Closing brace of nested function must be on a new line';
                $fix   = $phpcsFile->addFixableError($error, $closeBrace, 'ContentBeforeClose');
                if ($fix === true) {
                    $phpcsFile->fixer->addNewlineBefore($closeBrace);
                }
            } else if ($found > $this->allowedNestedLines) {
                $error = 'Expected %s blank lines before closing brace of nested function; %s found';
                $data  = array(
                          $this->allowedNestedLines,
                          $found,
                         );
                $fix   = $phpcsFile->addFixableError($error, $closeBrace, 'SpacingBeforeNestedClose', $data);

                if ($fix === true) {
                    $phpcsFile->fixer->beginChangeset();
                    $changeMade = false;
                    for ($i = ($prevContent + 1); $i < $closeBrace; $i++) {
                        // Try to maintain indentation.
                        if ($tokens[$i]['line'] === ($braceLine - 1)) {
                            break;
                        }

                        $phpcsFile->fixer->replaceToken($i, '');
                        $changeMade = true;
                    }

                    // Special case for when the last content contains the newline
                    // token as well, like with a comment.
                    if ($changeMade === false) {
                        $phpcsFile->fixer->replaceToken(($prevContent + 1), '');
                    }

                    $phpcsFile->fixer->endChangeset();
                }//end if
            }//end if
        } else {
            if ($found !== (int) $this->allowedLines) {
                if ($this->allowedLines === 1) {
                    $plural = '';
                } else {
                    $plural = 's';
                }

                $error = 'Expected %s blank line%s before closing function brace; %s found';
                $data  = array(
                          $this->allowedLines,
                          $plural,
                          $found,
                         );
                $fix   = $phpcsFile->addFixableError($error, $closeBrace, 'SpacingBeforeClose', $data);

                if ($fix === true) {
                    if ($found > $this->allowedLines) {
                        $phpcsFile->fixer->beginChangeset();
                        for ($i = ($prevContent + 1); $i < ($closeBrace); $i++) {
                            $phpcsFile->fixer->replaceToken($i, '');
                        }

                        $phpcsFile->fixer->replaceToken(($closeBrace - 1), $phpcsFile->eolChar);
                        $phpcsFile->fixer->endChangeset();
                    } else {
                        $phpcsFile->fixer->addNewlineBefore($closeBrace);
                    }
                }
            }//end if
        }//end if

    }
892963
protected function checkBridge(File $phpCsFile, int $stackPointer): void
    {
        $index = $stackPointer;
        while ($index = $phpCsFile->findNext(T_FUNCTION, $index + 1)) {
            $methodName = $phpCsFile->getDeclarationName($index);
            if ($methodName !== '__construct') {
                continue;
            }

            $this->assertValidConstructor($phpCsFile, $index);
            $this->assertValidDocBlock($phpCsFile, $index);
            break;
        }
    }
848178
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();
        $token = $tokens[$stackPtr];
        $length = 0;
        if (isset($token['scope_opener']) === false) {
            //ignore empty methods
            return;
        }

        //get the full length of the method
        $firstToken = $tokens[$token['scope_opener']];
        $lastToken = $tokens[$token['scope_closer']];
        $length = $lastToken['line'] - $firstToken['line'];

        if ($length > $this->absoluteMaxLength) {
            $error = sprintf("Method's length (%d) exceeds maximum length of %d", $length, $this->absoluteMaxLength);
            $phpcsFile->addError($error, $stackPtr, 'MethodTooBig');
            return;
        }

        if ($length > $this->maxLength) {
            $error = sprintf("Method's length (%d) exceeds %d, consider refactoring", $length, $this->maxLength);
            $phpcsFile->addWarning($error, $stackPtr, 'MethodTooBig');
        }
    }
128511
protected function checkConstructorAndDestructor(File $phpcsFile, int $stackPtr): void
    {
        $docBlockEndIndex = $this->findRelatedDocBlock($phpcsFile, $stackPtr);
        if ($docBlockEndIndex) {
            return;
        }

        $methodSignature = $this->getMethodSignature($phpcsFile, $stackPtr);
        $arguments = count($methodSignature);
        if (!$arguments) {
            return;
        }

        $phpcsFile->addError('Missing doc block for method', $stackPtr, 'ConstructDesctructMissingDocBlock');
    }
958675
protected function loadStatements(File $phpcsFile): void
    {
        $this->className = $this->findClassName($phpcsFile);

        if ($this->existingStatements !== null) {
            return;
        }

        $existingStatements = $this->getUseStatements($phpcsFile);
        $this->existingStatements = $existingStatements;
        $this->allStatements = $existingStatements;
        $this->newStatements = [];
    }
1014610
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
            return;
        }

        if (isset($tokens[$stackPtr]['parenthesis_opener']) === true) {
            $parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
            $parenCloser = $tokens[$stackPtr]['parenthesis_closer'];

            if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE
                && !$this->isNextTokenAnException($tokens, $parenOpener + 1)
            ) {
                $gap   = strlen($tokens[($parenOpener + 1)]['content']);
                $error = 'Expected 0 spaces after opening bracket; %s found';
                $data  = array($gap);
                $phpcsFile->addError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data);
            }

            if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']
                && $tokens[($parenCloser - 1)]['code'] === T_WHITESPACE
            ) {
                $gap   = strlen($tokens[($parenCloser - 1)]['content']);
                $error = 'Expected 0 spaces before closing bracket; %s found';
                $data  = array($gap);
                $phpcsFile->addError($error, ($parenCloser - 1), 'SpaceBeforeCloseBrace', $data);
            }
        }//end if

        $scopeOpener = $tokens[$stackPtr]['scope_opener'];
        $scopeCloser = $tokens[$stackPtr]['scope_closer'];

        $firstContent = $phpcsFile->findNext(
            T_WHITESPACE,
            ($scopeOpener + 1),
            null,
            true
        );

        if ($tokens[$firstContent]['line'] !== ($tokens[$scopeOpener]['line'] + 1)) {
            $error = 'Blank line found at start of control structure';
            $phpcsFile->addError($error, $scopeOpener, 'SpacingBeforeOpen');
        }

        $lastContent = $phpcsFile->findPrevious(
            T_WHITESPACE,
            ($scopeCloser - 1),
            null,
            true
        );

        if ($tokens[$lastContent]['line'] !== ($tokens[$scopeCloser]['line'] - 1)) {
            $errorToken = $scopeCloser;
            for ($i = ($scopeCloser - 1); $i > $lastContent; $i--) {
                if ($tokens[$i]['line'] < $tokens[$scopeCloser]['line']) {
                    $errorToken = $i;
                    break;
                }
            }

            $error = 'Blank line found at end of control structure';
            $phpcsFile->addError($error, $errorToken, 'SpacingAfterClose');
        }

        $trailingContent = $phpcsFile->findNext(
            T_WHITESPACE,
            ($scopeCloser + 1),
            null,
            true
        );

        if ($tokens[$trailingContent]['code'] === T_ELSE) {
            if ($tokens[$stackPtr]['code'] === T_IF) {
                // IF with ELSE.
                return;
            }
        }

        if ($tokens[$trailingContent]['code'] === T_COMMENT) {
            if ($tokens[$trailingContent]['line'] === $tokens[$scopeCloser]['line']) {
                if (substr($tokens[$trailingContent]['content'], 0, 5) === '//end') {
                    // There is an end comment, so we have to get the next piece
                    // of content.
                    $trailingContent = $phpcsFile->findNext(
                        T_WHITESPACE,
                        ($trailingContent + 1),
                        null,
                        true
                    );
                }
            }
        }

        // If this token is closing a CASE or DEFAULT, we don't need the
        // blank line after this control structure.
        if (isset($tokens[$trailingContent]['scope_condition']) === true) {
            $condition = $tokens[$trailingContent]['scope_condition'];
            if ($tokens[$condition]['code'] === T_CASE
                || $tokens[$condition]['code'] === T_DEFAULT
            ) {
                return;
            }
        }

        if ($tokens[$trailingContent]['code'] === T_CLOSE_TAG) {
            // At the end of the script or embedded code.
            return;
        }

        if ($tokens[$trailingContent]['code'] === T_CLOSE_CURLY_BRACKET) {
            // Another control structure's closing brace.
            if (isset($tokens[$trailingContent]['scope_condition']) === true) {
                $owner = $tokens[$trailingContent]['scope_condition'];
                if ($tokens[$owner]['code'] === T_FUNCTION) {
                    // The next content is the closing brace of a function
                    // so normal function rules apply and we can ignore it.
                    return;
                }
            }

            if ($tokens[$trailingContent]['line'] !== ($tokens[$scopeCloser]['line'] + 1)) {
                $error = 'Blank line found after control structure';
                $phpcsFile->addError($error, $scopeCloser, 'LineAfterClose');
            }
        } else {
            if ($tokens[$trailingContent]['line'] === ($tokens[$scopeCloser]['line'] + 1)) {
                $error = 'No blank line found after control structure';
                $phpcsFile->addError($error, $scopeCloser, 'NoLineAfterClose');
            }
        }

    }
119272
protected function fixDocCommentOpenTags(File $phpCsFile, int $startIndex, int $endIndex): void
    {
        $tokens = $phpCsFile->getTokens();

        for ($i = $startIndex + 1; $i < $endIndex; $i++) {
            if ($tokens[$i]['code'] !== T_COMMENT) {
                continue;
            }

            if (!preg_match('|^\/\*\s*@\w+ (.+)|', $tokens[$i]['content'])) {
                continue;
            }

            $fix = $phpCsFile->addFixableError('Inline Doc Block comment should be using `/** ... */`', $i, 'InlineDocBlock');
            if ($fix) {
                $phpCsFile->fixer->beginChangeset();

                $comment = $tokens[$i]['content'];
                $comment = str_replace('/*', '/**', $comment);

                $phpCsFile->fixer->replaceToken($i, $comment);

                $phpCsFile->fixer->endChangeset();
            }
        }
    }
282923
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
        $tokens = $phpcsFile->getTokens();
        $errorData = array($tokens[$stackPtr]['content']);

        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
            $error = 'Possible parse error: %s missing opening or closing brace';
            $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $errorData);
            return;
        }

        $curlyBrace = $tokens[$stackPtr]['scope_opener'];
        $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($curlyBrace - 1), $stackPtr, true);
        $classLine = $tokens[$lastContent]['line'];
        $braceLine = $tokens[$curlyBrace]['line'];
        if ($braceLine !== $classLine) {
            $error = 'Opening brace of a %s must be on the same line as the definition';
            $phpcsFile->addError($error, $curlyBrace, 'OpenBraceNewLine', $errorData);
            return;
        }

        if ($tokens[($curlyBrace - 1)]['code'] === T_WHITESPACE) {
            $prevContent = $tokens[($curlyBrace - 1)]['content'];
            if ($prevContent === $phpcsFile->eolChar) {
                $spaces = 0;
            } else {
                $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
                $spaces = strlen($blankSpace);
            }

            $expected = 1;
            if ($spaces !== $expected) {
                $error = 'Expected %s spaces before opening brace; %s found';
                $data = array($expected, $spaces,);
                $phpcsFile->addError($error, $curlyBrace, 'SpaceBeforeBrace', $data);
            }
        }

    }
123203
public function process(File $phpcsFile, $stackPtr)
    {
        if ($this->supportsAbove('7.0') === false) {
            return;
        }

        $tokens = $phpcsFile->getTokens();
        $token  = $tokens[$stackPtr];
        // Skip function without body.
        if (isset($token['scope_opener']) === false) {
            return;
        }

        // Get all parameters from method signature.
        $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr);
        if (empty($parameters) || \is_array($parameters) === false) {
            return;
        }

        $paramNames = array();
        foreach ($parameters as $param) {
            $paramNames[] = strtolower($param['name']);
        }

        if (\count($paramNames) !== \count(array_unique($paramNames))) {
            $phpcsFile->addError(
                'Functions can not have multiple parameters with the same name since PHP 7.0',
                $stackPtr,
                'Found'
            );
        }
    }
14266
protected function checkAnnotationTagGrouping(File $phpCsFile, int $docBlockStartIndex, int $docBlockEndIndex): void
    {
        $tags = $this->readTags($phpCsFile, $docBlockStartIndex, $docBlockEndIndex);

        $currentTag = null;
        foreach ($tags as $i => $tag) {
            if ($currentTag === null) {
                $currentTag = $tag['tag'];
                continue;
            }

            if ($currentTag === $tag['tag'] || strpos($tag['tag'], $currentTag) === 0) {
                $this->assertNoSpacing($phpCsFile, $tags[$i - 1], $tag);
                continue;
            }

            $this->assertSpacing($phpCsFile, $tags[$i - 1], $tag);
            $currentTag = $tag['tag'];
        }
    }
386525
protected function detectReturnTypeVoid(File $phpcsFile, int $index): ?string
    {
        $tokens = $phpcsFile->getTokens();

        $type = 'void';

        $methodStartIndex = $tokens[$index]['scope_opener'];
        $methodEndIndex = $tokens[$index]['scope_closer'];

        for ($i = $methodStartIndex + 1; $i < $methodEndIndex; ++$i) {
            if ($this->isGivenKind([T_FUNCTION], $tokens[$i])) {
                $endIndex = $tokens[$i]['scope_closer'];
                $i = $endIndex;
                continue;
            }

            if (!$this->isGivenKind([T_RETURN], $tokens[$i])) {
                continue;
            }

            $nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $i + 1, null, true);
            if (!$this->isGivenKind(T_SEMICOLON, $tokens[$nextIndex])) {
                return null;
            }
        }

        return $type;
    }
602690
protected function findUnnecessaryTrailingComparisonEnd(File $phpcsFile, int $index): ?int
    {
        $tokens = $phpcsFile->getTokens();

        $next = (int)$phpcsFile->findNext(T_WHITESPACE, ($index + 1), null, true);
        if (!$next || !in_array($tokens[$next]['code'], [T_IS_IDENTICAL, T_IS_NOT_IDENTICAL])) {
            return null;
        }

        $next = (int)$phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), null, true);
        if (!$next || !in_array($tokens[$next]['code'], [T_TRUE, T_FALSE])) {
            return null;
        }

        return $next;
    }
926205
protected function findUnnecessaryLeadingComparisonStart(File $phpcsFile, int $index): ?int
    {
        $tokens = $phpcsFile->getTokens();

        $previous = (int)$phpcsFile->findPrevious(T_WHITESPACE, ($index - 1), null, true);
        if (!$previous || !in_array($tokens[$previous]['code'], [T_IS_IDENTICAL, T_IS_NOT_IDENTICAL])) {
            return null;
        }

        $previous = (int)$phpcsFile->findPrevious(T_WHITESPACE, ($previous - 1), null, true);
        if (!$previous || !in_array($tokens[$previous]['code'], [T_TRUE, T_FALSE])) {
            return null;
        }

        return $previous;
    }