Detect indented and/or non-stand alone closing markers.

@param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
@param int                   $stackPtr  The position of the current token in the
stack passed in $tokens.

@return void
433147
protected function detectIndentedNonStandAloneClosingMarker(File $phpcsFile, $stackPtr)
    {
        $tokens            = $phpcsFile->getTokens();
        $indentError       = 'Heredoc/nowdoc with an indented closing marker is not supported in PHP 7.2 or earlier.';
        $indentErrorCode   = 'IndentedClosingMarker';
        $trailingError     = 'Having code - other than a semi-colon or new line - after the closing marker of a heredoc/nowdoc is not supported in PHP 7.2 or earlier.';
        $trailingErrorCode = 'ClosingMarkerNoNewLine';

        if (version_compare(\PHP_VERSION_ID, '70299', '>') === true) {

            /*
             * Check for indented closing marker.
             */
            if (ltrim($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) {
                $phpcsFile->addError($indentError, $stackPtr, $indentErrorCode);
            }

            /*
             * Check for tokens after the closing marker.
             */
            $nextNonWhitespace = $phpcsFile->findNext(array(\T_WHITESPACE, \T_SEMICOLON), ($stackPtr + 1), null, true);
            if ($tokens[$stackPtr]['line'] === $tokens[$nextNonWhitespace]['line']) {
                $phpcsFile->addError($trailingError, $stackPtr, $trailingErrorCode);
            }
        } else {
            // For PHP < 7.3, we're only interested in T_STRING tokens.
            if ($tokens[$stackPtr]['code'] !== \T_STRING) {
                return;
            }

            if (preg_match('`^<<<([\'"]?)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\1[\r\n]+`', $tokens[$stackPtr]['content'], $matches) !== 1) {
                // Not the start of a PHP 7.3 flexible heredoc/nowdoc.
                return;
            }

            $identifier = $matches[2];

            for ($i = ($stackPtr + 1); $i <= $phpcsFile->numTokens; $i++) {
                if ($tokens[$i]['code'] !== \T_ENCAPSED_AND_WHITESPACE) {
                    continue;
                }

                $trimmed = ltrim($tokens[$i]['content']);

                if (strpos($trimmed, $identifier) !== 0) {
                    continue;
                }

                // OK, we've found the PHP 7.3 flexible heredoc/nowdoc closing marker.

                /*
                 * Check for indented closing marker.
                 */
                if ($trimmed !== $tokens[$i]['content']) {
                    // Indent found before closing marker.
                    $phpcsFile->addError($indentError, $i, $indentErrorCode);
                }

                /*
                 * Check for tokens after the closing marker.
                 */
                // Remove the identifier.
                $afterMarker = substr($trimmed, \strlen($identifier));
                // Remove a potential semi-colon at the beginning of what's left of the string.
                $afterMarker = ltrim($afterMarker, ';');
                // Remove new line characters at the end of the string.
                $afterMarker = rtrim($afterMarker, "\r\n");

                if ($afterMarker !== '') {
                    $phpcsFile->addError($trailingError, $i, $trailingErrorCode);
                }

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

		if ($tokens[($stackPtr + 1)]['code'] == T_WHITESPACE && $tokens[($stackPtr + 2)]['code'] == T_IF) {
			$message = 'Detected else if. Use elseif instead.';
			$phpcsFile->addError($message, $stackPtr, 'Missing');
		}

	}
974098
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
	{
		$tokens = $phpcsFile->getTokens();
		$className = $phpcsFile->findNext(T_STRING, $stackPtr);
		$name      = trim($tokens[$className]['content']);
		$errorData = array(ucfirst($tokens[$stackPtr]['content']));

		$endsWithInterface = substr($name, -5) == 'Trait';
		$isClass = $tokens[$stackPtr]['code'] === T_CLASS;

		if ($isClass && $endsWithInterface) {
			$error = '%s name must not end with "Trait"';
			$phpcsFile->addError($error, $stackPtr, 'EndNotWithTrait', $errorData);
		}

		if (!$isClass && !$endsWithInterface) {
			$error = '%s name must end with "Trait"';
			$phpcsFile->addError($error, $stackPtr, 'EndWithTrait', $errorData);
		}
	}
1196437
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();
        $stackPrev = $stackPtr - 1;
        $stackNext = $stackPtr + 1;

        // One space after the namespace keyword.
        if ($tokens[$stackNext]['content'] !== ' ') {
            $error = 'There must be a single space after the NAMESPACE keyword';
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterNamespace');
            if ($fix === true) {
                $phpcsFile->fixer->replaceToken($stackNext, ' ');
            }
        }

        // Find the previous token that is NOT whitespace
        $lastNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, $stackPrev, null, true);

        // should not happen; something is horribly wrong
        if ($lastNonWhitespace === false) {
            return;
        }

        $diff = ($tokens[$stackPtr]['line'] - $tokens[$lastNonWhitespace]['line']);

        if ($diff !== 2) {
            $error = 'There must be one blank line before the namespace declaration';
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineBefore');

            if ($fix === true) {
                /*
                 * If there is less than a 2 line difference, add newlines in.
                 * If there is more than a 2 line difference, take newlines out.
                 */
                if ($diff < 2) {
                    $phpcsFile->fixer->beginChangeset();
                    for ($i = 0; $i < (2 - $diff); $i++) {
                        $phpcsFile->fixer->addNewlineBefore($stackPtr);
                    }
                    $phpcsFile->fixer->endChangeset();
                } else {
                    /*
                     * Remove all whitespace between the namespace and the previous non-whitespace
                     * tokens. If a newline was removed from the same line as the previous
                     * non-whitespace token, we need to make sure to add that back in.
                     */
                    $phpcsFile->fixer->beginChangeset();
                    $newlineRemoved = false;
                    for ($i = ($lastNonWhitespace + 1); $i < $stackPtr; $i++) {
                        if ($tokens[$lastNonWhitespace]['line'] === $tokens[$i]['line']) {
                            $newlineRemoved = true;
                        }
                        $phpcsFile->fixer->replaceToken($i, '');
                    }
                    if ($newlineRemoved) {
                        $phpcsFile->fixer->addNewline($lastNonWhitespace);
                    }
                    $phpcsFile->fixer->addNewline($lastNonWhitespace);
                    $phpcsFile->fixer->endChangeset();
                }
            }
        } elseif ($tokens[$stackPtr]['column'] !== 1) {
            $error = 'Line indented incorrectly; expected 0 spaces, found '.($tokens[$stackPtr]['column'] - 1);
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeNamespace');

            if ($fix === true) {
                $content = explode("\n", $tokens[$stackPrev]['content']);
                $content[count($content) - 1] = '';
                $phpcsFile->fixer->replaceToken($stackPrev, implode("\n", $content));
            }
        }
    }
902021
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
	{
		$tokens = $phpcsFile->getTokens();
		// Single space after the keyword.
		$found = 1;
		if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
			$found = 0;
		} else if ( ! in_array($tokens[($stackPtr + 1)]['content'], [' ', $phpcsFile->eolChar])) {
			$found = strlen($tokens[($stackPtr + 1)]['content']);
		}
		if ($found !== 1) {
			$error = 'Expected 1 space after %s keyword; %s found';
			$data  = array(
				strtoupper($tokens[$stackPtr]['content']),
				$found,
			);
			$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword', $data);
			if ($fix === true) {
				if ($found === 0) {
					$phpcsFile->fixer->addContent($stackPtr, ' ');
				} else {
					$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
				}
			}
		}
		// Single space after closing parenthesis.
		if (isset($tokens[$stackPtr]['parenthesis_closer']) === true
			&& isset($tokens[$stackPtr]['scope_opener']) === true
		) {
			$closer  = $tokens[$stackPtr]['parenthesis_closer'];
			$opener  = $tokens[$stackPtr]['scope_opener'];
			$content = $phpcsFile->getTokensAsString(($closer + 1), ($opener - $closer - 1));

			if (isset($content[0]) && $content[0] !== $phpcsFile->eolChar) {
				$error = 'Expected 1 new line after closing parenthesis; found "%s"';
				$data  = array(str_replace($phpcsFile->eolChar, '\n', $content));
				$fix   = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseParenthesis', $data);
				if ($fix === true) {
					if ($closer === ($opener - 1)) {
						$phpcsFile->fixer->addContent($closer, ' ');
					} else {
						$phpcsFile->fixer->beginChangeset();
						for ($i = ($closer + 1); $i < $opener; $i++) {
							$phpcsFile->fixer->replaceToken($i, '');
						}
						$phpcsFile->fixer->addContent($closer, ' ');
						$phpcsFile->fixer->endChangeset();
					}
				}
			}
		}//end if
		// Single newline after opening brace.
		if (isset($tokens[$stackPtr]['scope_opener']) === true) {
			$opener = $tokens[$stackPtr]['scope_opener'];
			$next   = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true);
			$found  = ($tokens[$next]['line'] - $tokens[$opener]['line']);
			if ($found !== 1) {
				$error = 'Expected 1 newline after opening brace; %s found';
				$data  = array($found);
				$fix   = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace', $data);
				if ($fix === true) {
					$phpcsFile->fixer->beginChangeset();
					for ($i = ($opener + 1); $i < $next; $i++) {
						if ($found > 0 && $tokens[$i]['line'] === $tokens[$next]['line']) {
							break;
						}
						$phpcsFile->fixer->replaceToken($i, '');
					}
					$phpcsFile->fixer->addContent($opener, $phpcsFile->eolChar);
					$phpcsFile->fixer->endChangeset();
				}
			}
		} else if ($tokens[$stackPtr]['code'] === T_WHILE) {
			// Zero spaces after parenthesis closer.
			$closer = $tokens[$stackPtr]['parenthesis_closer'];
			$found  = 0;
			if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) {
				if (strpos($tokens[($closer + 1)]['content'], $phpcsFile->eolChar) !== false) {
					$found = 'newline';
				} else {
					$found = strlen($tokens[($closer + 1)]['content']);
				}
			}
			if ($found !== 0) {
				$error = 'Expected 0 spaces before semicolon; %s found';
				$data  = array($found);
				$fix   = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data);
				if ($fix === true) {
					$phpcsFile->fixer->replaceToken(($closer + 1), '');
				}
			}
		}//end if
		// Only want to check multi-keyword structures from here on.
		if ($tokens[$stackPtr]['code'] === T_TRY
			|| $tokens[$stackPtr]['code'] === T_DO
		) {
			$closer = $tokens[$stackPtr]['scope_closer'];
		} else if ($tokens[$stackPtr]['code'] === T_ELSE
			|| $tokens[$stackPtr]['code'] === T_ELSEIF
		) {
			$closer = $phpcsFile->findPrevious(T_CLOSE_CURLY_BRACKET, ($stackPtr - 1));
		} else {
			return;
		}
		// Single space after closing brace.
		$found = 1;
		if ($tokens[($closer + 1)]['code'] !== T_WHITESPACE) {
			$found = 0;
		} else if ( ! in_array($tokens[($closer + 1)]['content'], [' ', $phpcsFile->eolChar])) {
			if (strpos($tokens[($closer + 1)]['content'], $phpcsFile->eolChar) !== false) {
				$found = 'newline';
			} else {
				$found = strlen($tokens[($closer + 1)]['content']);
			}
		}
		if ($found !== 1) {
			$error = 'Expected 1 space after closing brace; %s found';
			$data  = array($found);
			$fix   = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseBrace', $data);
			if ($fix === true) {
				if ($found === 0) {
					$phpcsFile->fixer->addContent($closer, ' ');
				} else {
					$phpcsFile->fixer->replaceToken(($closer + 1), ' ');
				}
			}
		}
	}
