Skip to content

Commit 4f91688

Browse files
authored
Merge pull request #856 from Automattic/feature/classes-restrictedextendclasses-sniff-review
2 parents 05a0547 + cb553b0 commit 4f91688

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace WordPressVIPMinimum\Sniffs\Classes;
1111

12+
use PHPCSUtils\Utils\MessageHelper;
1213
use WordPressCS\WordPress\AbstractClassRestrictionsSniff;
1314

1415
/**
@@ -35,6 +36,20 @@ public function getGroups() {
3536
];
3637
}
3738

39+
/**
40+
* Returns an array of tokens this test wants to listen for.
41+
*
42+
* @return array<int|string>
43+
*/
44+
public function register() {
45+
$targets = parent::register();
46+
if ( empty( $targets ) ) {
47+
return $targets;
48+
}
49+
50+
return [ T_EXTENDS ];
51+
}
52+
3853
/**
3954
* Process a matched token.
4055
*
@@ -46,15 +61,9 @@ public function getGroups() {
4661
* @return void
4762
*/
4863
public function process_matched_token( $stackPtr, $group_name, $matched_content ) {
49-
$tokens = $this->phpcsFile->getTokens();
50-
51-
if ( $tokens[ $stackPtr ]['code'] !== T_EXTENDS ) {
52-
// If not extending, bail.
53-
return;
54-
}
55-
5664
foreach ( $this->getGroups() as $group => $group_args ) {
57-
$this->phpcsFile->{ 'add' . $group_args['type'] }( $group_args['message'], $stackPtr, $group );
65+
$isError = ( $group_args['type'] === 'error' );
66+
MessageHelper::addMessage( $this->phpcsFile, $group_args['message'], $stackPtr, $isError, $group );
5867
}
5968
}
6069
}

WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.inc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
class OkTestClass extends WPCOM_VIP_CLI_Command { } // Ok - correct class.
44
class AnotherOkTestClass extends WPC__CLI_Command { } // Ok - spacing + similar class.
5-
class Ok_TestClass extends wpcom_vip_cli_command { } // Ok - lowercase correct class.
6-
class Ok_Test_Class extends WpCoM_VIP_cli_command { } // Ok - mixed case correct class.
5+
class Ok_TestClass extends \wpcom_vip_cli_command { } // Ok - lowercase correct class.
6+
$ok = new class extends WpCoM_VIP_cli_command { }; // Ok - mixed case correct class.
77
class WP_CLI_Command { } // Ok - not extending.
88

99
class BadTestClass extends WP_CLI_Command { } // Warning - wrong class.
10-
class AnotherBadTestClass extends wp_CLI_comMand { } // Warning - mixed case wrong class.
11-
class AnotherBad_TestClass extends WP_CLI_comMand { } // Warning - spacing + mixed case wrong class.
10+
class AnotherBadTestClass extends \wp_CLI_comMand { } // Warning - mixed case wrong class.
11+
$bad = new class() extends WP_CLI_comMand { }; // Warning - spacing + mixed case wrong class.
1212
class Bad_Test_Class extends wp_cli_command { } // Warning - lowercase wrong class.
13+
14+
// phpcs:set WordPressVIPMinimum.Classes.RestrictedExtendClasses exclude[] wp_cli
15+
class IgnoredClass extends WP_CLI_Command { } // OK, group is excluded.
16+
17+
// Reset to the default value.
18+
// phpcs:set WordPressVIPMinimum.Classes.RestrictedExtendClasses exclude[]
19+
20+
class Ok_NotTheTargetFQN extends \Fully\Qualified\WP_CLI_Command { } // Ok - not the right namespace.
21+
class Ok_NotTheTargetPQN extends Partially\Qualified\WP_CLI_Command { } // Ok - not the right namespace.
22+
class Bad_NamespaceRelative extends namespace\WP_CLI_Command { } // Warning - this is not a namespaced file, so "namespace" resolves to the global namespace.

WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function getWarningList() {
3636
10 => 1,
3737
11 => 1,
3838
12 => 1,
39+
22 => 1,
3940
];
4041
}
4142
}

0 commit comments

Comments
 (0)