diff --git a/WordPress/Sniffs/Files/FileNameSniff.php b/WordPress/Sniffs/Files/FileNameSniff.php index e69472572c..4178599dcd 100644 --- a/WordPress/Sniffs/Files/FileNameSniff.php +++ b/WordPress/Sniffs/Files/FileNameSniff.php @@ -96,27 +96,39 @@ final class FileNameSniff extends Sniff { /** * Historical exceptions in WP core to the class name rule. * + * Note: these files were renamed to comply with the naming conventions in + * WP 6.1.0. + * This means we no longer need to make an exception for them in the + * `check_filename_has_class_prefix()` check, however, we do still need to + * make an exception in the `check_filename_is_hyphenated()` check. + * * @since 0.11.0 + * @since 3.0.0 Property has been renamed from `$class_exceptions` to `$hyphenation_exceptions`, * * @var array */ - private $class_exceptions = array( + private $hyphenation_exceptions = array( 'class.wp-dependencies.php' => true, 'class.wp-scripts.php' => true, 'class.wp-styles.php' => true, + 'functions.wp-scripts.php' => true, + 'functions.wp-styles.php' => true, ); /** * Unit test version of the historical exceptions in WP core. * * @since 0.11.0 + * @since 3.0.0 Property has been renamed from `$unittest_class_exceptions` to `$unittest_hyphenation_exceptions`, * * @var array */ - private $unittest_class_exceptions = array( + private $unittest_hyphenation_exceptions = array( 'class.wp-dependencies.inc' => true, 'class.wp-scripts.inc' => true, 'class.wp-styles.inc' => true, + 'functions.wp-scripts.inc' => true, + 'functions.wp-styles.inc' => true, ); /** @@ -126,7 +138,7 @@ final class FileNameSniff extends Sniff { */ public function register() { if ( \defined( '\PHP_CODESNIFFER_IN_TESTS' ) ) { - $this->class_exceptions += $this->unittest_class_exceptions; + $this->hyphenation_exceptions += $this->unittest_hyphenation_exceptions; } return Collections::phpOpenTags(); @@ -207,8 +219,13 @@ public function process_token( $stackPtr ) { * @return void */ protected function check_filename_is_hyphenated( $file_name ) { - $expected = strtolower( str_replace( '_', '-', $file_name ) ); - if ( $file_name === $expected ) { + $extension = strrchr( $file_name, '.' ); + $name = substr( $file_name, 0, ( strlen( $file_name ) - strlen( $extension ) ) ); + + $expected = strtolower( preg_replace( '`[[:punct:]]`', '-', $name ) ) . $extension; + if ( $file_name === $expected + || isset( $this->hyphenation_exceptions[ $file_name ] ) + ) { return; } @@ -237,12 +254,11 @@ protected function check_filename_is_hyphenated( $file_name ) { * @return void */ protected function check_filename_has_class_prefix( $class_ptr, $file_name ) { + $extension = strrchr( $file_name, '.' ); $class_name = ObjectDeclarations::getName( $this->phpcsFile, $class_ptr ); - $expected = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ); + $expected = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . $extension; - if ( substr( $file_name, 0, -4 ) === $expected - || isset( $this->class_exceptions[ $file_name ] ) - ) { + if ( $file_name === $expected ) { return; } @@ -251,7 +267,7 @@ protected function check_filename_has_class_prefix( $class_ptr, $file_name ) { 0, 'InvalidClassFileName', array( - $expected . '.php', + $expected, $file_name, ) ); diff --git a/WordPress/Tests/Files/FileNameUnitTest.php b/WordPress/Tests/Files/FileNameUnitTest.php index aa80029cc6..8c327f96d8 100644 --- a/WordPress/Tests/Files/FileNameUnitTest.php +++ b/WordPress/Tests/Files/FileNameUnitTest.php @@ -40,6 +40,9 @@ final class FileNameUnitTest extends AbstractSniffUnitTest { 'SomeFile.inc' => 1, 'some-File.inc' => 1, 'SomeView.inc' => 1, + 'class.with.dot.not.underscore.inc' => 2, + 'class@with#other+punctuation.inc' => 2, + 'class-wrong-with-different-extension.php3' => 1, // Class file names. 'my-class.inc' => 1, @@ -76,23 +79,23 @@ final class FileNameUnitTest extends AbstractSniffUnitTest { 'test-sample-phpunit6.inc' => 0, 'test-sample-phpunit6-case-insensitive.inc' => 0, 'test-sample-wpunit.inc' => 0, - 'test-sample-custom-unit.1.inc' => 0, - 'test-sample-custom-unit.2.inc' => 0, - 'test-sample-custom-unit.3.inc' => 0, - 'test-sample-custom-unit.4.inc' => 0, - 'test-sample-custom-unit.5.inc' => 1, // Namespaced vs non-namespaced. - 'test-sample-namespaced-declaration.1.inc' => 0, - 'test-sample-namespaced-declaration.2.inc' => 1, // Namespaced vs non-namespaced. - 'test-sample-namespaced-declaration.3.inc' => 1, // Wrong namespace. - 'test-sample-namespaced-declaration.4.inc' => 1, // Non-namespaced vs namespaced. - 'test-sample-global-namespace-extends.1.inc' => 0, // Prefixed user input. - 'test-sample-global-namespace-extends.2.inc' => 1, // Non-namespaced vs namespaced. + 'test-sample-custom-unit-1.inc' => 0, + 'test-sample-custom-unit-2.inc' => 0, + 'test-sample-custom-unit-3.inc' => 0, + 'test-sample-custom-unit-4.inc' => 0, + 'test-sample-custom-unit-5.inc' => 1, // Namespaced vs non-namespaced. + 'test-sample-namespaced-declaration-1.inc' => 0, + 'test-sample-namespaced-declaration-2.inc' => 1, // Namespaced vs non-namespaced. + 'test-sample-namespaced-declaration-3.inc' => 1, // Wrong namespace. + 'test-sample-namespaced-declaration-4.inc' => 1, // Non-namespaced vs namespaced. + 'test-sample-global-namespace-extends-1.inc' => 0, // Prefixed user input. + 'test-sample-global-namespace-extends-2.inc' => 1, // Non-namespaced vs namespaced. 'test-sample-extends-with-use.inc' => 0, - 'test-sample-namespaced-extends.1.inc' => 0, - 'test-sample-namespaced-extends.2.inc' => 1, // Wrong namespace. - 'test-sample-namespaced-extends.3.inc' => 1, // Namespaced vs non-namespaced. - 'test-sample-namespaced-extends.4.inc' => 1, // Non-namespaced vs namespaced. - 'test-sample-namespaced-extends.5.inc' => 0, + 'test-sample-namespaced-extends-1.inc' => 0, + 'test-sample-namespaced-extends-2.inc' => 1, // Wrong namespace. + 'test-sample-namespaced-extends-3.inc' => 1, // Namespaced vs non-namespaced. + 'test-sample-namespaced-extends-4.inc' => 1, // Non-namespaced vs namespaced. + 'test-sample-namespaced-extends-5.inc' => 0, 'Test_Sample.inc' => 0, /* @@ -131,6 +134,11 @@ protected function getTestFiles( $testFileBase ) { $sep = \DIRECTORY_SEPARATOR; $test_files = glob( dirname( $testFileBase ) . $sep . 'FileNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', \GLOB_BRACE ); + $php3_test_files = glob( dirname( $testFileBase ) . $sep . 'FileNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.php3', \GLOB_BRACE ); + foreach ( $php3_test_files as $file ) { + $test_files[] = $file; + } + if ( ! empty( $test_files ) ) { return $test_files; } diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.1.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-1.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.1.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-1.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.2.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-2.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.2.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-2.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.3.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-3.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.3.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-3.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.4.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-4.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.4.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-4.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.5.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-5.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit.5.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-custom-unit-5.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends.1.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends-1.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends.1.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends-1.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends.2.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends-2.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends.2.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-global-namespace-extends-2.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.1.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-1.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.1.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-1.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.2.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-2.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.2.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-2.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.3.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-3.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.3.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-3.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.4.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-4.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration.4.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-declaration-4.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.1.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-1.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.1.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-1.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.2.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-2.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.2.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-2.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.3.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-3.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.3.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-3.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.4.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-4.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.4.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-4.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.5.inc b/WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-5.inc similarity index 100% rename from WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends.5.inc rename to WordPress/Tests/Files/FileNameUnitTests/TestFiles/test-sample-namespaced-extends-5.inc diff --git a/WordPress/Tests/Files/FileNameUnitTests/class-file-with-different-extension.php3 b/WordPress/Tests/Files/FileNameUnitTests/class-file-with-different-extension.php3 new file mode 100644 index 0000000000..ad920b4cab --- /dev/null +++ b/WordPress/Tests/Files/FileNameUnitTests/class-file-with-different-extension.php3 @@ -0,0 +1,3 @@ +