magicMethods['debuginfo'] = true; } /** * Processes the tokens within the scope. * * @since 8.2.0 * * @param \PHP_CodeSniffer_File $phpcsFile The file being processed. * @param int $stackPtr The position where this token was * found. * @param int $currScope The position of the current scope. * * @return void */ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) { $tokens = $phpcsFile->getTokens(); /* * Determine if this is a function which needs to be examined. * The `processTokenWithinScope()` is called for each valid scope a method is in, * so for nested classes, we need to make sure we only examine the token for * the lowest level valid scope. */ $conditions = $tokens[$stackPtr]['conditions']; end($conditions); $deepestScope = key($conditions); if ($deepestScope !== $currScope) { return; } if ($this->isFunctionDeprecated($phpcsFile, $stackPtr) === true) { /* * Deprecated functions don't have to comply with the naming conventions, * otherwise functions deprecated in favour of a function with a compliant * name would still trigger an error. */ return; } $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { // Ignore closures. return; } // Is this a magic method. i.e., is prefixed with "__" ? if (preg_match('|^__[^_]|', $methodName) > 0) { $magicPart = strtolower(substr($methodName, 2)); if (isset($this->magicMethods[$magicPart]) === false && isset($this->methodsDoubleUnderscore[$magicPart]) === false ) { $className = '[anonymous class]'; $scopeNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($currScope + 1), null, true); if ($scopeNextNonEmpty !== false && $tokens[$scopeNextNonEmpty]['code'] === \T_STRING) { $className = $tokens[$scopeNextNonEmpty]['content']; } $phpcsFile->addWarning( 'Method name "%s" is discouraged; PHP has reserved all method names with a double underscore prefix for future use.', $stackPtr, 'MethodDoubleUnderscore', array($className . '::' . $methodName) ); } } } /** * Processes the tokens outside the scope. * * @since 8.2.0 * * @param \PHP_CodeSniffer_File $phpcsFile The file being processed. * @param int $stackPtr The position where this token was * found. * * @return void */ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { if ($this->isFunctionDeprecated($phpcsFile, $stackPtr) === true) { /* * Deprecated functions don't have to comply with the naming conventions, * otherwise functions deprecated in favour of a function with a compliant * name would still trigger an error. */ return; } $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { // Ignore closures. return; } // Is this a magic function. i.e., it is prefixed with "__". if (preg_match('|^__[^_]|', $functionName) > 0) { $magicPart = strtolower(substr($functionName, 2)); if (isset($this->magicFunctions[$magicPart]) === false) { $phpcsFile->addWarning( 'Function name "%s" is discouraged; PHP has reserved all method names with a double underscore prefix for future use.', $stackPtr, 'FunctionDoubleUnderscore', array($functionName) ); } } } /** * Check whether a function has been marked as deprecated via a @deprecated tag * in the function docblock. * * @since 9.3.2 * * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. * @param int $stackPtr The position of a T_FUNCTION * token in the stack. * * @return bool */ private function isFunctionDeprecated(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $find = Tokens::$methodPrefixes; $find[] = \T_WHITESPACE; $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); if ($tokens[$commentEnd]['code'] !== \T_DOC_COMMENT_CLOSE_TAG) { // Function doesn't have a doc comment or is using the wrong type of comment. return false; } $commentStart = $tokens[$commentEnd]['comment_opener']; foreach ($tokens[$commentStart]['comment_tags'] as $tag) { if ($tokens[$tag]['content'] === '@deprecated') { return true; } } return false; } }