Skip to content

Commit

Permalink
Merge pull request #2285 from WordPress/feature/filename-dont-assume-…
Browse files Browse the repository at this point in the history
…file-extension-and-deal-with-other-sep
  • Loading branch information
GaryJones authored Jul 4, 2023
2 parents 5853837 + 39183db commit a1c5aa4
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 29 deletions.
36 changes: 26 additions & 10 deletions WordPress/Sniffs/Files/FileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -251,7 +267,7 @@ protected function check_filename_has_class_prefix( $class_ptr, $file_name ) {
0,
'InvalidClassFileName',
array(
$expected . '.php',
$expected,
$file_name,
)
);
Expand Down
40 changes: 24 additions & 16 deletions WordPress/Tests/Files/FileNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,

/*
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class File_With_Different_Extension {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class File_With_Different_Extension {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class With_Dot_Not_Underscore {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

class WP_Dependencies {}
// WP Core file no longer contains a class.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

class WP_Scripts {}
// WP Core file no longer contains a class.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

class WP_Styles {}
// WP Core file no longer contains a class.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class With_Other_Punctuation {}

0 comments on commit a1c5aa4

Please sign in to comment.