getDeclarationName( $stackPtr ); if ( ! isset( $functionName ) ) { // Ignore closures. return; } if ( '' === ltrim( $functionName, '_' ) ) { // Ignore special functions, like __(). return; } $functionNameLc = strtolower( $functionName ); // Is this a magic function ? I.e., it is prefixed with "__" ? // Outside class scope this basically just means __autoload(). if ( 0 === strpos( $functionName, '__' ) ) { $magicPart = substr( $functionNameLc, 2 ); if ( isset( $this->magicFunctions[ $magicPart ] ) ) { return; } $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; $errorData = array( $functionName ); $phpcsFile->addError( $error, $stackPtr, 'FunctionDoubleUnderscore', $errorData ); } if ( $functionNameLc !== $functionName ) { $error = 'Function name "%s" is not in snake case format, try "%s"'; $errorData = array( $functionName, Sniff::get_snake_case_name_suggestion( $functionName ), ); $phpcsFile->addError( $error, $stackPtr, 'FunctionNameInvalid', $errorData ); } } /** * Processes the tokens within the scope. * * @param \PHP_CodeSniffer\Files\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. $conditions = $tokens[ $stackPtr ]['conditions']; end( $conditions ); $deepestScope = key( $conditions ); if ( $deepestScope !== $currScope ) { return; } if ( Sniff::is_function_deprecated( $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 ( ! isset( $methodName ) ) { // Ignore closures. return; } $className = $phpcsFile->getDeclarationName( $currScope ); if ( isset( $className ) === false ) { $className = '[Anonymous Class]'; } $methodNameLc = strtolower( $methodName ); $classNameLc = strtolower( $className ); // Ignore special functions. if ( '' === ltrim( $methodName, '_' ) ) { return; } // PHP4 constructors are allowed to break our rules. if ( $methodNameLc === $classNameLc ) { return; } // PHP4 destructors are allowed to break our rules. if ( '_' . $classNameLc === $methodNameLc ) { return; } $extended = $phpcsFile->findExtendedClassName( $currScope ); $interfaces = $phpcsFile->findImplementedInterfaceNames( $currScope ); // If this is a child class or interface implementation, it may have to use camelCase or double underscores. if ( ! empty( $extended ) || ! empty( $interfaces ) ) { return; } // Is this a magic method ? I.e. is it prefixed with "__" ? if ( 0 === strpos( $methodName, '__' ) ) { $magicPart = substr( $methodNameLc, 2 ); if ( isset( $this->magicMethods[ $magicPart ] ) ) { return; } $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; $errorData = array( $className . '::' . $methodName ); $phpcsFile->addError( $error, $stackPtr, 'MethodDoubleUnderscore', $errorData ); } // Check for all lowercase. if ( $methodNameLc !== $methodName ) { $error = 'Method name "%s" in class %s is not in snake case format, try "%s"'; $errorData = array( $methodName, $className, Sniff::get_snake_case_name_suggestion( $methodName ), ); $phpcsFile->addError( $error, $stackPtr, 'MethodNameInvalid', $errorData ); } } }