351940
protected function checkTryToken(File $phpcsFile, int $stackPtr): void
    {
        $tokens = $phpcsFile->getTokens();

        if ($tokens[$stackPtr]['code'] !== T_TRY) {
            return;
        }

        $this->expectSingleSpaceAfter($phpcsFile, $stackPtr, 'try');
    }
138727
public function process(File $phpcsFile, $stackPtr)
    {
        if ($this->bowOutEarly() === true) {
            return;
        }

        $tokens = $phpcsFile->getTokens();

        if ($tokens[$stackPtr]['code'] === \T_OPEN_SHORT_ARRAY
            && $this->isShortList($phpcsFile, $stackPtr) === false
        ) {
            // Short array, not short list.
            return;
        }

        if ($tokens[$stackPtr]['code'] === \T_LIST) {
            $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
            if ($nextNonEmpty === false
                || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS
                || isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false
            ) {
                // Parse error or live coding.
                return;
            }

            $opener = $nextNonEmpty;
            $closer = $tokens[$nextNonEmpty]['parenthesis_closer'];
        } else {
            // Short list syntax.
            $opener = $stackPtr;

            if (isset($tokens[$stackPtr]['bracket_closer'])) {
                $closer = $tokens[$stackPtr]['bracket_closer'];
            }
        }

        if (isset($opener, $closer) === false) {
            return;
        }

        $this->examineList($phpcsFile, $opener, $closer);
    }
