true, \T_OBJECT_OPERATOR => true, \T_FUNCTION => true, \T_NEW => true, \T_CONST => true, \T_USE => true, ); /** * Returns an array of tokens this test wants to listen for. * * @since 8.2.0 * * @return array */ public function register() { // Handle case-insensitivity of function names. $this->targetFunctions = $this->arrayKeysToLowercase($this->targetFunctions); return array(\T_STRING); } /** * Processes this test, when one of its tokens is encountered. * * @since 8.2.0 * * @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 int|void Integer stack pointer to skip forward or void to continue * normal file processing. */ public function process(File $phpcsFile, $stackPtr) { if ($this->bowOutEarly() === true) { return; } $tokens = $phpcsFile->getTokens(); $function = $tokens[$stackPtr]['content']; $functionLc = strtolower($function); if (isset($this->targetFunctions[$functionLc]) === false) { return; } $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); if ($this->isMethod === true) { if ($tokens[$prevNonEmpty]['code'] !== \T_DOUBLE_COLON && $tokens[$prevNonEmpty]['code'] !== \T_OBJECT_OPERATOR ) { // Not a call to a PHP method. return; } } else { if (isset($this->ignoreTokens[$tokens[$prevNonEmpty]['code']]) === true) { // Not a call to a PHP function. return; } if ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING ) { // Namespaced function. return; } } $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); if (empty($parameters)) { return $this->processNoParameters($phpcsFile, $stackPtr, $function); } else { return $this->processParameters($phpcsFile, $stackPtr, $function, $parameters); } } /** * Do a version check to determine if this sniff needs to run at all. * * @since 8.2.0 * * If the check done in a child class is not specific to one PHP version, * this function should return `false`. * * @return bool */ abstract protected function bowOutEarly(); /** * Process the parameters of a matched function. * * This method has to be made concrete in child classes. * * @since 8.2.0 * * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the stack. * @param string $functionName The token content (function name) which was matched. * @param array $parameters Array with information about the parameters. * * @return int|void Integer stack pointer to skip forward or void to continue * normal file processing. */ abstract public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters); /** * Process the function if no parameters were found. * * Defaults to doing nothing. Can be overloaded in child classes to handle functions * were parameters are expected, but none found. * * @since 8.2.0 * * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the stack. * @param string $functionName The token content (function name) which was matched. * * @return int|void Integer stack pointer to skip forward or void to continue * normal file processing. */ public function processNoParameters(File $phpcsFile, $stackPtr, $functionName) { return; } }