Checks if there are fluent setter errors and registers errors if needed.

@param File $phpcsFile The file for this sniff.
@param int $functionPos The position of the used token.
@param int $classPos The position of the class.

@return void
172684
private function checkForFluentSetterErrors(File $phpcsFile, int $functionPos, int $classPos): void
    {
        $tokens = $phpcsFile->getTokens();
        $errorData = $phpcsFile->getDeclarationName($classPos) . '::' . $phpcsFile->getDeclarationName($functionPos);

        $functionToken = $tokens[$functionPos];
        $openBracePtr = $functionToken['scope_opener'];
        $closeBracePtr = $functionToken['scope_closer'];

        $returnPtr = $phpcsFile->findNext(T_RETURN, $openBracePtr, $closeBracePtr);

        if ($returnPtr === false) {
            $fixNoReturnFound = $phpcsFile->addFixableError(
                self::ERROR_NO_RETURN_FOUND,
                $functionPos,
                static::CODE_NO_RETURN_FOUND,
                $errorData
            );

            if ($fixNoReturnFound) {
                $this->fixNoReturnFound($phpcsFile, $closeBracePtr);
            }

            return;
        }

        $this->checkAndRegisterEmptyReturnErrors($phpcsFile, $functionPos, $returnPtr, $errorData);
    }
885828
public function process(File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();
        $allowed = array(
            T_STRING,
            T_NS_SEPARATOR,
            T_VARIABLE,
            T_STATIC,
            T_SELF,
            T_DOUBLE_COLON,
            T_OBJECT_OPERATOR,
            T_OPEN_SQUARE_BRACKET,
            T_CLOSE_SQUARE_BRACKET,
        );

        $object = $stackPtr;
        $line   = $tokens[$object]['line'];

        while ($object && $tokens[$object]['line'] === $line) {
            $object = $phpcsFile->findNext($allowed, $object + 1);

            if ($tokens[$object]['line'] === $line
                && !in_array($tokens[$object + 1]['code'], $allowed)
            ) {
                if ($tokens[$object + 1]['code'] !== T_OPEN_PARENTHESIS) {
                    $phpcsFile->addError(
                        'Use parentheses when instantiating classes',
                        $stackPtr,
                        'Invalid'
                    );
                }

                break;
            }
        }
    }
163102
protected function checkConstantsInterface(File $phpCsFile, int $stackPointer): void
    {
        $docBlockEndIndex = $this->findDocBlock($phpCsFile, $stackPointer);

        if (!$docBlockEndIndex) {
            $this->addNewDocBlock($phpCsFile, $stackPointer);
            return;
        }

        $this->checkExistingDocBlock($phpCsFile, $docBlockEndIndex);
    }
149380
protected function addMethodAnnotation(File $phpCsFile, int $stackPointer): void
    {
        $phpCsFile->fixer->beginChangeset();

        $stackPointer = $this->getStackPointerOfClassBegin($phpCsFile, $stackPointer);

        if (!$this->hasDocBlock($phpCsFile, $stackPointer)) {
            $phpCsFile->fixer->addNewlineBefore($stackPointer);
            $phpCsFile->fixer->addContentBefore($stackPointer, ' */');
            $phpCsFile->fixer->addNewlineBefore($stackPointer);
            $phpCsFile->fixer->addContentBefore(
                $stackPointer,
                ' * @method ' . $this->getMethodAnnotationFileName($phpCsFile) . ' ' . $this->getMethodName() . '()'
            );
            $phpCsFile->fixer->addNewlineBefore($stackPointer);
            $phpCsFile->fixer->addContentBefore($stackPointer, '/**');
        } else {
            $position = $phpCsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPointer);
            $phpCsFile->fixer->addNewlineBefore($position);
            $phpCsFile->fixer->addContentBefore(
                $position,
                ' * @method ' . $this->getMethodAnnotationFileName($phpCsFile) . ' ' . $this->getMethodName() . '()'
            );
        }

        $phpCsFile->fixer->endChangeset();
    }
992844
private function handleLineNotEmpty(File $phpcsFile, int $whitespacePtr): void
    {
        $fixSpaceNotScndLine = $phpcsFile->addFixableError(
            self::ERROR_LINE_NOT_EMPTY,
            $whitespacePtr,
            static::CODE_LINE_NOT_EMPTY
        );

        if ($fixSpaceNotScndLine) {
            $phpcsFile->fixer->beginChangeset();
            $phpcsFile->fixer->replaceToken(
                $whitespacePtr,
                ''
            );
            $phpcsFile->fixer->endChangeset();
        }
    }
