Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files/FileName: various improvements #2285

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -75,23 +78,23 @@ final class FileNameUnitTest extends AbstractSniffUnitTest {
'test-sample-phpunit.inc' => 0,
'test-sample-phpunit6.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 @@ -130,6 +133,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 {}
Loading