899629
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens    = $phpcsFile->getTokens();
        $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);

        if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS)
        {
            $error = '"%s" is a statement not a function; no parentheses are required';
            $data  = [$tokens[$stackPtr]['content']];
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'BracketsNotRequired', $data);

            if ($fix === true)
            {
                $end      = $phpcsFile->findEndOfStatement($nextToken);
                $ignore   = PHP_CodeSniffer_Tokens::$emptyTokens;
                $ignore[] = T_SEMICOLON;
                $closer   = $phpcsFile->findPrevious($ignore, ($end - 1), null, true);

                $phpcsFile->fixer->beginChangeset();
                $phpcsFile->fixer->replaceToken($nextToken, '');

                if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE)
                {
                    $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
                }

                if ($tokens[$closer]['code'] === T_CLOSE_PARENTHESIS)
                {
                    $phpcsFile->fixer->replaceToken($closer, '');
                }

                $phpcsFile->fixer->addContent($stackPtr, ' ');
                $phpcsFile->fixer->endChangeset();
            }
        }
    }
1107269
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();
        $token = $tokens[$stackPtr];

        if (!array_key_exists('scope_opener', $token) || !array_key_exists('scope_closer', $token)) {
            return;
        }

        $scopeOpener = $token['scope_opener'];
        $firstContent = $phpcsFile->findNext(T_WHITESPACE, $scopeOpener + 1, null, true);

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

        $scopeCloser = $token['scope_closer'];
        $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $scopeCloser - 1, null, true);

        if ($tokens[$lastContent]['line'] !== $tokens[$scopeCloser]['line'] - 1) {
            $phpcsFile->addError('Blank line found at end of control structure', $lastContent + 2, 'SpacingAfterClose');
        }
    }