748636
public function process(File $phpcsFile, $stackPtr) {
		$tokens = $phpcsFile->getTokens();
		$utils = new \PHPCS_SecurityAudit\Sniffs\Utils();

		if ($tokens[$stackPtr]['content'] == 'error_reporting') {
			$p = $utils::get_param_tokens($phpcsFile, $stackPtr, 1);
			if (is_array($p) && count($p) == 1 && $p[0]['content'] === '0') {
				$error = 'Please do not disable error_reporting, it could be useful';
				$phpcsFile->addWarning($error, $stackPtr, 'ErrorReporting0');
			}
		}
	}
631817
public function process(File $phpcsFile, $stackPtr) {
		$utils = \PHPCS_SecurityAudit\Sniffs\UtilsFactory::getInstance();
		$tokens = $phpcsFile->getTokens();
        $closer = $phpcsFile->findNext(T_BACKTICK, $stackPtr + 1, null, false, null, true);
		if (!$closer) {
			return;
		}
        $s = $stackPtr + 1;
		$s = $phpcsFile->findNext(T_VARIABLE, $s, $closer);
        if ($s) {
			$msg = 'System execution with backticks detected with dynamic parameter';
			if ($utils::is_token_user_input($tokens[$s])) {
				$phpcsFile->addError($msg . ' directly from user input', $stackPtr, 'ErrSystemExec');
			} else {
				$phpcsFile->addWarning($msg, $stackPtr, 'WarnSystemExec');
			}
		}

	}
336468
public function process(File $phpcsFile, $stackPtr)
    {
        // Get all parameters from method signature.
        $paramNames = PHPCSHelper::getMethodParameters($phpcsFile, $stackPtr);
        if (empty($paramNames)) {
            return;
        }

        $supportsPHP4 = $this->supportsBelow('4.4');

        foreach ($paramNames as $param) {
            if ($param['type_hint'] === '') {
                continue;
            }

            // Strip off potential nullable indication.
            $typeHint = ltrim($param['type_hint'], '?');

            if ($supportsPHP4 === true) {
                $phpcsFile->addError(
                    'Type declarations were not present in PHP 4.4 or earlier.',
                    $param['token'],
                    'TypeHintFound'
                );

            } elseif (isset($this->newTypes[$typeHint])) {
                $itemInfo = array(
                    'name' => $typeHint,
                );
                $this->handleFeature($phpcsFile, $param['token'], $itemInfo);

                // As of PHP 7.0, using `self` or `parent` outside class scope throws a fatal error.
                // Only throw this error for PHP 5.2+ as before that the "type hint not supported" error
                // will be thrown.
                if (($typeHint === 'self' || $typeHint === 'parent')
                    && $this->inClassScope($phpcsFile, $stackPtr, false) === false
                    && $this->supportsAbove('5.2') !== false
                ) {
                    $phpcsFile->addError(
                        "'%s' type cannot be used outside of class scope",
                        $param['token'],
                        ucfirst($typeHint) . 'OutsideClassScopeFound',
                        array($typeHint)
                    );
                }
            } elseif (isset($this->invalidTypes[$typeHint])) {
                $error = "'%s' is not a valid type declaration. Did you mean %s ?";
                $data  = array(
                    $typeHint,
                    $this->invalidTypes[$typeHint],
                );

                $phpcsFile->addError($error, $param['token'], 'InvalidTypeHintFound', $data);
            }
        }
    }
