-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDeclarationCompatibilitySniff.php
More file actions
362 lines (330 loc) · 10.2 KB
/
DeclarationCompatibilitySniff.php
File metadata and controls
362 lines (330 loc) · 10.2 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
* @link https://github.com/Automattic/VIP-Coding-Standards
* @license https://opensource.org/license/gpl-2-0 GPL-2.0
*/
namespace WordPressVIPMinimum\Sniffs\Classes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHPCSUtils\Utils\FunctionDeclarations;
use PHPCSUtils\Utils\ObjectDeclarations;
/**
* Class WordPressVIPMinimum_Sniffs_Classes_DeclarationCompatibilitySniff
*/
class DeclarationCompatibilitySniff extends AbstractScopeSniff {
/**
* The name of the class we are currently checking.
*
* @var string
*/
private $currentClass = '';
/**
* A list of classes and methods to check.
*
* @var array<string, array<string, array<int|string, string|array<string, bool|string>>>>
*/
public $checkClasses = [
'WP_Widget' => [
'widget' => [ 'args', 'instance' ],
'update' => [ 'new_instance', 'old_instance' ],
'form' => [ 'instance' ],
'WP_Widget' => [
'id_base',
'name',
'widget_options' => [
'default' => 'array()',
],
'control_options' => [
'default' => 'array()',
],
],
'get_field_name' => [ 'field_name' ],
'get_field_id' => [ 'field_name' ],
'_register' => [],
'_set' => [ 'number' ],
'_get_display_callback' => [],
'_get_update_callback' => [],
'_get_form_callback' => [],
'is_preview' => [],
'display_callback' => [
'args',
'widget_args' => [
'default' => '1',
],
],
'update_callback' => [
'deprecated' => [
'default' => '1',
],
],
'form_callback' => [
'widget_args' => [
'default' => '1',
],
],
'register_one' => [
'number' => [
'default' => '-1',
],
],
'save_settings' => [ 'settings' ],
'get_settings' => [],
],
'Walker' => [
'start_lvl' => [
'output' => [
'pass_by_reference' => true,
],
'depth' => [
'default' => '0',
],
'args' => [
'default' => 'array()',
],
],
'end_lvl' => [
'output' => [
'pass_by_reference' => true,
],
'depth' => [
'default' => '0',
],
'args' => [
'default' => 'array()',
],
],
'start_el' => [
'output' => [
'pass_by_reference' => true,
],
'data_object',
'depth' => [
'default' => '0',
],
'args' => [
'default' => 'array()',
],
'current_object_id' => [
'default' => '0',
],
],
'end_el' => [
'output' => [
'pass_by_reference' => true,
],
'data_object',
'depth' => [
'default' => '0',
],
'args' => [
'default' => 'array()',
],
],
'display_element' => [
'element',
'children_elements' => [
'pass_by_reference' => true,
],
'max_depth',
'depth',
'args',
'output' => [
'pass_by_reference' => true,
],
],
'walk' => [
'elements',
'max_depth',
'args' => [
'variable_length' => true,
],
],
'paged_walk' => [
'elements',
'max_depth',
'page_num',
'per_page',
'args' => [
'variable_length' => true,
],
],
'get_number_of_root_elements' => [
'elements',
],
'unset_children' => [
'element',
'children_elements' => [
'pass_by_reference' => true,
],
],
],
];
/**
* List of grouped classes with same methods (as they extend the same parent class)
*
* @var array<string, string[]>
*/
public $checkClassesGroups = [
'Walker' => [
'Walker_Category_Checklist',
'Walker_Category',
'Walker_CategoryDropdown',
'Walker_PageDropdown',
'Walker_Nav_Menu',
'Walker_Page',
'Walker_Comment',
],
];
/**
* Constructs the test with the tokens it wishes to listen for.
*/
public function __construct() {
parent::__construct( [ T_CLASS ], [ T_FUNCTION ], true );
}
/**
* Processes this test when one of its tokens is encountered.
*
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
*
* @return void
*/
protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currScope ) {
$className = ObjectDeclarations::getName( $phpcsFile, $currScope );
if ( $className !== $this->currentClass ) {
$this->currentClass = $className;
}
$methodName = FunctionDeclarations::getName( $phpcsFile, $stackPtr );
$parentClassName = ObjectDeclarations::findExtendedClassName( $phpcsFile, $currScope );
if ( $parentClassName === false ) {
// This class does not extend any other class.
return;
}
// Need to define the originalParentClassName since we might override the parentClassName due to signature notations grouping.
$originalParentClassName = $parentClassName;
if ( array_key_exists( $parentClassName, $this->checkClasses ) === false ) {
// This class does not extend a class we are interested in.
foreach ( $this->checkClassesGroups as $parent => $children ) {
// But it might be one of the grouped classes.
foreach ( $children as $child ) {
if ( $child === $parentClassName ) {
$parentClassName = $parent;
break 2;
}
}
}
if ( array_key_exists( $parentClassName, $this->checkClasses ) === false ) {
// This class really does not extend a class we are interested in.
return;
}
}
if ( array_key_exists( $methodName, $this->checkClasses[ $parentClassName ] ) === false &&
in_array( $methodName, $this->checkClasses[ $parentClassName ], true ) === false
) {
// This method is not a one we are interested in.
return;
}
$signatureParams = FunctionDeclarations::getParameters( $phpcsFile, $stackPtr );
$parentSignature = $this->checkClasses[ $parentClassName ][ $methodName ];
if ( count( $signatureParams ) > count( $parentSignature ) ) {
$extra_params = array_slice( $signatureParams, count( $parentSignature ) - count( $signatureParams ) );
$all_extra_params_have_default = true;
foreach ( $extra_params as $extra_param ) {
if ( array_key_exists( 'default', $extra_param ) === false || $extra_param['default'] !== 'true' ) {
$all_extra_params_have_default = false;
}
}
if ( $all_extra_params_have_default === true ) {
return; // We're good.
}
}
if ( count( $signatureParams ) !== count( $parentSignature ) ) {
$this->addError( $originalParentClassName, $methodName, $signatureParams, $parentSignature, $phpcsFile, $stackPtr );
return;
}
$i = 0;
foreach ( $parentSignature as $key => $param ) {
if ( is_array( $param ) === true ) {
if (
(
array_key_exists( 'default', $param ) === true &&
array_key_exists( 'default', $signatureParams[ $i ] ) === false
) || (
array_key_exists( 'pass_by_reference', $param ) === true &&
$param['pass_by_reference'] !== $signatureParams[ $i ]['pass_by_reference']
) || (
array_key_exists( 'variable_length', $param ) === true &&
$param['variable_length'] !== $signatureParams[ $i ]['variable_length']
)
) {
$this->addError( $originalParentClassName, $methodName, $signatureParams, $parentSignature, $phpcsFile, $stackPtr );
return;
}
}
++$i;
}
}
/**
* Generates an error with nice current and parent class method notations
*
* @param string $parentClassName The name of the extended (parent) class.
* @param string $methodName The name of the method currently being examined.
* @param array $currentMethodSignature The list of params and their options of the method which is being examined.
* @param array $parentMethodSignature The list of params and their options of the parent class method.
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
private function addError( $parentClassName, $methodName, $currentMethodSignature, $parentMethodSignature, $phpcsFile, $stackPtr ) {
$currentSignature = sprintf( '%s::%s(%s)', $this->currentClass, $methodName, implode( ', ', $this->generateParamList( $currentMethodSignature ) ) );
$parentSignature = sprintf( '%s::%s(%s)', $parentClassName, $methodName, implode( ', ', $this->generateParamList( $parentMethodSignature ) ) );
$message = 'Declaration of `%s` should be compatible with `%s`.';
$data = [ $currentSignature, $parentSignature ];
$phpcsFile->addError( $message, $stackPtr, 'DeclarationCompatibility', $data );
}
/**
* Generates an array of params as they appear in the signature.
*
* @param array $methodSignature Signature of a method.
*
* @return array<string>
*/
private function generateParamList( $methodSignature ) {
$paramList = [];
foreach ( $methodSignature as $param => $options ) {
$paramName = '$';
if ( is_array( $options ) === false ) {
$paramList[] = '$' . $options;
continue;
}
if ( array_key_exists( 'name', $options ) === true ) {
$paramName = $options['name'];
} else {
$paramName .= $param;
}
if ( array_key_exists( 'variable_length', $options ) === true && $options['variable_length'] === true ) {
$paramName = '...' . $paramName;
}
if ( array_key_exists( 'pass_by_reference', $options ) === true && $options['pass_by_reference'] === true ) {
$paramName = '&' . $paramName;
}
if ( array_key_exists( 'default', $options ) === true && empty( $options['default'] ) === false ) {
$paramName .= ' = ' . trim( $options['default'] );
}
$paramList[] = $paramName;
}
return $paramList;
}
/**
* Do nothing outside the scope. Has to be implemented accordingly to parent abstract class.
*
* @param File $phpcsFile PHPCS File.
* @param int $stackPtr Stack position.
*/
public function processTokenOutsideScope( File $phpcsFile, $stackPtr ) {}
}