diff --git a/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php deleted file mode 100644 index 2d6132e49d21f..0000000000000 --- a/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ /dev/null @@ -1,217 +0,0 @@ -onRegisterEvent(); - - return array( T_FUNCTION, T_CLASS ); - } - - /** - * Processes function and class tokens. - * - * @param File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token - * in the stack passed in $tokens. - * - * @return void - */ - public function process( File $phpcsFile, $stackPtr ) { - $tokens = $phpcsFile->getTokens(); - $token = $tokens[ $stackPtr ]; - - if ( 'T_FUNCTION' === $token['type'] ) { - $this->processFunctionToken( $phpcsFile, $stackPtr ); - return; - } - - if ( 'T_CLASS' === $token['type'] ) { - $this->processClassToken( $phpcsFile, $stackPtr ); - } - } - - /** - * Functions should be wrapped with !function_exists() to avoid fatal errors. - * E.g.: - * if ( ! function_exists( 'wp_get_navigation' ) ) { - * function wp_get_navigation( $slug ) { ... } - * } - * - * @param File $phpcsFile The file being scanned. - * @param int $stackPointer The position of the current token - * in the stack passed in $tokens. - * - * @return void - */ - private function processFunctionToken( File $phpcsFile, $stackPointer ) { - $tokens = $phpcsFile->getTokens(); - $functionToken = $phpcsFile->findNext( T_STRING, $stackPointer ); - - $wrappingTokensToCheck = array( - T_CLASS, - T_INTERFACE, - T_TRAIT, - ); - - foreach ( $wrappingTokensToCheck as $wrappingTokenToCheck ) { - if ( false !== $phpcsFile->getCondition( $functionToken, $wrappingTokenToCheck, false ) ) { - // This sniff only processes functions, not class methods. - return; - } - } - - $functionName = $tokens[ $functionToken ]['content']; - foreach ( $this->whitelisted_functions as $functionRegExp ) { - if ( preg_match( $functionRegExp, $functionName ) ) { - // Ignore whitelisted function names. - return; - } - } - - foreach ( $this->blacklisted_functions as $functionRegExp ) { - if ( preg_match( $functionRegExp, $functionName ) ) { - $errorMessage = $this->getFunctionNameErrorMessage( $functionName, $functionRegExp ); - $phpcsFile->addError( $errorMessage, $functionToken, 'FunctionNameViolatesNamingConvention' ); - return; - } - } - } - - /** - * Classes should be wrapped with !function_exists() to avoid fatal errors. - * E.g.: - * if ( class_exists( 'WP_Navigation' ) ) { - * return; - * } - * - * Alternatively: - * - * if ( ! class_exists( 'WP_Navigation' ) ) { - * class WP_Navigation { ... } - * } - * - * @param File $phpcsFile The file being scanned. - * @param int $stackPointer The position of the current token - * in the stack passed in $tokens. - * - * @return void - */ - private function processClassToken( File $phpcsFile, $stackPointer ) { - $tokens = $phpcsFile->getTokens(); - $classToken = $phpcsFile->findNext( T_STRING, $stackPointer ); - $className = $tokens[ $classToken ]['content']; - - foreach ( $this->whitelisted_classes as $classnameRegExp ) { - if ( preg_match( $classnameRegExp, $className ) ) { - // Ignore whitelisted class names. - return; - } - } - - foreach ( $this->blacklisted_classes as $classnameRegExp ) { - if ( preg_match( $classnameRegExp, $className ) ) { - $errorMessage = $this->getClassNameErrorMessage( $className, $classnameRegExp ); - $phpcsFile->addError( $errorMessage, $classnameRegExp, 'ClassNameViolatesNamingConvention' ); - return; - } - } - } - - private function getFunctionNameErrorMessage( $functionName, $regularExpression) { - return sprintf( - 'The function name "%s()" violates the naming convention defined by the regular expression: "{regular_expression}".', - $functionName, - $regularExpression - ); - } - - private function getClassNameErrorMessage( $className, $regularExpression) { - return sprintf( - 'The class name "%s" violates the naming convention defined by the regular expression: "{regular_expression}".', - $className, - $regularExpression - ); - } - - /** - * The purpose of this method is to sanitize the input data - * after the properties have been set. - */ - private function onRegisterEvent() { - $this->whitelisted_functions = self::sanitize( $this->whitelisted_functions ); - $this->whitelisted_classes = self::sanitize( $this->whitelisted_classes ); - } - - /** - * Input data needs to be sanitized. - * - * @param array $values The values being sanitized. - * - * @return array - */ - private static function sanitize( $values ) { - $values = array_map( 'trim', $values ); - - return array_filter( $values ); - } -} diff --git a/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/NamingConventions/ValidBlockLibraryFunctionName.php b/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/NamingConventions/ValidBlockLibraryFunctionName.php new file mode 100644 index 0000000000000..789ca220d1820 --- /dev/null +++ b/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/NamingConventions/ValidBlockLibraryFunctionName.php @@ -0,0 +1,128 @@ +onRegisterEvent(); + + return array( T_FUNCTION ); + } + + /** + * Processes function and class tokens. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + $token = $tokens[ $stackPtr ]; + + if ( 'T_FUNCTION' !== $token['type'] ) { + return; + } + + $this->processFunctionToken( $phpcsFile, $stackPtr ); + } + + /** + * Functions should be wrapped with !function_exists() to avoid fatal errors. + * E.g.: + * if ( ! function_exists( 'wp_get_navigation' ) ) { + * function wp_get_navigation( $slug ) { ... } + * } + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPointer The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + private function processFunctionToken( File $phpcsFile, $stackPointer ) { + $tokens = $phpcsFile->getTokens(); + $functionToken = $phpcsFile->findNext( T_STRING, $stackPointer ); + + $wrappingTokensToCheck = array( + T_CLASS, + T_INTERFACE, + T_TRAIT, + ); + + foreach ( $wrappingTokensToCheck as $wrappingTokenToCheck ) { + if ( false !== $phpcsFile->getCondition( $functionToken, $wrappingTokenToCheck, false ) ) { + // This sniff only processes functions, not class methods. + return; + } + } + + $functionName = $tokens[ $functionToken ]['content']; + + foreach ( $this->whitelistedFunctions as $functionRegExp ) { + if ( preg_match( $functionRegExp, $functionName ) ) { + // Ignore whitelisted function names. + return; + } + } + } + + /** + * The purpose of this method is to sanitize the input data + * after the properties have been set. + */ + private function onRegisterEvent() { + $this->prefixes = self::sanitize( $this->prefixes ); + } + + /** + * Input data needs to be sanitized. + * + * @param array $values The values being sanitized. + * + * @return array + */ + private static function sanitize( $values ) { + $values = array_map( 'trim', $values ); + + return array_filter( $values ); + } +}