1262604
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        if ($this->supportsAbove('5.3')) {
            $tokens = $phpcsFile->getTokens();

            // Find all the functions in this class or interface

            // Use the scope closer to limit the sniffing within the scope of
            // this class or interface
            $classScopeCloser = (isset($tokens[$stackPtr]['scope_closer']))
                ? $tokens[$stackPtr]['scope_closer']
                : $stackPtr + 1;

            $functionPtr = $stackPtr;

            while ($functionToken = $phpcsFile->findNext(T_FUNCTION, $functionPtr, $classScopeCloser)) {

                // Get the scope closer for this function in order to know how
                // to advance to the next function.
                // If no body of function (e.g. for interfaces), there is
                // no closing curly brace; advance the pointer differently
                $scopeCloser = isset($tokens[$functionToken]['scope_closer'])
                    ? $tokens[$functionToken]['scope_closer']
                    : $functionToken + 1;

                $scopeToken = false;
                $staticToken = false;

                $nameToken = $phpcsFile->findNext(T_WHITESPACE, $functionToken + 1, $classScopeCloser, true);
                if (in_array($tokens[$nameToken]['content'], $this->magicMethods) === false) {
                    $functionPtr = $scopeCloser;
                    continue;
                }

                // What is the token before the function token?
                $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $functionToken - 1, null, true);
                if (in_array($tokens[$prevToken]['code'], array(T_PUBLIC, T_PROTECTED, T_PRIVATE))) {
                    $scopeToken = $prevToken;
                } elseif ($tokens[$prevToken]['code'] == T_STATIC) {
                    $staticToken = $prevToken;
                } else {
                    // no scope or static modifiers, this function is okay
                    $functionPtr = $scopeCloser;
                    continue;
                }

                // What is the token before that one?
                $prevPrevToken = $phpcsFile->findPrevious(T_WHITESPACE, $prevToken - 1, null, true);
                if (in_array($tokens[$prevPrevToken]['code'], array(T_PUBLIC, T_PROTECTED, T_PRIVATE))) {
                    $scopeToken = $prevPrevToken;
                } elseif ($tokens[$prevPrevToken]['code'] == T_STATIC) {
                    $staticToken = $prevPrevToken;
                }

                $isPublic = ($scopeToken !== false && $tokens[$scopeToken]['type'] != 'T_PUBLIC') ? false : true;
                $isStatic = ($staticToken === false) ? false : true;

                if ($isPublic && !$isStatic) {
                    $functionPtr = $scopeCloser;
                    continue;
                }

                // Summarize what the problems are
                if (!$isPublic && $isStatic) {
                    $error = sprintf(
                        "Magic methods must be public and cannot be static (since PHP 5.3)! Found static %s function %s",
                        $tokens[$scopeToken]['content'],
                        $tokens[$nameToken]['content']
                    );
                    $phpcsFile->addError($error, $functionToken);
                } else {
                    if (!$isPublic) {
                        $error = sprintf(
                            "Magic methods must be public (since PHP 5.3)! Found %s function %s",
                            $tokens[$scopeToken]['content'],
                            $tokens[$nameToken]['content']
                        );
                        $phpcsFile->addError($error, $functionToken);
                    }

                    if ($isStatic) {
                        $error = sprintf(
                            "Magic methods cannot be static (since PHP 5.3)! Found static function %s",
                            $tokens[$nameToken]['content']
                        );
                        $phpcsFile->addError($error, $functionToken);
                    }
                }

                // Advance to next function
                $functionPtr = $scopeCloser;
            }
        }

    }
1312372
protected function assertNoApiTag(File $phpCsFile, int $stackPointer): void
    {
        $apiIndex = $this->findApiAnnotationIndex($phpCsFile, $stackPointer);
        if (!$apiIndex) {
            return;
        }

        $fix = $phpCsFile->addFixableError('@api annotation is invalid.', $stackPointer, 'ApiAnnotationInvalid');

        if (!$fix) {
            return;
        }

        $tokens = $phpCsFile->getTokens();
        $line = $tokens[$apiIndex]['line'];

        $phpCsFile->fixer->beginChangeset();
        $phpCsFile->fixer->replaceToken($apiIndex, '');

        $index = $apiIndex;
        while ($tokens[$index - 1]['line'] === $line) {
            $index--;
            $phpCsFile->fixer->replaceToken($index, '');
        }
        $index = $apiIndex;
        while ($tokens[$index + 1]['line'] === $line) {
            $index++;
            $phpCsFile->fixer->replaceToken($index, '');
        }

        $phpCsFile->fixer->endChangeset();
    }