634230
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
        $this->currentFile = $phpcsFile;

        // We are only interested if this is the first open tag.
        if ($stackPtr !== 0) {
            if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
                return;
            }
        }

        $tokens = $phpcsFile->getTokens();

        // Find the next non whitespace token.
        $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);

        // Allow declare() statements at the top of the file.
        if ($tokens[$commentStart]['code'] === T_DECLARE) {
            $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($commentStart + 1));
            $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($semicolon + 1), null, true);
        }

        // Ignore vim header.
        if ($tokens[$commentStart]['code'] === T_COMMENT) {
            if (strstr($tokens[$commentStart]['content'], 'vim:') !== false) {
                $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($commentStart + 1), null, true);
            }
        }

        $errorToken = ($stackPtr + 1);
        if (isset($tokens[$errorToken]) === false) {
            $errorToken--;
        }

        if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
            // We are only interested if this is the first open tag.
            return;
        } else if ($tokens[$commentStart]['code'] === T_COMMENT) {
            $error = 'You must use "/**" style comments for a file comment';
            $phpcsFile->addError($error, $errorToken, 'WrongStyle');
            return;
        } else if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT) {
            $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing');
            return;
        } else {

            // Extract the header comment docblock.
            $commentEnd = $phpcsFile->findNext(T_DOC_COMMENT, ($commentStart + 1), null, true);

            $commentEnd--;

            // Check if there is only 1 doc comment between the
            // open tag and class token.
            $nextToken = array(T_ABSTRACT, T_CLASS, T_FUNCTION, T_DOC_COMMENT,);

            $commentNext = $phpcsFile->findNext($nextToken, ($commentEnd + 1));
            if ($commentNext !== false && $tokens[$commentNext]['code'] !== T_DOC_COMMENT) {
                // Found a class token right after comment doc block.
                $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $commentNext, false,
                    $phpcsFile->eolChar);

                if ($newlineToken !== false) {
                    $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $commentNext, false,
                        $phpcsFile->eolChar);

                    if ($newlineToken === false) {
                        // No blank line between the class token and the doc block.
                        // The doc block is most likely a class comment.
                        $error = 'Missing file doc comment';
                        $phpcsFile->addError($error, $errorToken, 'Missing');
                        return;
                    }
                }
            }//end if

            $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));

            // Parse the header comment docblock.
            try {
                $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
                $this->commentParser->parse();
            } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
                $line = ($e->getLineWithinComment() + $commentStart);
                $phpcsFile->addError($e->getMessage(), $line, 'FailedParse');
                return;
            }

            $comment = $this->commentParser->getComment();
            if (is_null($comment) === true) {
                $error = 'File doc comment is empty';
                $phpcsFile->addError($error, $commentStart, 'Empty');
                return;
            }

            // No extra newline before short description.
            $short = $comment->getShortComment();
            $newlineCount = 0;
            $newlineSpan = strspn($short, $phpcsFile->eolChar);
            if ($short !== '' && $newlineSpan > 0) {
                $error = 'Extra newline(s) found before file comment short description';
                $phpcsFile->addError($error, ($commentStart + 1), 'SpacingBefore');
            }

            $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);

            // Exactly one blank line between short and long description.
            $long = $comment->getLongComment();
            if (empty($long) === false) {
                $between = $comment->getWhiteSpaceBetween();
                $newlineBetween = substr_count($between, $phpcsFile->eolChar);
                if ($newlineBetween !== 2) {
                    $error = 'There must be exactly one blank line between descriptions in file comment';
                    $phpcsFile->addError($error, ($commentStart + $newlineCount + 1), 'DescriptionSpacing');
                }

                $newlineCount += $newlineBetween;
            }

            // Exactly one blank line before tags.
            $tags = $this->commentParser->getTagOrders();
            if (count($tags) > 1) {
                $newlineSpan = $comment->getNewlineAfter();
                if ($newlineSpan !== 2) {
                    $error = 'There must be exactly one blank line before the tags in file comment';
                    if ($long !== '') {
                        $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
                    }

                    $phpcsFile->addError($error, ($commentStart + $newlineCount), 'SpacingBeforeTags');
                    $short = rtrim($short, $phpcsFile->eolChar . ' ');
                }
            }

            // Check the PHP Version.
            $this->processPHPVersion($commentStart, $commentEnd, $long);

            // Check each tag.
            $this->processTags($commentStart, $commentEnd);
        }//end if

    }
