/
BCFile.php
857 lines (801 loc) · 37.3 KB
/
BCFile.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*
* The methods in this class are imported from the PHP_CodeSniffer project.
* Note: this is not a one-on-one import of the `File` class!
*
* Copyright of the original code in this class as per the import:
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Jaroslav Hanslík <kukulich@kukulich.cz>
* @author jdavis <jdavis@bamboohr.com>
* @author Klaus Purer <klaus.purer@gmail.com>
* @author Juliette Reinders Folmer <jrf@phpcodesniffer.info>
* @author Nick Wilde <nick@briarmoon.ca>
* @author Martin Hujer <mhujer@gmail.com>
* @author Chris Wilkinson <c.wilkinson@elifesciences.org>
*
* With documentation contributions from:
* @author Pascal Borreli <pascal@borreli.com>
* @author Diogo Oliveira de Melo <dmelo87@gmail.com>
* @author Stefano Kowalke <blueduck@gmx.net>
* @author George Mponos <gmponos@gmail.com>
* @author Tyson Andre <tysonandre775@hotmail.com>
* @author Klaus Purer <klaus.purer@protonmail.ch>
*
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHPCSUtils\BackCompat;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
/**
* PHPCS native utility functions.
*
* Backport of the latest versions of PHPCS native utility functions to make them
* available in older PHPCS versions without the bugs and other quirks that the
* older versions of the native functions had.
*
* Additionally, this class works round the following tokenizer issues for
* any affected utility functions:
* - `readonly` classes.
* - Constructor property promotion with `readonly` without visibility.
* - OO methods called `self`, `parent` or `static`.
*
* Most functions in this class will have a related twin-function in the relevant
* class in the `PHPCSUtils\Utils` namespace.
* These will be indicated with `@see` tags in the docblock of the function.
*
* The PHPCSUtils native twin-functions will often have additional features and/or
* improved functionality, but will generally be fully compatible with the PHPCS
* native functions.
* The differences between the functions here and the twin functions are documented
* in the docblock of the respective twin-function.
*
* @see \PHP_CodeSniffer\Files\File Original source of these utility methods.
*
* @since 1.0.0
*/
final class BCFile
{
/**
* Returns the declaration name for classes, interfaces, traits, enums, and functions.
*
* PHPCS cross-version compatible version of the `File::getDeclarationName()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - PHPCS 3.8.0: OO methods called `self`, `parent` or `static` are now correctly recognized.
*
* @see \PHP_CodeSniffer\Files\File::getDeclarationName() Original source.
* @see \PHPCSUtils\Utils\ObjectDeclarations::getName() PHPCSUtils native improved version.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the declaration token
* which declared the class, interface,
* trait, enum or function.
*
* @return string|null The name of the class, interface, trait, enum, or function;
* or `NULL` if the function or class is anonymous or
* in case of a parse error/live coding.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type
* `T_FUNCTION`, `T_CLASS`, `T_ANON_CLASS`,
* `T_CLOSURE`, `T_TRAIT`, `T_ENUM` or `T_INTERFACE`.
*/
public static function getDeclarationName(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$tokenCode = $tokens[$stackPtr]['code'];
if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) {
return null;
}
if ($tokenCode !== T_FUNCTION
&& $tokenCode !== T_CLASS
&& $tokenCode !== T_INTERFACE
&& $tokenCode !== T_TRAIT
&& $tokenCode !== T_ENUM
) {
throw new RuntimeException('Token type "' . $tokens[$stackPtr]['type'] . '" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
}
if ($tokenCode === T_FUNCTION
&& strtolower($tokens[$stackPtr]['content']) !== 'function'
) {
// This is a function declared without the "function" keyword.
// So this token is the function name.
return $tokens[$stackPtr]['content'];
}
$content = null;
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
if ($tokens[$i]['code'] === T_STRING
// BC: PHPCS < 3.8.0.
|| $tokens[$i]['code'] === T_SELF
|| $tokens[$i]['code'] === T_PARENT
|| $tokens[$i]['code'] === T_STATIC
) {
$content = $tokens[$i]['content'];
break;
}
}
return $content;
}
/**
* Returns the method parameters for the specified function token.
*
* Also supports passing in a `T_USE` token for a closure use group.
*
* Each parameter is in the following format:
* ```php
* 0 => array(
* 'name' => string, // The variable name.
* 'token' => integer, // The stack pointer to the variable name.
* 'content' => string, // The full content of the variable definition.
* 'has_attributes' => boolean, // Does the parameter have one or more attributes attached ?
* 'pass_by_reference' => boolean, // Is the variable passed by reference?
* 'reference_token' => integer|false, // The stack pointer to the reference operator
* // or FALSE if the param is not passed by reference.
* 'variable_length' => boolean, // Is the param of variable length through use of `...` ?
* 'variadic_token' => integer|false, // The stack pointer to the ... operator
* // or FALSE if the param is not variable length.
* 'type_hint' => string, // The type hint for the variable.
* 'type_hint_token' => integer|false, // The stack pointer to the start of the type hint
* // or FALSE if there is no type hint.
* 'type_hint_end_token' => integer|false, // The stack pointer to the end of the type hint
* // or FALSE if there is no type hint.
* 'nullable_type' => boolean, // TRUE if the var type is preceded by the nullability
* // operator.
* 'comma_token' => integer|false, // The stack pointer to the comma after the param
* // or FALSE if this is the last param.
* )
* ```
*
* Parameters with default values have the following additional array indexes:
* ```php
* 'default' => string, // The full content of the default value.
* 'default_token' => integer, // The stack pointer to the start of the default value.
* 'default_equal_token' => integer, // The stack pointer to the equals sign.
* ```
*
* Parameters declared using PHP 8 constructor property promotion, have these additional array indexes:
* ```php
* 'property_visibility' => string, // The property visibility as declared.
* 'visibility_token' => integer,|false // The stack pointer to the visibility modifier token.
* // or FALSE if the visibility is not explicitly declared.
* 'property_readonly' => bool, // TRUE if the readonly keyword was found.
* 'readonly_token' => integer, // The stack pointer to the readonly modifier token.
* // This index will only be set if the property is readonly.
* ```
*
* PHPCS cross-version compatible version of the `File::getMethodParameters()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - PHPCS 3.8.0: Added support for constructor property promotion with readonly without explicit visibility.
*
* @see \PHP_CodeSniffer\Files\File::getMethodParameters() Original source.
* @see \PHPCSUtils\Utils\FunctionDeclarations::getParameters() PHPCSUtils native improved version.
*
* @since 1.0.0
* @since 1.0.6 Sync with PHPCS 3.8.0, support for readonly properties without explicit visibility. PHPCS#3801.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack of the function token
* to acquire the parameters for.
*
* @return array
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified `$stackPtr` is not of
* type `T_FUNCTION`, `T_CLOSURE`, `T_USE`,
* or `T_FN`.
*/
public static function getMethodParameters(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset(Collections::functionDeclarationTokens()[$tokens[$stackPtr]['code']]) === false
&& $tokens[$stackPtr]['code'] !== T_USE
) {
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_USE or T_FN');
}
if ($tokens[$stackPtr]['code'] === T_USE) {
$opener = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1));
if ($opener === false
|| (isset($tokens[$opener]['parenthesis_owner']) === true
// BC: as of PHPCS 4.x, closure use tokens are parentheses owners.
&& $tokens[$opener]['parenthesis_owner'] !== $stackPtr)
) {
throw new RuntimeException('$stackPtr was not a valid T_USE');
}
} else {
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
// Live coding or syntax error, so no params to find.
return [];
}
$opener = $tokens[$stackPtr]['parenthesis_opener'];
}
if (isset($tokens[$opener]['parenthesis_closer']) === false) {
// Live coding or syntax error, so no params to find.
return [];
}
$closer = $tokens[$opener]['parenthesis_closer'];
$vars = [];
$currVar = null;
$paramStart = ($opener + 1);
$defaultStart = null;
$equalToken = null;
$paramCount = 0;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$readonlyToken = null;
for ($i = $paramStart; $i <= $closer; $i++) {
// Check to see if this token has a parenthesis or bracket opener. If it does
// it's likely to be an array which might have arguments in it. This
// could cause problems in our parsing below, so lets just skip to the
// end of it.
if (isset($tokens[$i]['parenthesis_opener']) === true) {
// Don't do this if it's the close parenthesis for the method.
if ($i !== $tokens[$i]['parenthesis_closer']) {
$i = ($tokens[$i]['parenthesis_closer'] + 1);
}
}
if (isset($tokens[$i]['bracket_opener']) === true) {
// Don't do this if it's the close parenthesis for the method.
if ($i !== $tokens[$i]['bracket_closer']) {
$i = ($tokens[$i]['bracket_closer'] + 1);
}
}
switch ($tokens[$i]['code']) {
case T_ATTRIBUTE:
$hasAttributes = true;
// Skip to the end of the attribute.
$i = $tokens[$i]['attribute_closer'];
break;
case T_BITWISE_AND:
if ($defaultStart === null) {
$passByReference = true;
$referenceToken = $i;
}
break;
case T_VARIABLE:
$currVar = $i;
break;
case T_ELLIPSIS:
$variableLength = true;
$variadicToken = $i;
break;
case T_CALLABLE:
if ($typeHintToken === false) {
$typeHintToken = $i;
}
$typeHint .= $tokens[$i]['content'];
$typeHintEndToken = $i;
break;
case T_SELF:
case T_PARENT:
case T_STATIC:
// Self and parent are valid, static invalid, but was probably intended as type hint.
if (isset($defaultStart) === false) {
if ($typeHintToken === false) {
$typeHintToken = $i;
}
$typeHint .= $tokens[$i]['content'];
$typeHintEndToken = $i;
}
break;
case T_STRING:
case T_NAME_QUALIFIED:
case T_NAME_FULLY_QUALIFIED:
case T_NAME_RELATIVE:
// This is an identifier name, so it may be a type declaration, but it could
// also be a constant used as a default value.
$prevComma = false;
for ($t = $i; $t >= $opener; $t--) {
if ($tokens[$t]['code'] === T_COMMA) {
$prevComma = $t;
break;
}
}
if ($prevComma !== false) {
$nextEquals = false;
for ($t = $prevComma; $t < $i; $t++) {
if ($tokens[$t]['code'] === T_EQUAL) {
$nextEquals = $t;
break;
}
}
if ($nextEquals !== false) {
break;
}
}
if ($defaultStart === null) {
if ($typeHintToken === false) {
$typeHintToken = $i;
}
$typeHint .= $tokens[$i]['content'];
$typeHintEndToken = $i;
}
break;
case T_NAMESPACE:
case T_NS_SEPARATOR:
case T_TYPE_UNION:
case T_TYPE_INTERSECTION:
case T_FALSE:
case T_NULL:
// Part of a type hint or default value.
if ($defaultStart === null) {
if ($typeHintToken === false) {
$typeHintToken = $i;
}
$typeHint .= $tokens[$i]['content'];
$typeHintEndToken = $i;
}
break;
case T_NULLABLE:
if ($defaultStart === null) {
$nullableType = true;
$typeHint .= $tokens[$i]['content'];
$typeHintEndToken = $i;
}
break;
case T_PUBLIC:
case T_PROTECTED:
case T_PRIVATE:
if ($defaultStart === null) {
$visibilityToken = $i;
}
break;
case T_READONLY:
if ($defaultStart === null) {
$readonlyToken = $i;
}
break;
case T_CLOSE_PARENTHESIS:
case T_COMMA:
// If it's null, then there must be no parameters for this
// method.
if ($currVar === null) {
continue 2;
}
$vars[$paramCount] = [];
$vars[$paramCount]['token'] = $currVar;
$vars[$paramCount]['name'] = $tokens[$currVar]['content'];
$vars[$paramCount]['content'] = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart)));
if ($defaultStart !== null) {
$vars[$paramCount]['default'] = trim($phpcsFile->getTokensAsString($defaultStart, ($i - $defaultStart)));
$vars[$paramCount]['default_token'] = $defaultStart;
$vars[$paramCount]['default_equal_token'] = $equalToken;
}
$vars[$paramCount]['has_attributes'] = $hasAttributes;
$vars[$paramCount]['pass_by_reference'] = $passByReference;
$vars[$paramCount]['reference_token'] = $referenceToken;
$vars[$paramCount]['variable_length'] = $variableLength;
$vars[$paramCount]['variadic_token'] = $variadicToken;
$vars[$paramCount]['type_hint'] = $typeHint;
$vars[$paramCount]['type_hint_token'] = $typeHintToken;
$vars[$paramCount]['type_hint_end_token'] = $typeHintEndToken;
$vars[$paramCount]['nullable_type'] = $nullableType;
if ($visibilityToken !== null || $readonlyToken !== null) {
$vars[$paramCount]['property_visibility'] = 'public';
$vars[$paramCount]['visibility_token'] = false;
$vars[$paramCount]['property_readonly'] = false;
if ($visibilityToken !== null) {
$vars[$paramCount]['property_visibility'] = $tokens[$visibilityToken]['content'];
$vars[$paramCount]['visibility_token'] = $visibilityToken;
}
if ($readonlyToken !== null) {
$vars[$paramCount]['property_readonly'] = true;
$vars[$paramCount]['readonly_token'] = $readonlyToken;
}
}
if ($tokens[$i]['code'] === T_COMMA) {
$vars[$paramCount]['comma_token'] = $i;
} else {
$vars[$paramCount]['comma_token'] = false;
}
// Reset the vars, as we are about to process the next parameter.
$currVar = null;
$paramStart = ($i + 1);
$defaultStart = null;
$equalToken = null;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$readonlyToken = null;
++$paramCount;
break;
case T_EQUAL:
$defaultStart = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
$equalToken = $i;
break;
}
}
return $vars;
}
/**
* Returns the visibility and implementation properties of a method.
*
* The format of the return value is:
* ```php
* array(
* 'scope' => string, // Public, private, or protected
* 'scope_specified' => boolean, // TRUE if the scope keyword was found.
* 'return_type' => string, // The return type of the method.
* 'return_type_token' => integer|false, // The stack pointer to the start of the return type
* // or FALSE if there is no return type.
* 'return_type_end_token' => integer|false, // The stack pointer to the end of the return type
* // or FALSE if there is no return type.
* 'nullable_return_type' => boolean, // TRUE if the return type is preceded by
* // the nullability operator.
* 'is_abstract' => boolean, // TRUE if the abstract keyword was found.
* 'is_final' => boolean, // TRUE if the final keyword was found.
* 'is_static' => boolean, // TRUE if the static keyword was found.
* 'has_body' => boolean, // TRUE if the method has a body
* );
* ```
*
* PHPCS cross-version compatible version of the `File::getMethodProperties()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::getMethodProperties() Original source.
* @see \PHPCSUtils\Utils\FunctionDeclarations::getProperties() PHPCSUtils native improved version.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack of the function token to
* acquire the properties for.
*
* @return array
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
* `T_FUNCTION`, `T_CLOSURE`, or `T_FN` token.
*/
public static function getMethodProperties(File $phpcsFile, $stackPtr)
{
return $phpcsFile->getMethodProperties($stackPtr);
}
/**
* Returns the visibility and implementation properties of a class member var.
*
* The format of the return value is:
* ```php
* array(
* 'scope' => string, // Public, private, or protected.
* 'scope_specified' => boolean, // TRUE if the scope was explicitly specified.
* 'is_static' => boolean, // TRUE if the static keyword was found.
* 'is_readonly' => boolean, // TRUE if the readonly keyword was found.
* 'type' => string, // The type of the var (empty if no type specified).
* 'type_token' => integer|false, // The stack pointer to the start of the type
* // or FALSE if there is no type.
* 'type_end_token' => integer|false, // The stack pointer to the end of the type
* // or FALSE if there is no type.
* 'nullable_type' => boolean, // TRUE if the type is preceded by the
* // nullability operator.
* );
* ```
*
* PHPCS cross-version compatible version of the `File::getMemberProperties() method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::getMemberProperties() Original source.
* @see \PHPCSUtils\Utils\Variables::getMemberProperties() PHPCSUtils native improved version.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack of the `T_VARIABLE` token to
* acquire the properties for.
*
* @return array
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
* `T_VARIABLE` token, or if the position is not
* a class member variable.
*/
public static function getMemberProperties(File $phpcsFile, $stackPtr)
{
return $phpcsFile->getMemberProperties($stackPtr);
}
/**
* Returns the implementation properties of a class.
*
* The format of the return value is:
* ```php
* array(
* 'is_abstract' => boolean, // TRUE if the abstract keyword was found.
* 'is_final' => boolean, // TRUE if the final keyword was found.
* 'is_readonly' => false, // TRUE if the readonly keyword was found.
* );
* ```
*
* PHPCS cross-version compatible version of the `File::getClassProperties()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 1.3.0.
* - PHPCS 3.8.0: Added support for PHP 8.2 `readonly` classes.
*
* @see \PHP_CodeSniffer\Files\File::getClassProperties() Original source.
* @see \PHPCSUtils\Utils\ObjectDeclarations::getClassProperties() PHPCSUtils native improved version.
*
* @since 1.0.0
* @since 1.0.6 Sync with PHPCS 3.8.0, support for readonly classes. PHPCS#3686.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack of the `T_CLASS`
* token to acquire the properties for.
*
* @return array
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
* `T_CLASS` token.
*/
public static function getClassProperties(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] !== T_CLASS) {
throw new RuntimeException('$stackPtr must be of type T_CLASS');
}
$valid = [
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
T_DOC_COMMENT => T_DOC_COMMENT,
];
$isAbstract = false;
$isFinal = false;
$isReadonly = false;
for ($i = ($stackPtr - 1); $i > 0; $i--) {
if (isset($valid[$tokens[$i]['code']]) === false) {
break;
}
switch ($tokens[$i]['code']) {
case T_ABSTRACT:
$isAbstract = true;
break;
case T_FINAL:
$isFinal = true;
break;
case T_READONLY:
$isReadonly = true;
break;
}
}
return [
'is_abstract' => $isAbstract,
'is_final' => $isFinal,
'is_readonly' => $isReadonly,
];
}
/**
* Determine if the passed token is a reference operator.
*
* PHPCS cross-version compatible version of the `File::isReference()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::isReference() Original source.
* @see \PHPCSUtils\Utils\Operators::isReference() PHPCSUtils native improved version.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the `T_BITWISE_AND` token.
*
* @return bool `TRUE` if the specified token position represents a reference.
* `FALSE` if the token represents a bitwise operator.
*/
public static function isReference(File $phpcsFile, $stackPtr)
{
return $phpcsFile->isReference($stackPtr);
}
/**
* Returns the content of the tokens from the specified start position in
* the token stack for the specified length.
*
* PHPCS cross-version compatible version of the `File::getTokensAsString()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::getTokensAsString() Original source.
* @see \PHPCSUtils\Utils\GetTokensAsString Related set of functions.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $start The position to start from in the token stack.
* @param int $length The length of tokens to traverse from the start pos.
* @param bool $origContent Whether the original content or the tab replaced
* content should be used.
*
* @return string The token contents.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified start position does not exist.
*/
public static function getTokensAsString(File $phpcsFile, $start, $length, $origContent = false)
{
return $phpcsFile->getTokensAsString($start, $length, $origContent);
}
/**
* Returns the position of the first non-whitespace token in a statement.
*
* PHPCS cross-version compatible version of the `File::findStartOfStatement()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 2.1.0.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::findStartOfStatement() Original source.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $start The position to start searching from in the token stack.
* @param int|string|array $ignore Token types that should not be considered stop points.
*
* @return int
*/
public static function findStartOfStatement(File $phpcsFile, $start, $ignore = null)
{
return $phpcsFile->findStartOfStatement($start, $ignore);
}
/**
* Returns the position of the last non-whitespace token in a statement.
*
* PHPCS cross-version compatible version of the `File::findEndOfStatement() method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 2.1.0.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::findEndOfStatement() Original source.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $start The position to start searching from in the token stack.
* @param int|string|array $ignore Token types that should not be considered stop points.
*
* @return int
*/
public static function findEndOfStatement(File $phpcsFile, $start, $ignore = null)
{
return $phpcsFile->findEndOfStatement($start, $ignore);
}
/**
* Determine if the passed token has a condition of one of the passed types.
*
* PHPCS cross-version compatible version of the `File::hasCondition()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::hasCondition() Original source.
* @see \PHPCSUtils\Utils\Conditions::hasCondition() PHPCSUtils native alternative.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the token we are checking.
* @param int|string|array $types The type(s) of tokens to search for.
*
* @return bool
*/
public static function hasCondition(File $phpcsFile, $stackPtr, $types)
{
return $phpcsFile->hasCondition($stackPtr, $types);
}
/**
* Return the position of the condition for the passed token.
*
* PHPCS cross-version compatible version of the `File::getCondition()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 1.3.0.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::getCondition() Original source.
* @see \PHPCSUtils\Utils\Conditions::getCondition() More versatile alternative.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the token we are checking.
* @param int|string $type The type of token to search for.
* @param bool $first If `true`, will return the matched condition
* furthest away from the passed token.
* If `false`, will return the matched condition
* closest to the passed token.
*
* @return int|false Integer stack pointer to the condition or `FALSE` if the token
* does not have the condition.
*/
public static function getCondition(File $phpcsFile, $stackPtr, $type, $first = true)
{
return $phpcsFile->getCondition($stackPtr, $type, $first);
}
/**
* Returns the name of the class that the specified class extends.
* (works for classes, anonymous classes and interfaces)
*
* PHPCS cross-version compatible version of the `File::findExtendedClassName()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 1.2.0.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::findExtendedClassName() Original source.
* @see \PHPCSUtils\Utils\ObjectDeclarations::findExtendedClassName() PHPCSUtils native improved version.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The stack position of the class or interface.
*
* @return string|false The extended class name or `FALSE` on error or if there
* is no extended class name.
*/
public static function findExtendedClassName(File $phpcsFile, $stackPtr)
{
return $phpcsFile->findExtendedClassName($stackPtr);
}
/**
* Returns the names of the interfaces that the specified class or enum implements.
*
* PHPCS cross-version compatible version of the `File::findImplementedInterfaceNames()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 2.7.0.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
*
* @see \PHP_CodeSniffer\Files\File::findImplementedInterfaceNames() Original source.
* @see \PHPCSUtils\Utils\ObjectDeclarations::findImplementedInterfaceNames() PHPCSUtils native improved version.
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The stack position of the class or enum token.
*
* @return array|false Array with names of the implemented interfaces or `FALSE` on
* error or if there are no implemented interface names.
*/
public static function findImplementedInterfaceNames(File $phpcsFile, $stackPtr)
{
return $phpcsFile->findImplementedInterfaceNames($stackPtr);
}
}