903361
public function process(File $phpcsFile, $stackPointer)
    {
        $tokens       = $phpcsFile->getTokens();
        $currentToken = $tokens[$stackPointer];

        $closingParenthesisCandidate = $currentToken['parenthesis_closer'];
        $whitespaceCandidate         = $tokens[$closingParenthesisCandidate - 1];
        $linebreakCandidate          = $tokens[$closingParenthesisCandidate - 2];
        $commaCandidate              = $tokens[$closingParenthesisCandidate - 3];

        $hasClosingParenthesis = ($tokens[$closingParenthesisCandidate]['type'] === 'T_CLOSE_PARENTHESIS'
            && $whitespaceCandidate['type'] === 'T_WHITESPACE');

        $hasLinebreakBeforeClosingParenthesis = ($linebreakCandidate['type'] === 'T_WHITESPACE'
            && $linebreakCandidate['content'] === chr(10));

        $hasCommaAfterLastElement = $commaCandidate['type'] === 'T_COMMA';

        if ($hasClosingParenthesis && $hasLinebreakBeforeClosingParenthesis && !$hasCommaAfterLastElement)
        {
            $type  = 'Production.LastArrayElementComma.ArrayElementWithoutComma';
            $data  = $commaCandidate['content'];
            $error = 'Array element without comma: ' . $commaCandidate['line'];
            $phpcsFile->addWarning($error, $closingParenthesisCandidate - 3, $type, $data);
        }
    }
321806
public function process(File $phpcsFile, $stackPtr)
    {
        $tokens     = $phpcsFile->getTokens();
        $functionLc = strtolower($tokens[$stackPtr]['content']);
        if (isset($this->targetFunctions[$functionLc]) === false) {
            return;
        }

        // Next non-empty token should be the open parenthesis.
        $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
        if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) {
            return;
        }

        $ignore = array(
            \T_DOUBLE_COLON    => true,
            \T_OBJECT_OPERATOR => true,
            \T_FUNCTION        => true,
            \T_NEW             => true,
        );

        $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
        if (isset($ignore[$tokens[$prevNonEmpty]['code']]) === true) {
            // Not a call to a PHP function.
            return;
        } elseif ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING) {
            // Namespaced function.
            return;
        }

        $data = $tokens[$stackPtr]['content'];

        /*
         * Check for usage of the functions in the global scope.
         *
         * As PHPCS can not determine whether a file is included from within a function in
         * another file, so always throw a warning/error.
         */
        if ($phpcsFile->hasCondition($stackPtr, array(\T_FUNCTION, \T_CLOSURE)) === false) {
            $isError = false;
            $message = 'Use of %s() outside of a user-defined function is only supported if the file is included from within a user-defined function in another file prior to PHP 5.3.';

            if ($this->supportsAbove('5.3') === true) {
                $isError  = true;
                $message .= ' As of PHP 5.3, it is no longer supported at all.';
            }

            $this->addMessage($phpcsFile, $message, $stackPtr, $isError, 'OutsideFunctionScope', $data);
        }

        /*
         * Check for usage of the functions as a parameter in a function call.
         */
        if ($this->supportsBelow('5.2') === false) {
            return;
        }

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

        $throwError = false;

        $closer = end($tokens[$stackPtr]['nested_parenthesis']);
        if (isset($tokens[$closer]['parenthesis_owner'])
            && $tokens[$tokens[$closer]['parenthesis_owner']]['type'] === 'T_CLOSURE'
        ) {
            $throwError = true;
        } else {
            $opener       = key($tokens[$stackPtr]['nested_parenthesis']);
            $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);
            if ($tokens[$prevNonEmpty]['code'] !== \T_STRING) {
                return;
            }

            $prevPrevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true);
            if ($tokens[$prevPrevNonEmpty]['code'] === \T_FUNCTION) {
                return;
            }

            $throwError = true;
        }

        if ($throwError === false) {
            return;
        }

        $phpcsFile->addError(
            '%s() could not be used in parameter lists prior to PHP 5.3.',
            $stackPtr,
            'InParameterList',
            $data
        );
    }
16591
protected function assertNoSpacing(File $phpCsFile, array $first, array $second): void
    {
        $tokens = $phpCsFile->getTokens();

        $lastIndexOfFirst = $first['tagEnd'];
        $lastLineOfFirst = $tokens[$lastIndexOfFirst]['line'];

        $tagIndexOfSecond = $second['index'];
        $firstLineOfSecond = $tokens[$tagIndexOfSecond]['line'];

        if ($lastLineOfFirst === $firstLineOfSecond - 1) {
            return;
        }

        $fix = $phpCsFile->addFixableError('No newline expected between tags of the same type `' . $first['tag'] . '`', $tagIndexOfSecond, 'NoNewlineBetweenSameType');
        if (!$fix) {
            return;
        }

        $phpCsFile->fixer->beginChangeset();

        for ($i = $first['tagEnd'] + 1; $i < $second['start']; $i++) {
            if ($tokens[$i]['line'] <= $lastLineOfFirst || $tokens[$i]['line'] >= $firstLineOfSecond) {
                continue;
            }

            $phpCsFile->fixer->replaceToken($i, '');
        }

        $phpCsFile->fixer->endChangeset();
    }