1039275
protected function checkUseForReturnTypeHint(File $phpcsFile, int $stackPtr): void
    {
        $tokens = $phpcsFile->getTokens();

        $openParenthesisIndex = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr + 1);
        $closeParenthesisIndex = $tokens[$openParenthesisIndex]['parenthesis_closer'];

        $colonIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $closeParenthesisIndex + 1, null, true);
        if (!$colonIndex) {
            return;
        }

        $startIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $colonIndex + 1, $colonIndex + 3, true);
        if (!$startIndex) {
            return;
        }

        if ($tokens[$startIndex]['type'] === 'T_NULLABLE') {
            $startIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $startIndex + 1, $startIndex + 3, true);
        }

        $lastIndex = null;
        $j = $startIndex;
        $extractedUseStatement = '';
        $lastSeparatorIndex = null;
        while (true) {
            if (!$this->isGivenKind([T_NS_SEPARATOR, T_STRING, T_RETURN_TYPE], $tokens[$j])) {
                break;
            }

            $lastIndex = $j;
            $extractedUseStatement .= $tokens[$j]['content'];
            if ($this->isGivenKind([T_NS_SEPARATOR], $tokens[$j])) {
                $lastSeparatorIndex = $j;
            }
            ++$j;
        }

        if ($lastIndex === null || $lastSeparatorIndex === null) {
            return;
        }

        $extractedUseStatement = ltrim($extractedUseStatement, '\\');
        $className = '';
        for ($k = $lastSeparatorIndex + 1; $k <= $lastIndex; ++$k) {
            $className .= $tokens[$k]['content'];
        }

        $error = 'Use statement ' . $extractedUseStatement . ' for class ' . $className . ' should be in use block.';
        $fix = $phpcsFile->addFixableError($error, $colonIndex, 'ReturnSignature');
        if (!$fix) {
            return;
        }

        $phpcsFile->fixer->beginChangeset();

        $firstSeparatorIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $startIndex, null, true);

        $addedUseStatement = $this->addUseStatement($phpcsFile, $className, $extractedUseStatement);

        for ($k = $lastSeparatorIndex; $k > $firstSeparatorIndex; --$k) {
            $phpcsFile->fixer->replaceToken($k, '');
        }
        $phpcsFile->fixer->replaceToken($firstSeparatorIndex, '');

        if ($addedUseStatement['alias'] !== null) {
            $phpcsFile->fixer->replaceToken($lastIndex, $addedUseStatement['alias']);
        }

        $phpcsFile->fixer->endChangeset();
    }
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');
        }
    }
