diff --git a/BigBite/Docs/Files/FileNameStandard.xml b/BigBite/Docs/Files/FileNameStandard.xml index e75d80a..1e0ab61 100644 --- a/BigBite/Docs/Files/FileNameStandard.xml +++ b/BigBite/Docs/Files/FileNameStandard.xml @@ -1,11 +1,12 @@ trait-my-trait.php - interface My_Interface {} => interface-my-interface.php - class My_Class {} => class-my-class.php - abstract class My_Abstract_Class {} => abstract-class-my-abstract-class.php + - enum My_Enum {} => enum-my-enum.php ]]> diff --git a/BigBite/Sniffs/Files/FileNameSniff.php b/BigBite/Sniffs/Files/FileNameSniff.php index 9236e97..ad0ad44 100644 --- a/BigBite/Sniffs/Files/FileNameSniff.php +++ b/BigBite/Sniffs/Files/FileNameSniff.php @@ -158,6 +158,12 @@ public function process_token( $stack_ptr ) { $trait_ptr = $this->phpcsFile->findNext( \T_TRAIT, $stack_ptr ); $interface_ptr = $this->phpcsFile->findNext( \T_INTERFACE, $stack_ptr ); + $enum_ptr = false; + // Since PHP 8.1. + if ( defined( '\T_ENUM' ) ) { + $enum_ptr = $this->phpcsFile->findNext( \T_ENUM, $stack_ptr ); + } + if ( false !== $class_ptr && $this->is_test_class( $this->phpcsFile, $class_ptr ) ) { /* * This rule should not be applied to test classes (at all). @@ -173,7 +179,7 @@ public function process_token( $stack_ptr ) { $file_name = basename( $file ); // plain file, just check hyphenation. - if ( ! $class_ptr && ! $trait_ptr && ! $interface_ptr ) { + if ( ! $class_ptr && ! $trait_ptr && ! $interface_ptr && ! $enum_ptr ) { $this->check_filename_is_hyphenated( $file_name ); return ( $this->phpcsFile->numTokens + 1 ); } @@ -202,10 +208,10 @@ public function process_token( $stack_ptr ) { return ( $this->phpcsFile->numTokens + 1 ); } - $is_wpinc_path = false !== strpos( $file, \DIRECTORY_SEPARATOR . 'wp-includes' . \DIRECTORY_SEPARATOR ); - - if ( $is_wpinc_path && false === $class_ptr ) { - $this->check_filename_for_template_suffix( $stack_ptr, $file_name ); + // check for "enum-" prefix. + if ( false !== $enum_ptr ) { + $this->check_filename_has_enum_prefix( $enum_ptr, $file_name ); + return ( $this->phpcsFile->numTokens + 1 ); } // Only run this sniff once per file, no need to run it again. @@ -365,43 +371,27 @@ protected function check_filename_has_interface_prefix( $interface_ptr, $file_na } /** - * Check non-class files in "wp-includes" with a "@subpackage Template" tag for a "-template" suffix. - * - * @since 3.0.0 + * Check files containing an enum for the "enum-" prefix, + * and that the rest of the file name reflects the enum name. * - * @param int $stackPtr Stack pointer to the first PHP open tag in the file. - * @param string $file_name The name of the current file. + * @param mixed $enum_ptr the token stack. + * @param string $file_name the name of the file. * - * @return void + * @return bool */ - protected function check_filename_for_template_suffix( $stackPtr, $file_name ) { - $subpackage_tag = $this->phpcsFile->findNext( \T_DOC_COMMENT_TAG, $stackPtr, null, false, '@subpackage' ); - if ( false === $subpackage_tag ) { - return; - } + protected function check_filename_has_enum_prefix( $enum_ptr, $file_name ) { + $extension = strrchr( $file_name, '.' ); + $enum_name = ObjectDeclarations::getName( $this->phpcsFile, $enum_ptr ); + $expected = 'enum-' . $this->kebab( $enum_name ) . $extension; + $err_message = 'Enum file names should be based on the enum name with "enum-" prepended. Expected %s, but found %s.'; - $subpackage = $this->phpcsFile->findNext( \T_DOC_COMMENT_STRING, $subpackage_tag ); - if ( false === $subpackage ) { - return; + if ( $file_name === $expected ) { + return true; } - $fileName_end = substr( $file_name, -13 ); - - if ( ( 'Template' === trim( $this->tokens[ $subpackage ]['content'] ) - && $this->tokens[ $subpackage_tag ]['line'] === $this->tokens[ $subpackage ]['line'] ) - && ( ( ! \defined( '\PHP_CODESNIFFER_IN_TESTS' ) && '-template.php' !== $fileName_end ) - || ( \defined( '\PHP_CODESNIFFER_IN_TESTS' ) && '-template.inc' !== $fileName_end ) ) - ) { - $this->phpcsFile->addError( - 'Files containing template tags should have "-template" appended to the end of the file name. Expected %s, but found %s.', - 0, - 'InvalidTemplateTagFileName', - array( - substr( $file_name, 0, -4 ) . '-template.php', - $file_name, - ) - ); - } + $this->phpcsFile->addError( $err_message, 0, 'InvalidEnumFileName', array( $expected, $file_name ) ); + + return false; } /** diff --git a/BigBite/Tests/Files/FileNameUnitTest.php b/BigBite/Tests/Files/FileNameUnitTest.php index bee7483..af1aece 100644 --- a/BigBite/Tests/Files/FileNameUnitTest.php +++ b/BigBite/Tests/Files/FileNameUnitTest.php @@ -48,6 +48,8 @@ final class FileNameUnitTest extends AbstractSniffUnitTest { 'ClassMyClass.inc' => 1, 'InterfaceMyInterface.inc' => 1, 'TraitMyTrait.inc' => 1, + 'enum-different-enum.inc' => 1, + 'EnumMyEnum.inc' => 1, // Theme specific exceptions in a non-theme context. 'single-my_post_type.inc' => 1, @@ -62,6 +64,7 @@ final class FileNameUnitTest extends AbstractSniffUnitTest { 'ClassNonStrictClass.inc' => 1, 'InterfaceNonStrictClass.inc' => 1, 'TraitNonStrictClass.inc' => 1, + 'EnumNonStrictEnum.inc' => 1, /* * In /FileNameUnitTests/PHPCSAnnotations. diff --git a/BigBite/Tests/Files/FileNameUnitTests/EnumMyEnum.inc b/BigBite/Tests/Files/FileNameUnitTests/EnumMyEnum.inc new file mode 100644 index 0000000..0472eb7 --- /dev/null +++ b/BigBite/Tests/Files/FileNameUnitTests/EnumMyEnum.inc @@ -0,0 +1,3 @@ + +phpcs:set BigBite.Files.FileName strict_file_names false + + +phpcs:set BigBite.Files.FileName strict_file_names false + +