173183
public function process(File $phpcsFile, $stackPointer)
    {
        $tokens = $phpcsFile->getTokens();
        // We are only interested in the first token in a multi-line string.
        if ($tokens[$stackPointer]['code'] === $tokens[$stackPointer - 1]['code'])
        {
            return;
        }

        $workingString     = $tokens[$stackPointer]['content'];
        $nextTokenPosition = $stackPointer + 1;
        while ($tokens[$nextTokenPosition]['code'] === $tokens[$stackPointer]['code'])
        {
            $workingString .= $tokens[$nextTokenPosition]['content'];
            $nextTokenPosition++;
        }

        // Check if it's a double quoted string.
        // Also make sure it's not a part of a string started in a previous line.
        // If it is, then we have already checked it.
        if (strpos($workingString, '"') === false || $workingString[0] !== '"')
        {
            return;
        }

        // The use of variables in double quoted strings is not allowed.
        if ($tokens[$stackPointer]['code'] === T_DOUBLE_QUOTED_STRING)
        {
            $this->handleDoubleQuotedString($phpcsFile, $stackPointer, $workingString);
        }

        $this->handleSpecialChars($phpcsFile, $stackPointer, $workingString);
    }
856652
public function process(File $phpcsFile, $stackPointer)
    {
        $tokens = $phpcsFile->getTokens();

        $nextParenthesisIndex     = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPointer);
        $parenthesisPositionStart = $tokens[$nextParenthesisIndex]['parenthesis_opener'];
        $parenthesisPositionEnd   = $tokens[$nextParenthesisIndex]['parenthesis_closer'];
        $equalOperatorIndex       = $phpcsFile->findNext(T_EQUAL, $parenthesisPositionStart, $parenthesisPositionEnd);
        if ($equalOperatorIndex === false)
        {
            return;
        }

        $braceBeforeParenthesis = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, $equalOperatorIndex, $nextParenthesisIndex);

        if ($braceBeforeParenthesis === $parenthesisPositionStart)
        {
            $type  = 'Production.AssignmentInControlStructure.AssignmentsInConditions';
            $data  = [$tokens[$stackPointer]['content']];
            $error = 'Please extract the assignment before the condition.';
            $phpcsFile->addWarning($error, $stackPointer, $type, $data);
        }
    }
728369
public function process(File $phpcsFile, $stackPtr)
    {
        $functionPtr = $phpcsFile->findPrevious(T_FUNCTION, ($stackPtr - 1));

        if ($functionPtr !== false) {
            $doubleColonPtr = $phpcsFile->findNext(T_DOUBLE_COLON, ($stackPtr + 1));

            if ($doubleColonPtr !== false) {
                $tokens        = $phpcsFile->getTokens();
                $functionName  = $phpcsFile->getDeclarationName($functionPtr);
                $methodNamePtr = $phpcsFile->findNext(T_STRING, ($stackPtr + 1));

                if ($methodNamePtr !== false && $tokens[$methodNamePtr]['content'] !== $functionName) {
                    $error = 'Method name mismatch in parent:: call';
                    $phpcsFile->addError($error, $stackPtr, 'WrongName');
                }
            }
        }//end if
    }
320807
public function process(File $phpcsFile, $stackPointer)
    {
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
        $tokens  = $phpcsFile->getTokens();
        $content = $tokens[$stackPointer]['content'];
        if ($tokens[$stackPointer]['type'] !== 'T_STRING' || stripos($content, 'in_array') === false)
        {
            return;
        }

        $end                 = $tokens[$stackPointer + 1]['parenthesis_closer'];
        $length              = $end - $stackPointer + 1;
        $inArrayContext      = $phpcsFile->getTokensAsString($stackPointer, $length);
        $inArrayContextMatch = [];
        preg_match('/in_array\\(\\s*.+,\\s*.+,\\s*true\\s*\\)/sim', $inArrayContext, $inArrayContextMatch);
        if (empty($inArrayContextMatch))
        {
            $type  = 'Production.StrictInArray.non-strictArray';
            $data  = [$inArrayContext];
            $error = 'in_array needs to have the third parameter set to true.';
            $phpcsFile->addWarning($error, $stackPointer, $type, $data);
        }
    }