1144415
public function process(File $phpcsFile, $stackPtr)
    {
        $tokens    = $phpcsFile->getTokens();
        $tokenizer = $phpcsFile->tokenizerType;

        if ($tokens[$stackPtr]['code'] === T_INLINE_THEN) {
            $end = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
            if ($tokens[$end]['code'] !== T_CLOSE_PARENTHESIS) {
                // This inline IF statement does not have its condition
                // bracketed, so we need to guess where it starts.
                for ($i = ($end - 1); $i >= 0; $i--) {
                    if ($tokens[$i]['code'] === T_SEMICOLON) {
                        // Stop here as we assume it is the end
                        // of the previous statement.
                        break;
                    } else if ($tokens[$i]['code'] === T_OPEN_TAG) {
                        // Stop here as this is the start of the file.
                        break;
                    } else if ($tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET) {
                        // Stop if this is the closing brace of
                        // a code block.
                        if (isset($tokens[$i]['scope_opener']) === true) {
                            break;
                        }
                    } else if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) {
                        // Stop if this is the opening brace of
                        // a code block.
                        if (isset($tokens[$i]['scope_closer']) === true) {
                            break;
                        }
                    }
                }//end for

                $start = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
            } else {
                if (isset($tokens[$end]['parenthesis_opener']) === false) {
                    return;
                }

                $start = $tokens[$end]['parenthesis_opener'];
            }//end if
        } else if ($tokens[$stackPtr]['code'] === T_FOR) {
            if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
                return;
            }

            $openingBracket = $tokens[$stackPtr]['parenthesis_opener'];
            $closingBracket = $tokens[$stackPtr]['parenthesis_closer'];

            $start = $phpcsFile->findNext(T_SEMICOLON, $openingBracket, $closingBracket);
            $end   = $phpcsFile->findNext(T_SEMICOLON, ($start + 1), $closingBracket);
            if ($start === false || $end === false) {
                return;
            }
        } else {
            if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
                return;
            }

            $start = $tokens[$stackPtr]['parenthesis_opener'];
            $end   = $tokens[$stackPtr]['parenthesis_closer'];
        }//end if

        $requiredOps = 0;
        $foundOps    = 0;
        $foundBools  = 0;

        for ($i = $start; $i <= $end; $i++) {
            $type = $tokens[$i]['code'];
            if (in_array($type, array_keys(self::$invalidOps[$tokenizer])) === true) {
                $error = 'Operator %s prohibited; use %s instead';
                $data  = [
                    $tokens[$i]['content'],
                    self::$invalidOps[$tokenizer][$type],
                ];
                $phpcsFile->addError($error, $i, 'NotAllowed', $data);
                $foundOps++;
            } else if (in_array($type, self::$validOps) === true) {
                $foundOps++;
            }

            if ($tokens[$i]['code'] === T_TRUE || $tokens[$i]['code'] === T_FALSE) {
                $foundBools++;
            }

            if ($phpcsFile->tokenizerType !== 'JS'
                && ($tokens[$i]['code'] === T_BOOLEAN_AND
                || $tokens[$i]['code'] === T_BOOLEAN_OR)
            ) {
                $requiredOps++;

                // When the instanceof operator is used with another operator
                // like ===, you can get more ops than are required.
                if ($foundOps > $requiredOps) {
                    $foundOps = $requiredOps;
                }

                // If we get to here and we have not found the right number of
                // comparison operators, then we must have had an implicit
                // true operation i.e., if ($a) instead of the required
                // if ($a === true), so let's add an error.
                if ($requiredOps !== $foundOps) {
                    $error = 'Implicit true comparisons prohibited; use === TRUE instead';
                    $phpcsFile->addError($error, $stackPtr, 'ImplicitTrue');
                    $foundOps++;
                }
            }
        }//end for

        $requiredOps++;

        if ($phpcsFile->tokenizerType !== 'JS'
            && $foundOps < $requiredOps
            && ($requiredOps !== $foundBools)
            && $tokens[($stackPtr + 1)]['code'] !== T_INLINE_ELSE
        ) {
            $error = 'Implicit true comparisons prohibited; use === TRUE instead';
            $phpcsFile->addError($error, $stackPtr, 'ImplicitTrue');
        }

    }
