-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
UnusedPrivateElementsSniff.php
702 lines (602 loc) · 22.5 KB
/
UnusedPrivateElementsSniff.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Classes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\AnnotationHelper;
use SlevomatCodingStandard\Helpers\ClassHelper;
use SlevomatCodingStandard\Helpers\FunctionHelper;
use SlevomatCodingStandard\Helpers\NamespaceHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\StringHelper;
use SlevomatCodingStandard\Helpers\SuppressHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_keys;
use function array_merge;
use function array_reverse;
use function count;
use function in_array;
use function preg_match_all;
use function preg_replace;
use function sprintf;
use function strpos;
use function strtolower;
use function substr;
use const PREG_PATTERN_ORDER;
use const T_ABSTRACT;
use const T_AND_EQUAL;
use const T_BITWISE_AND;
use const T_CLASS;
use const T_CONCAT_EQUAL;
use const T_CONST;
use const T_DIV_EQUAL;
use const T_DOUBLE_COLON;
use const T_DOUBLE_QUOTED_STRING;
use const T_EQUAL;
use const T_FUNCTION;
use const T_HEREDOC;
use const T_MINUS_EQUAL;
use const T_MOD_EQUAL;
use const T_MUL_EQUAL;
use const T_NEW;
use const T_NULLABLE;
use const T_OBJECT_OPERATOR;
use const T_OPEN_PARENTHESIS;
use const T_OR_EQUAL;
use const T_PLUS_EQUAL;
use const T_POW_EQUAL;
use const T_PRIVATE;
use const T_PROTECTED;
use const T_PUBLIC;
use const T_SELF;
use const T_SL_EQUAL;
use const T_SR_EQUAL;
use const T_STATIC;
use const T_STRING;
use const T_VARIABLE;
use const T_XOR_EQUAL;
/**
* @deprecated https://phpstan.org/blog/detecting-unused-private-properties-methods-constants
*/
class UnusedPrivateElementsSniff implements Sniff
{
public const CODE_UNUSED_PROPERTY = 'UnusedProperty';
public const CODE_WRITE_ONLY_PROPERTY = 'WriteOnlyProperty';
public const CODE_UNUSED_METHOD = 'UnusedMethod';
public const CODE_UNUSED_CONSTANT = 'UnusedConstant';
private const NAME = 'SlevomatCodingStandard.Classes.UnusedPrivateElements';
/** @var string[] */
public $alwaysUsedPropertiesAnnotations = [];
/** @var string[] */
public $alwaysUsedPropertiesSuffixes = [];
/** @var string[] */
public $alwaysUsedMethodsAnnotations = [];
/** @var string[]|null */
private $normalizedAlwaysUsedPropertiesAnnotations;
/** @var string[]|null */
private $normalizedAlwaysUsedPropertiesSuffixes;
/** @var string[]|null */
private $normalizedAlwaysUsedMethodsAnnotations;
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_CLASS,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param File $phpcsFile
* @param int $classPointer
*/
public function process(File $phpcsFile, $classPointer): void
{
$tokens = $phpcsFile->getTokens();
$classToken = $tokens[$classPointer];
$className = ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer);
$reportedProperties = $this->getProperties($phpcsFile, $classPointer);
$reportedConstants = $this->getConstants($phpcsFile, $classPointer);
$reportedMethods = [];
$originalMethods = [];
foreach ($this->getMethods($phpcsFile, $classPointer) as $methodName => $methodPointer) {
$normalizedMethodName = $this->getNormalizedMethodName($methodName);
$reportedMethods[$normalizedMethodName] = $methodPointer;
$originalMethods[$normalizedMethodName] = $methodName;
}
if (count($reportedProperties) + count($reportedMethods) + count($reportedConstants) === 0) {
return;
}
$writeOnlyProperties = [];
$findUsagesStartTokenPointer = $classToken['scope_opener'] + 1;
$isCurrentClass = static function (int $referencedNamePointer) use ($phpcsFile, $tokens, $className): bool {
if (in_array($tokens[$referencedNamePointer]['code'], [T_SELF, T_STATIC], true)) {
return true;
}
if (in_array($tokens[$referencedNamePointer]['code'], TokenHelper::getNameTokenCodes(), true)) {
$referencedClassName = NamespaceHelper::resolveClassName(
$phpcsFile,
$tokens[$referencedNamePointer]['content'],
$referencedNamePointer
);
if ($className === $referencedClassName) {
return true;
}
}
return false;
};
$checkPropertyUsage = function (int $propertyNameTokenPointer, int $thisOrSelfTokenPointer) use ($phpcsFile, $tokens, &$reportedProperties, &$writeOnlyProperties): void {
$propertyName = $this->getNormalizedPropertyName($tokens[$propertyNameTokenPointer]['content']);
$possibleNewTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $thisOrSelfTokenPointer - 1);
if ($possibleNewTokenPointer !== null && $tokens[$possibleNewTokenPointer]['code'] === T_NEW) {
unset($reportedProperties[$propertyName]);
return;
}
$possibleAssignTokenPointer = TokenHelper::findNextEffective($phpcsFile, $propertyNameTokenPointer + 1);
$possibleAssignToken = $tokens[$possibleAssignTokenPointer];
if (
in_array($possibleAssignToken['code'], [
T_EQUAL,
T_PLUS_EQUAL,
T_MINUS_EQUAL,
T_MUL_EQUAL,
T_DIV_EQUAL,
T_POW_EQUAL,
T_MOD_EQUAL,
T_AND_EQUAL,
T_OR_EQUAL,
T_XOR_EQUAL,
T_SL_EQUAL,
T_SR_EQUAL,
T_CONCAT_EQUAL,
], true)
) {
$pointerAfterAssignToken = TokenHelper::findNextEffective($phpcsFile, $possibleAssignTokenPointer + 1);
if ($tokens[$pointerAfterAssignToken]['code'] !== T_BITWISE_AND) {
$writeOnlyProperties[$propertyName] = $propertyNameTokenPointer;
return;
}
}
unset($reportedProperties[$propertyName]);
};
$checkObjectOperatorUsage = function (int $objectOperatorTokenPointer, int $thisTokenPointer) use ($phpcsFile, $tokens, $checkPropertyUsage, &$reportedMethods): int {
$nextTokenPointer = TokenHelper::findNextEffective($phpcsFile, $objectOperatorTokenPointer + 1);
$nextToken = $tokens[$nextTokenPointer];
if ($nextToken['code'] !== T_STRING) {
// $variable-> but not accessing a specific property (e. g. $variable->$foo or $variable->{$foo})
return $objectOperatorTokenPointer + 1;
}
$methodCallTokenPointer = TokenHelper::findNextEffective($phpcsFile, $nextTokenPointer + 1);
$methodCallToken = $tokens[$methodCallTokenPointer];
if ($methodCallToken['code'] === T_OPEN_PARENTHESIS) {
$possibleNewTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $thisTokenPointer - 1);
if ($possibleNewTokenPointer === null || $tokens[$possibleNewTokenPointer]['code'] !== T_NEW) {
// Calling a method on $variable
unset($reportedMethods[$this->getNormalizedMethodName($nextToken['content'])]);
return $methodCallTokenPointer + 1;
}
}
$checkPropertyUsage($nextTokenPointer, $thisTokenPointer);
return $nextTokenPointer + 1;
};
$checkDoubleColonUsage = function (int $doubleColonTokenPointer, int $selfTokenPointer) use ($phpcsFile, $tokens, $checkPropertyUsage, &$reportedMethods, &$reportedConstants): int {
$nextTokenPointer = TokenHelper::findNextEffective($phpcsFile, $doubleColonTokenPointer + 1);
$nextToken = $tokens[$nextTokenPointer];
if ($nextToken['code'] !== T_STRING) {
// self:: or static:: not followed by a string - possible static property access
if ($nextToken['code'] === T_VARIABLE) {
$checkPropertyUsage($nextTokenPointer, $selfTokenPointer);
}
return $nextTokenPointer + 1;
}
$tokenPointerAfterNextToken = TokenHelper::findNextEffective($phpcsFile, $nextTokenPointer + 1);
$tokenAfterNextToken = $tokens[$tokenPointerAfterNextToken];
if ($tokenAfterNextToken['code'] !== T_OPEN_PARENTHESIS) {
// self::string or static::string not followed by ( - possible constant access
$name = $nextToken['content'];
unset($reportedConstants[$name]);
return $tokenPointerAfterNextToken + 1;
}
$name = $this->getNormalizedMethodName($nextToken['content']);
unset($reportedMethods[$name]);
return $tokenPointerAfterNextToken + 1;
};
while (($tokenPointer = TokenHelper::findNext(
$phpcsFile,
[T_NEW, T_HEREDOC, T_DOUBLE_QUOTED_STRING, T_DOUBLE_COLON, T_OBJECT_OPERATOR],
$findUsagesStartTokenPointer,
$classToken['scope_closer']
)) !== null) {
$token = $tokens[$tokenPointer];
if (in_array($token['code'], [T_HEREDOC, T_DOUBLE_QUOTED_STRING], true)) {
if (preg_match_all('~(?<!\\\\)\$this->(.+?\b)(?!\()~', $token['content'], $matches, PREG_PATTERN_ORDER) !== 0) {
foreach ($matches[1] as $propertyInString) {
if (!isset($reportedProperties[$propertyInString])) {
continue;
}
unset($reportedProperties[$propertyInString]);
}
}
if (preg_match_all('~(?:self|static)::(.+?\b)~', $token['content'], $matches, PREG_PATTERN_ORDER) !== 0) {
foreach ($matches[1] as $constantInString) {
if (!isset($reportedConstants[$constantInString])) {
continue;
}
unset($reportedConstants[$constantInString]);
}
}
if (preg_match_all('~(?<=\{)\$this->(.+?\b)(?=\()~', $token['content'], $matches, PREG_PATTERN_ORDER) !== 0) {
foreach ($matches[1] as $methodInString) {
$normalizedMethodInString = $this->getNormalizedMethodName($methodInString);
if (!isset($reportedMethods[$normalizedMethodInString])) {
continue;
}
unset($reportedMethods[$normalizedMethodInString]);
}
}
$findUsagesStartTokenPointer = $tokenPointer + 1;
} elseif ($token['code'] === T_OBJECT_OPERATOR) {
$variableTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokenPointer - 1);
if ($tokens[$variableTokenPointer]['code'] === T_VARIABLE) {
$variableName = $tokens[$variableTokenPointer]['content'];
if ($variableName === '$this') {
$findUsagesStartTokenPointer = $checkObjectOperatorUsage($tokenPointer, $variableTokenPointer);
} else {
$functionPointer = null;
foreach (array_reverse(
$tokens[$variableTokenPointer]['conditions'],
true
) as $conditionPointer => $conditionTokenCode) {
if (in_array($conditionTokenCode, TokenHelper::$functionTokenCodes, true)) {
$functionPointer = $conditionPointer;
break;
}
}
if ($functionPointer !== null) {
$parameterPointer = TokenHelper::findNextContent(
$phpcsFile,
T_VARIABLE,
$variableName,
$tokens[$functionPointer]['parenthesis_opener'] + 1,
$tokens[$functionPointer]['parenthesis_closer']
);
if ($parameterPointer !== null) {
$typeHintPointer = TokenHelper::findPreviousEffective($phpcsFile, $parameterPointer - 1);
if ($isCurrentClass($typeHintPointer)) {
$checkObjectOperatorUsage($tokenPointer, $variableTokenPointer);
}
}
}
$findUsagesStartTokenPointer = $tokenPointer + 1;
}
} else {
$possibleThisTokenPointer = $tokenPointer - 1;
do {
$possibleThisTokenPointer = TokenHelper::findPreviousLocal($phpcsFile, T_VARIABLE, $possibleThisTokenPointer - 1);
} while (
$possibleThisTokenPointer !== null
&& $tokens[$possibleThisTokenPointer]['content'] !== '$this'
);
if ($possibleThisTokenPointer !== null) {
$possibleMethodNamePointer = TokenHelper::findNextEffective($phpcsFile, $tokenPointer + 1);
if ($tokens[$possibleMethodNamePointer]['code'] === T_STRING) {
$possibleMethodCallPointer = TokenHelper::findNextEffective($phpcsFile, $possibleMethodNamePointer + 1);
if ($tokens[$possibleMethodCallPointer]['code'] === T_OPEN_PARENTHESIS) {
$methodName = $this->getNormalizedMethodName($tokens[$possibleMethodNamePointer]['content']);
if (isset($reportedMethods[$methodName])) {
unset($reportedMethods[$methodName]);
$findUsagesStartTokenPointer = $possibleMethodCallPointer + 1;
continue;
}
}
}
}
$findUsagesStartTokenPointer = $tokenPointer + 1;
}
} elseif ($token['code'] === T_DOUBLE_COLON) {
$previousTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokenPointer - 1);
$findUsagesStartTokenPointer = $isCurrentClass($previousTokenPointer)
? $checkDoubleColonUsage($tokenPointer, $previousTokenPointer)
: $tokenPointer + 1;
} elseif ($token['code'] === T_NEW) {
$variableTokenPointer = TokenHelper::findPreviousLocal($phpcsFile, T_VARIABLE, $tokenPointer - 1);
$nextTokenPointer = TokenHelper::findNextEffective($phpcsFile, $tokenPointer + 1);
if ($variableTokenPointer !== null && $isCurrentClass($nextTokenPointer)) {
$scopeMethodPointer = TokenHelper::findPrevious($phpcsFile, T_FUNCTION, $variableTokenPointer - 1);
for ($i = $tokens[$scopeMethodPointer]['scope_opener']; $i < $tokens[$scopeMethodPointer]['scope_closer']; $i++) {
if ($tokens[$i]['content'] !== $tokens[$variableTokenPointer]['content']) {
continue;
}
$afterActualTokenPointer = TokenHelper::findNextEffective($phpcsFile, $i + 1);
if ($tokens[$afterActualTokenPointer]['code'] === T_OBJECT_OPERATOR) {
$checkObjectOperatorUsage($afterActualTokenPointer, $i);
} elseif ($tokens[$afterActualTokenPointer]['code'] === T_DOUBLE_COLON) {
$checkDoubleColonUsage($afterActualTokenPointer, $i);
}
}
}
$findUsagesStartTokenPointer = $tokenPointer + 1;
}
if (count($reportedProperties) + count($reportedMethods) + count($reportedConstants) !== 0) {
continue;
}
return;
}
$className = ClassHelper::getName($phpcsFile, $classPointer);
foreach ($reportedProperties as $name => $propertyTokenPointer) {
if (isset($writeOnlyProperties[$name])) {
if (!SuppressHelper::isSniffSuppressed(
$phpcsFile,
$propertyTokenPointer,
$this->getSniffName(self::CODE_WRITE_ONLY_PROPERTY)
)) {
$phpcsFile->addError(sprintf(
'Class %s contains write-only property $%s.',
$className,
$name
), $propertyTokenPointer, self::CODE_WRITE_ONLY_PROPERTY);
}
} else {
if (!SuppressHelper::isSniffSuppressed(
$phpcsFile,
$propertyTokenPointer,
$this->getSniffName(self::CODE_UNUSED_PROPERTY)
)) {
$phpcsFile->addError(sprintf(
'Class %s contains unused property $%s.',
$className,
$name
), $propertyTokenPointer, self::CODE_UNUSED_PROPERTY);
}
}
}
foreach ($reportedMethods as $name => $methodTokenPointer) {
if (SuppressHelper::isSniffSuppressed($phpcsFile, $methodTokenPointer, $this->getSniffName(self::CODE_UNUSED_METHOD))) {
continue;
}
$phpcsFile->addError(sprintf(
'Class %s contains unused private method %s().',
$className,
$originalMethods[$name]
), $methodTokenPointer, self::CODE_UNUSED_METHOD);
}
foreach ($reportedConstants as $name => $constantTokenPointer) {
if (SuppressHelper::isSniffSuppressed($phpcsFile, $constantTokenPointer, $this->getSniffName(self::CODE_UNUSED_CONSTANT))) {
continue;
}
$phpcsFile->addError(sprintf(
'Class %s contains unused private constant %s.',
$className,
$name
), $constantTokenPointer, self::CODE_UNUSED_CONSTANT);
}
}
/**
* @return string[]
*/
private function getAlwaysUsedPropertiesAnnotations(): array
{
if ($this->normalizedAlwaysUsedPropertiesAnnotations === null) {
$this->normalizedAlwaysUsedPropertiesAnnotations = SniffSettingsHelper::normalizeArray($this->alwaysUsedPropertiesAnnotations);
}
return $this->normalizedAlwaysUsedPropertiesAnnotations;
}
/**
* @return string[]
*/
private function getAlwaysUsedPropertiesSuffixes(): array
{
if ($this->normalizedAlwaysUsedPropertiesSuffixes === null) {
$this->normalizedAlwaysUsedPropertiesSuffixes = SniffSettingsHelper::normalizeArray($this->alwaysUsedPropertiesSuffixes);
}
return $this->normalizedAlwaysUsedPropertiesSuffixes;
}
/**
* @return string[]
*/
private function getAlwaysUsedMethodsAnnotations(): array
{
if ($this->normalizedAlwaysUsedMethodsAnnotations === null) {
$this->normalizedAlwaysUsedMethodsAnnotations = SniffSettingsHelper::normalizeArray($this->alwaysUsedMethodsAnnotations);
}
return $this->normalizedAlwaysUsedMethodsAnnotations;
}
private function getSniffName(string $sniffName): string
{
return sprintf('%s.%s', self::NAME, $sniffName);
}
/**
* @param File $phpcsFile
* @param int $classTokenPointer
* @return int[] string(name) => pointer
*/
private function getProperties(File $phpcsFile, int $classTokenPointer): array
{
$tokens = $phpcsFile->getTokens();
$classToken = $tokens[$classTokenPointer];
$reportedProperties = [];
$findPropertiesStartTokenPointer = $classToken['scope_opener'] + 1;
while (true) {
$propertyTokenPointer = TokenHelper::findNext(
$phpcsFile,
T_VARIABLE,
$findPropertiesStartTokenPointer,
$classToken['scope_closer']
);
if ($propertyTokenPointer === null) {
break;
}
$visibilityModifierTokenPointer = $this->findVisibilityPointer($phpcsFile, $propertyTokenPointer);
if (
$visibilityModifierTokenPointer === null
|| $tokens[$visibilityModifierTokenPointer]['code'] !== T_PRIVATE
) {
$findPropertiesStartTokenPointer = $propertyTokenPointer + 1;
continue;
}
$findPropertiesStartTokenPointer = $propertyTokenPointer + 1;
$annotationNames = $this->getAnnotationNames($phpcsFile, $visibilityModifierTokenPointer);
$alwaysUsedProperty = false;
foreach ($annotationNames as $annotationName) {
foreach ($this->getAlwaysUsedPropertiesAnnotations() as $alwaysUsedPropertyAnnotationName) {
if ($annotationName === $alwaysUsedPropertyAnnotationName) {
$alwaysUsedProperty = true;
break 2;
}
if (
substr($alwaysUsedPropertyAnnotationName, -1) === '\\'
&& strpos($annotationName, $alwaysUsedPropertyAnnotationName) === 0
) {
$alwaysUsedProperty = true;
break 2;
}
}
}
if ($alwaysUsedProperty) {
continue;
}
$propertyToken = $tokens[$propertyTokenPointer];
$name = substr($propertyToken['content'], 1);
foreach ($this->getAlwaysUsedPropertiesSuffixes() as $prefix) {
if (!StringHelper::endsWith($name, $prefix)) {
continue;
}
continue 2;
}
$reportedProperties[$name] = $propertyTokenPointer;
}
return $reportedProperties;
}
/**
* @param File $phpcsFile
* @param int $privateTokenPointer
* @return string[]
*/
private function getAnnotationNames(File $phpcsFile, int $privateTokenPointer): array
{
return array_keys(AnnotationHelper::getAnnotations($phpcsFile, $privateTokenPointer));
}
/**
* @param File $phpcsFile
* @param int $classTokenPointer
* @return int[] string(name) => pointer
*/
private function getMethods(File $phpcsFile, int $classTokenPointer): array
{
$tokens = $phpcsFile->getTokens();
$classToken = $tokens[$classTokenPointer];
$reportedMethods = [];
$findMethodsStartTokenPointer = $classToken['scope_opener'] + 1;
while (true) {
$methodTokenPointer = TokenHelper::findNext($phpcsFile, T_FUNCTION, $findMethodsStartTokenPointer, $classToken['scope_closer']);
if ($methodTokenPointer === null) {
break;
}
$visibilityModifierTokenPointer = $this->findVisibilityPointer($phpcsFile, $methodTokenPointer);
if (
$visibilityModifierTokenPointer === null
|| $tokens[$visibilityModifierTokenPointer]['code'] !== T_PRIVATE
) {
$findMethodsStartTokenPointer = $methodTokenPointer + 1;
continue;
}
$findMethodsStartTokenPointer = $methodTokenPointer + 1;
$annotationNames = $this->getAnnotationNames($phpcsFile, $visibilityModifierTokenPointer);
$alwaysUsedMethod = false;
foreach ($annotationNames as $annotationName) {
foreach ($this->getAlwaysUsedMethodsAnnotations() as $alwaysUsedMethodAnnotationName) {
if ($annotationName === $alwaysUsedMethodAnnotationName) {
$alwaysUsedMethod = true;
break 2;
}
if (
substr($alwaysUsedMethodAnnotationName, -1) === '\\'
&& strpos($annotationName, $alwaysUsedMethodAnnotationName) === 0
) {
$alwaysUsedMethod = true;
break 2;
}
}
}
if ($alwaysUsedMethod) {
continue;
}
$methodName = FunctionHelper::getName($phpcsFile, $methodTokenPointer);
if (in_array($methodName, ['__construct', '__clone'], true)) {
continue;
}
$reportedMethods[$methodName] = $methodTokenPointer;
}
return $reportedMethods;
}
private function getNormalizedMethodName(string $methodName): string
{
return strtolower($methodName);
}
private function getNormalizedPropertyName(string $propertyName): string
{
return preg_replace('~^\$~', '', $propertyName);
}
/**
* @param File $phpcsFile
* @param int $classTokenPointer
* @return int[] string(name) => pointer
*/
private function getConstants(File $phpcsFile, int $classTokenPointer): array
{
$tokens = $phpcsFile->getTokens();
$classToken = $tokens[$classTokenPointer];
$reportedConstants = [];
$findConstantsStartTokenPointer = $classToken['scope_opener'] + 1;
while (true) {
$constantTokenPointer = TokenHelper::findNext(
$phpcsFile,
T_CONST,
$findConstantsStartTokenPointer,
$classToken['scope_closer']
);
if ($constantTokenPointer === null) {
break;
}
$visibilityModifierTokenPointer = $this->findVisibilityPointer($phpcsFile, $constantTokenPointer);
if (
$visibilityModifierTokenPointer === null
|| $tokens[$visibilityModifierTokenPointer]['code'] !== T_PRIVATE
) {
$findConstantsStartTokenPointer = $constantTokenPointer + 1;
continue;
}
$namePointer = TokenHelper::findNextEffective($phpcsFile, $constantTokenPointer + 1);
$constantName = $tokens[$namePointer]['content'];
$reportedConstants[$constantName] = $constantTokenPointer;
$findConstantsStartTokenPointer = $constantTokenPointer + 1;
}
return $reportedConstants;
}
private function findVisibilityPointer(File $phpcsFile, int $pointer): ?int
{
$tokens = $phpcsFile->getTokens();
$visibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $pointer - 1);
if (in_array($tokens[$visibilityPointer]['code'], array_merge([T_SELF], TokenHelper::getNameTokenCodes()), true)) {
$visibilityPointer = TokenHelper::findPreviousExcluding(
$phpcsFile,
array_merge(TokenHelper::getNameTokenCodes(), TokenHelper::$ineffectiveTokenCodes),
$visibilityPointer - 1
);
if ($tokens[$visibilityPointer]['code'] === T_NULLABLE) {
$visibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $visibilityPointer - 1);
}
}
if (in_array($tokens[$visibilityPointer]['code'], [T_ABSTRACT, T_STATIC], true)) {
$visibilityPointer = TokenHelper::findPreviousExcluding(
$phpcsFile,
array_merge([T_ABSTRACT, T_STATIC], TokenHelper::$ineffectiveTokenCodes),
$visibilityPointer - 1
);
}
if (in_array($tokens[$visibilityPointer]['code'], [T_PUBLIC, T_PROTECTED, T_PRIVATE], true)) {
return $visibilityPointer;
}
return null;
}
}