-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathRestrictedExtendClassesSniff.php
58 lines (51 loc) · 1.61 KB
/
RestrictedExtendClassesSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
*/
namespace WordPressVIPMinimum\Sniffs\Classes;
use WordPressCS\WordPress\AbstractClassRestrictionsSniff;
/**
* WordPressVIPMinimum_Sniffs_Classes_RestrictedExtendClassesSniff.
*
* @since 0.4.0
*/
class RestrictedExtendClassesSniff extends AbstractClassRestrictionsSniff {
/**
* Groups of classes to restrict.
*
* @return array<string, array<string, string|array<string>>>
*/
public function getGroups() {
return [
'wp_cli' => [
'type' => 'warning',
'message' => 'We recommend extending `WPCOM_VIP_CLI_Command` instead of `WP_CLI_Command` and using the helper functions available in it (such as `stop_the_insanity()`), see https://vip.wordpress.com/documentation/writing-bin-scripts/ for more information.',
'classes' => [
'WP_CLI_Command',
],
],
];
}
/**
* Process a matched token.
*
* @param int $stackPtr The position of the current token in the stack.
* @param string $group_name The name of the group which was matched.
* @param string $matched_content The token content (class name) which was matched
* in lowercase.
*
* @return void
*/
public function process_matched_token( $stackPtr, $group_name, $matched_content ) {
$tokens = $this->phpcsFile->getTokens();
if ( $tokens[ $stackPtr ]['code'] !== T_EXTENDS ) {
// If not extending, bail.
return;
}
foreach ( $this->getGroups() as $group => $group_args ) {
$this->phpcsFile->{ 'add' . $group_args['type'] }( $group_args['message'], $stackPtr, $group );
}
}
}