319765
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
	{
		// chech the first open tag.
		if ($stackPtr !== 0) {
			if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false)
				return;
		}

		$filePath    = $phpcsFile->getFilename();
		$fileContent = file_get_contents($filePath);
		$encoding    = mb_detect_encoding($fileContent, 'UTF-8, ASCII, ISO-8859-1');

		if ($encoding !== 'UTF-8') {
			$fileName = basename($filePath);
			
			if ($encoding) {
				$error = 'File must use only UTF-8 encoding. but %s found';
				$phpcsFile->addError($error, $stackPtr, 'FileIsNotUTF8Encoded', array($encoding));
			} else {
				$error = 'File must use only UTF-8 encoding.';
                                $phpcsFile->addError($error, 0);
			}
		}
	}
1340203
protected function checkImplements(File $phpcsFile, int $stackPtr): void
    {
        $implementsIndex = $phpcsFile->findNext([T_IMPLEMENTS], $stackPtr + 1);
        if (!$implementsIndex) {
            return;
        }

        $implements = $this->parseImplements($phpcsFile, $implementsIndex);
        foreach ($implements as $implement) {
            $this->fixStatement($phpcsFile, $implement, $stackPtr);
        }
    }
507635
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();
        $arrayStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr);
        $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr);

        $isSingleLine = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line'];

        foreach (DWS_Helpers_Array::commaPositions($phpcsFile, $arrayStart) as $comma) {
            if ($tokens[$comma - 1]['code'] === T_WHITESPACE) {
                $phpcsFile->addError('No whitespace allowed before commas in an array', $comma, 'SpaceBeforeComma');
            }

            if ($isSingleLine) {
                if ($tokens[$comma + 1]['content'] !== ' ') {
                    $phpcsFile->addError('Expected 1 space after comma in single-line array', $comma, 'SingleLineSpaceAfterComma');
                }
            } elseif ($tokens[$comma + 1]['content'][0] !== "\n") {
                $nextMember = $phpcsFile->findNext([T_WHITESPACE, T_COMMENT], $comma + 1, $arrayEnd, true);
                if ($nextMember !== false && $tokens[$nextMember]['line'] === $tokens[$comma]['line']) {
                    $phpcsFile->addError('Comma in multi-line array was not last token on line', $comma, 'MultiLineNewlineAfterComma');
                }
            }
        }
    }
282251
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
		$tokens = $phpcsFile->getTokens();

		if ( $tokens[( $stackPtr + 1 )]['code'] !== T_WHITESPACE
			&& ! ( $tokens[$stackPtr]['code'] === T_ELSE && $tokens[( $stackPtr + 1 )]['code'] === T_COLON )
			&& ! ( T_CLOSURE === $tokens[ $stackPtr ]['code'] && 0 === (int) $this->spaces_before_closure_open_paren )
		) {
			$error = 'Space after opening control structure is required';

			if ( isset( $phpcsFile->fixer ) === true ) {
				$fix = $phpcsFile->addFixableError( $error, $stackPtr, 'NoSpaceAfterStructureOpen' );

				if ( $fix === true ) {
					$phpcsFile->fixer->beginChangeset();
					$phpcsFile->fixer->addContent( $stackPtr, ' ' );
					$phpcsFile->fixer->endChangeset();
				}
			} else {
				$phpcsFile->addError( $error, $stackPtr, 'NoSpaceAfterStructureOpen' );
			}
		}

		if ( isset( $tokens[ $stackPtr ] ) && isset( $tokens[ $stackPtr ]['scope_closer'] ) === false ) {
			if ( T_USE === $tokens[ $stackPtr ]['code'] ) {
				$scopeOpener = $phpcsFile->findNext( T_OPEN_CURLY_BRACKET, $stackPtr + 1 );
				$scopeCloser = isset( $tokens[ $scopeOpener ]['scope_closer'] ) ? $tokens[ $scopeOpener ]['scope_closer'] : null;
			} else {
				return;
			}
		} else {
			$scopeOpener = isset( $tokens[ $stackPtr ]['scope_opener'] ) ? $tokens[ $stackPtr ]['scope_opener'] : null;
			$scopeCloser = isset( $tokens[ $stackPtr ]['scope_closer'] ) ? $tokens[ $stackPtr ]['scope_closer'] : null;
		}

		$firstContent = $phpcsFile->findNext( T_WHITESPACE, ( $scopeOpener + 1 ), null, true );
		$currentToken = $tokens[$scopeOpener];
		$class_line = array_values( array_filter( array_filter( $tokens, function ( $token ) use( $currentToken ) {
			return $token['line'] === $currentToken['line'];
		} ), function ( $token ) {
			return $token['code'] === T_CLASS;
		} ) );

		if ( count( $class_line ) > 0 ) {
			$next_line = array_filter( $tokens, function ( $token ) use( $class_line ) {
				return $token['line'] === $class_line[0]['line'] + 1;
			} );

			if ( count( $next_line ) > 0 ) {
				$whitespace = array_filter( $next_line, function ( $token ) {
					return $token['code'] === T_WHITESPACE;
				} );

				$whitespace = count( $whitespace ) < count( $next_line );

				if ( $whitespace
					&& $tokens[$scopeOpener]['code'] === T_OPEN_CURLY_BRACKET
					&& ! isset( $tokens[$firstContent]['nested_parenthesis'] )
					&& $tokens[$firstContent]['level'] === 1
				) {
					$error = 'No blank line found at start of control structure';

					if ( isset( $phpcsFile->fixer ) === true ) {
						$fix = $phpcsFile->addFixableError( $error, $scopeOpener, 'NoBlankLineAfterStart' );

						if ( $fix === true ) {
							$phpcsFile->fixer->beginChangeset();

							for ( $i = ( $scopeOpener + 2 ); $i < $firstContent; $i++ ) {
								$phpcsFile->fixer->replaceToken( $i, '' );
							}

							$phpcsFile->fixer->addNewline( $scopeOpener );
							$phpcsFile->fixer->endChangeset();
						}
					} else {
						$phpcsFile->addError( $error, $scopeOpener, 'NoBlankLineAfterStart' );
					}
				}

				$next_line = array_values( $next_line );

				$next_next_line = array_filter( $tokens, function ( $token ) use ( $next_line ) {
					return $token['line'] === $next_line[0]['line'] + 1;
				} );

				if ( count( $next_next_line )  > 0 ) {
					$next_whitespace = array_filter( $next_next_line, function ( $token ) {
						return $token['code'] === T_WHITESPACE;
					} );

					$next_whitespace = count( $next_next_line ) === count( $next_whitespace );

					if ( $next_whitespace
						&& $tokens[$scopeOpener]['code'] === T_OPEN_CURLY_BRACKET
						&& ! isset( $tokens[$firstContent]['nested_parenthesis'] )
						&& $tokens[$firstContent]['level'] === 1
					) {
						$error = 'More then one blank line found at start of control structure';

						if ( isset( $phpcsFile->fixer ) === true ) {
							$fix = $phpcsFile->addFixableError( $error, $scopeOpener, 'MoreThenOneBlankLineAfterStart' );

							if ( $fix === true ) {
								$phpcsFile->fixer->beginChangeset();

								for ( $i = ( $scopeOpener + 2 ); $i < $firstContent; $i++ ) {
									$phpcsFile->fixer->replaceToken( $i, '' );
								}

								$phpcsFile->fixer->addNewline( $scopeOpener );
								$phpcsFile->fixer->endChangeset();
							}
						} else {
							$phpcsFile->addError( $error, $scopeOpener, 'MoreThenOneBlankLineAfterStart' );
						}
					}
				}
			}
		}

		$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 ( $tokens[$trailingContent]['code'] === T_BREAK ) {
			// If this BREAK is closing a CASE, 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;
				}
			}
		}
	}