Completed
Pull Request — master (#44)
by Björn
02:58
created

AbstractFileDecorator::getTokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer;
6
7
use PHP_CodeSniffer\Files\File;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BestIt\CodeSniffer\File.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use function array_walk;
9
use function func_get_args;
10
use function get_object_vars;
11
12
/**
13
 * Copy of the file api to provide an file decorator.
14
 *
15
 * @author blange <[email protected]>
16
 * @package BestIt\CodeSniffer
17
 */
18
abstract class AbstractFileDecorator extends File
19
{
20
    /**
21
     * The CodeSniffer file
22
     *
23
     * @var File
24
     */
25
    private $baseFile;
26
27
    /**
28
     * File constructor.
29
     *
30
     * @param File $baseFile CodeSniffer file
31
     */
32
    public function __construct(File $baseFile)
33
    {
34
        $this->takeProperties($baseFile);
35
36
        $this->baseFile = $baseFile;
37
    }
38
39
    /**
40
     * Returns the wrapped classes method result.
41
     *
42
     * @param string $method
43
     * @param array $args
44
     *
45
     * @return mixed
46
     */
47
    public function __call(string $method, array $args = [])
48
    {
49
        return $this->getBaseFile()->{$method}(...$args);
50
    }
51
    
52
    /**
53
     * Records an error against a specific token in the file.
54
     *
55
     * @param string $error The error message.
56
     * @param int $stackPtr The stack position where the error occurred.
57
     * @param string $code A violation code unique to the sniff message.
58
     * @param array $data Replacements for the error message.
59
     * @param int $severity The severity level for this error. A value of 0
60
     *                          will be converted into the default severity level.
61
     * @param boolean $fixable Can the error be fixed by the sniff?
62
     *
63
     * @return boolean
64
     */
65
    public function addError(
66
        $error,
67
        $stackPtr,
68
        $code,
69
        $data = [],
70
        $severity = 0,
71
        $fixable = false
72
    ) {
73
        return $this->__call(__FUNCTION__, func_get_args());
74
    }
75
76
    /**
77
     * Records an error against a specific line in the file.
78
     *
79
     * @param string $error The error message.
80
     * @param int $line The line on which the error occurred.
81
     * @param string $code A violation code unique to the sniff message.
82
     * @param array $data Replacements for the error message.
83
     * @param int $severity The severity level for this error. A value of 0
84
     *                         will be converted into the default severity level.
85
     *
86
     * @return boolean
87
     */
88
    public function addErrorOnLine(
89
        $error,
90
        $line,
91
        $code,
92
        $data = [],
93
        $severity = 0
94
    ) {
95
        return $this->__call(__FUNCTION__, func_get_args());
96
    }
97
98
    /**
99
     * Records a fixable error against a specific token in the file.
100
     *
101
     * Returns true if the error was recorded and should be fixed.
102
     *
103
     * @param string $error The error message.
104
     * @param int $stackPtr The stack position where the error occurred.
105
     * @param string $code A violation code unique to the sniff message.
106
     * @param array $data Replacements for the error message.
107
     * @param int $severity The severity level for this error. A value of 0
108
     *                         will be converted into the default severity level.
109
     *
110
     * @return boolean
111
     */
112
    public function addFixableError(
113
        $error,
114
        $stackPtr,
115
        $code,
116
        $data = [],
117
        $severity = 0
118
    ) {
119
        return $this->__call(__FUNCTION__, func_get_args());
120
    }
121
122
    /**
123
     * Records a fixable warning against a specific token in the file.
124
     *
125
     * Returns true if the warning was recorded and should be fixed.
126
     *
127
     * @param string $warning The error message.
128
     * @param int $stackPtr The stack position where the error occurred.
129
     * @param string $code A violation code unique to the sniff message.
130
     * @param array $data Replacements for the warning message.
131
     * @param int $severity The severity level for this warning. A value of 0
132
     *                         will be converted into the default severity level.
133
     *
134
     * @return boolean
135
     */
136
    public function addFixableWarning(
137
        $warning,
138
        $stackPtr,
139
        $code,
140
        $data = [],
141
        $severity = 0
142
    ) {
143
        return $this->__call(__FUNCTION__, func_get_args());
144
    }
145
146
    /**
147
     * Adds an error to the error stack.
148
     *
149
     * @param boolean $error Is this an error message?
150
     * @param string $message The text of the message.
151
     * @param int $line The line on which the message occurred.
152
     * @param int $column The column at which the message occurred.
153
     * @param string $code A violation code unique to the sniff message.
154
     * @param array $data Replacements for the message.
155
     * @param int $severity The severity level for this message. A value of 0
156
     *                          will be converted into the default severity level.
157
     * @param boolean $fixable Can the problem be fixed by the sniff?
158
     *
159
     * @return boolean
160
     */
161
    protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable)
162
    {
163
        return $this->__call(__FUNCTION__, func_get_args());
164
    }
165
166
    /**
167
     * Records a warning against a specific token in the file.
168
     *
169
     * @param string $warning The error message.
170
     * @param int $stackPtr The stack position where the error occurred.
171
     * @param string $code A violation code unique to the sniff message.
172
     * @param array $data Replacements for the warning message.
173
     * @param int $severity The severity level for this warning. A value of 0
174
     *                          will be converted into the default severity level.
175
     * @param boolean $fixable Can the warning be fixed by the sniff?
176
     *
177
     * @return boolean
178
     */
179
    public function addWarning(
180
        $warning,
181
        $stackPtr,
182
        $code,
183
        $data = [],
184
        $severity = 0,
185
        $fixable = false
186
    ) {
187
        return $this->__call(__FUNCTION__, func_get_args());
188
    }
189
190
    /**
191
     * Records a warning against a specific token in the file.
192
     *
193
     * @param string $warning The error message.
194
     * @param int $line The line on which the warning occurred.
195
     * @param string $code A violation code unique to the sniff message.
196
     * @param array $data Replacements for the warning message.
197
     * @param int $severity The severity level for this warning. A value of 0 will
198
     *                         will be converted into the default severity level.
199
     *
200
     * @return boolean
201
     */
202
    public function addWarningOnLine(
203
        $warning,
204
        $line,
205
        $code,
206
        $data = [],
207
        $severity = 0
208
    ) {
209
        return $this->__call(__FUNCTION__, func_get_args());
210
    }
211
212
    /**
213
     * Remove vars stored in this file that are no longer required.
214
     *
215
     * @return void
216
     */
217
    public function cleanUp()
218
    {
219
        $this->__call(__FUNCTION__, func_get_args());
220
    }
221
222
    /**
223
     * Disables caching of this file.
224
     *
225
     * @return void
226
     */
227
    public function disableCaching()
228
    {
229
        $this->__call(__FUNCTION__, func_get_args());
230
    }
231
232
    /**
233
     * Returns the position of the last non-whitespace token in a statement.
234
     *
235
     * @param int $start The position to start searching from in the token stack.
236
     * @param int|array $ignore Token types that should not be considered stop points.
237
     *
238
     * @return int
239
     */
240
    public function findEndOfStatement($start, $ignore = null)
241
    {
242
        return $this->__call(__FUNCTION__, func_get_args());
243
    }
244
245
    /**
246
     * Returns the name of the class that the specified class extends.
247
     * (works for classes, anonymous classes and interfaces)
248
     *
249
     * Returns FALSE on error or if there is no extended class name.
250
     *
251
     * @param int $stackPtr The stack position of the class.
252
     *
253
     * @return string|false
254
     */
255
    public function findExtendedClassName($stackPtr)
256
    {
257
        return $this->__call(__FUNCTION__, func_get_args());
258
    }
259
260
    /**
261
     * Returns the position of the first token on a line, matching given type.
262
     *
263
     * Returns false if no token can be found.
264
     *
265
     * @param int|array $types The type(s) of tokens to search for.
266
     * @param int $start The position to start searching from in the
267
     *                           token stack. The first token matching on
268
     *                           this line before this token will be returned.
269
     * @param bool $exclude If true, find the token that is NOT of
270
     *                           the types specified in $types.
271
     * @param string $value The value that the token must be equal to.
272
     *                           If value is omitted, tokens with any value will
273
     *                           be returned.
274
     *
275
     * @return int | bool
276
     */
277
    public function findFirstOnLine($types, $start, $exclude = false, $value = null)
278
    {
279
        return $this->__call(__FUNCTION__, func_get_args());
280
    }
281
282
    /**
283
     * Returns the names of the interfaces that the specified class implements.
284
     *
285
     * Returns FALSE on error or if there are no implemented interface names.
286
     *
287
     * @param int $stackPtr The stack position of the class.
288
     *
289
     * @return array|false
290
     */
291
    public function findImplementedInterfaceNames($stackPtr)
292
    {
293
        return $this->__call(__FUNCTION__, func_get_args());
294
    }
295
296
    /**
297
     * Returns the position of the next specified token(s).
298
     *
299
     * If a value is specified, the next token of the specified type(s)
300
     * containing the specified value will be returned.
301
     *
302
     * Returns false if no token can be found.
303
     *
304
     * @param int|array $types The type(s) of tokens to search for.
305
     * @param int $start The position to start searching from in the
306
     *                           token stack.
307
     * @param int $end The end position to fail if no token is found.
308
     *                           if not specified or null, end will default to
309
     *                           the end of the token stack.
310
     * @param bool $exclude If true, find the next token that is NOT of
311
     *                           a type specified in $types.
312
     * @param string $value The value that the token(s) must be equal to.
313
     *                           If value is omitted, tokens with any value will
314
     *                           be returned.
315
     * @param bool $local If true, tokens outside the current statement
316
     *                           will not be checked. i.e., checking will stop
317
     *                           at the next semi-colon found.
318
     *
319
     * @return int|bool
320
     * @see    findPrevious()
321
     */
322
    public function findNext(
323
        $types,
324
        $start,
325
        $end = null,
326
        $exclude = false,
327
        $value = null,
328
        $local = false
329
    ) {
330
        return $this->__call(__FUNCTION__, func_get_args());
331
    }
332
333
    /**
334
     * Returns the position of the previous specified token(s).
335
     *
336
     * If a value is specified, the previous token of the specified type(s)
337
     * containing the specified value will be returned.
338
     *
339
     * Returns false if no token can be found.
340
     *
341
     * @param int|array $types The type(s) of tokens to search for.
342
     * @param int $start The position to start searching from in the
343
     *                           token stack.
344
     * @param int $end The end position to fail if no token is found.
345
     *                           if not specified or null, end will default to
346
     *                           the start of the token stack.
347
     * @param bool $exclude If true, find the previous token that is NOT of
348
     *                           the types specified in $types.
349
     * @param string $value The value that the token(s) must be equal to.
350
     *                           If value is omitted, tokens with any value will
351
     *                           be returned.
352
     * @param bool $local If true, tokens outside the current statement
353
     *                           will not be checked. IE. checking will stop
354
     *                           at the previous semi-colon found.
355
     *
356
     * @return int|bool
357
     * @see    findNext()
358
     */
359
    public function findPrevious(
360
        $types,
361
        $start,
362
        $end = null,
363
        $exclude = false,
364
        $value = null,
365
        $local = false
366
    ) {
367
        return $this->__call(__FUNCTION__, func_get_args());
368
    }
369
370
    /**
371
     * Returns the position of the first non-whitespace token in a statement.
372
     *
373
     * @param int $start The position to start searching from in the token stack.
374
     * @param int|array $ignore Token types that should not be considered stop points.
375
     *
376
     * @return int
377
     */
378
    public function findStartOfStatement($start, $ignore = null)
379
    {
380
        return $this->__call(__FUNCTION__, func_get_args());
381
    }
382
383
    /**
384
     * Returns the original file for this decorator
385
     *
386
     * @return File The original file wrapper.
387
     */
388
    public function getBaseFile(): File
389
    {
390
        return $this->baseFile;
391
    }
392
393
    /**
394
     * Returns the visibility and implementation properties of a class.
395
     *
396
     * The format of the array is:
397
     * <code>
398
     *   array(
399
     *    'is_abstract' => false, // true if the abstract keyword was found.
400
     *    'is_final'    => false, // true if the final keyword was found.
401
     *   );
402
     * </code>
403
     *
404
     * @param int $stackPtr The position in the stack of the T_CLASS token to
405
     *                      acquire the properties for.
406
     *
407
     * @return array
408
     * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
409
     *                                                        T_CLASS token.
410
     */
411
    public function getClassProperties($stackPtr)
412
    {
413
        return $this->__call(__FUNCTION__, func_get_args());
414
    }
415
416
    /**
417
     * Return the position of the condition for the passed token.
418
     *
419
     * Returns FALSE if the token does not have the condition.
420
     *
421
     * @param int $stackPtr The position of the token we are checking.
422
     * @param int $type The type of token to search for.
423
     *
424
     * @return int
425
     */
426
    public function getCondition($stackPtr, $type)
427
    {
428
        return $this->__call(__FUNCTION__, func_get_args());
429
    }
430
431
    /**
432
     * Returns the declaration names for classes, interfaces, traits, and functions.
433
     *
434
     * @param int $stackPtr The position of the declaration token which
435
     *                      declared the class, interface, trait, or function.
436
     *
437
     * @return string|null The name of the class, interface, trait, or function;
438
     *                     or NULL if the function or class is anonymous.
439
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type
440
     *                                                      T_FUNCTION, T_CLASS, T_ANON_CLASS,
441
     *                                                      T_CLOSURE, T_TRAIT, or T_INTERFACE.
442
     */
443
    public function getDeclarationName($stackPtr)
444
    {
445
        return $this->__call(__FUNCTION__, func_get_args());
446
    }
447
448
    /**
449
     * Returns the number of errors raised.
450
     *
451
     * @return int
452
     */
453
    public function getErrorCount()
454
    {
455
        return $this->__call(__FUNCTION__, func_get_args());
456
    }
457
458
    /**
459
     * Returns the errors raised from processing this file.
460
     *
461
     * @return array
462
     */
463
    public function getErrors()
464
    {
465
        return $this->__call(__FUNCTION__, func_get_args());
466
    }
467
468
    /**
469
     * Returns the absolute filename of this file.
470
     *
471
     * @return string
472
     */
473
    public function getFilename()
474
    {
475
        return $this->__call(__FUNCTION__, func_get_args());
476
    }
477
478
    /**
479
     * Returns the number of fixable errors/warnings raised.
480
     *
481
     * @return int
482
     */
483
    public function getFixableCount()
484
    {
485
        return $this->__call(__FUNCTION__, func_get_args());
486
    }
487
488
    /**
489
     * Returns the number of fixed errors/warnings.
490
     *
491
     * @return int
492
     */
493
    public function getFixedCount()
494
    {
495
        return $this->__call(__FUNCTION__, func_get_args());
496
    }
497
498
    /**
499
     * Returns the list of ignored lines.
500
     *
501
     * @return array
502
     */
503
    public function getIgnoredLines()
504
    {
505
        return $this->__call(__FUNCTION__, func_get_args());
506
    }
507
508
    /**
509
     * Returns the visibility and implementation properties of the class member
510
     * variable found at the specified position in the stack.
511
     *
512
     * The format of the array is:
513
     *
514
     * <code>
515
     *   array(
516
     *    'scope'           => 'public', // public protected or protected.
517
     *    'scope_specified' => false,    // true if the scope was explicitely specified.
518
     *    'is_static'       => false,    // true if the static keyword was found.
519
     *   );
520
     * </code>
521
     *
522
     * @param int $stackPtr The position in the stack of the T_VARIABLE token to
523
     *                      acquire the properties for.
524
     *
525
     * @return array
526
     * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
527
     *                                                        T_VARIABLE token, or if the position is not
528
     *                                                        a class member variable.
529
     */
530
    public function getMemberProperties($stackPtr)
531
    {
532
        return $this->__call(__FUNCTION__, func_get_args());
533
    }
534
535
    /**
536
     * Returns the method parameters for the specified function token.
537
     *
538
     * Each parameter is in the following format:
539
     *
540
     * <code>
541
     *   0 => array(
542
     *         'name'              => '$var',  // The variable name.
543
     *         'token'             => integer, // The stack pointer to the variable name.
544
     *         'content'           => string,  // The full content of the variable definition.
545
     *         'pass_by_reference' => boolean, // Is the variable passed by reference?
546
     *         'variable_length'   => boolean, // Is the param of variable length through use of `...` ?
547
     *         'type_hint'         => string,  // The type hint for the variable.
548
     *         'type_hint_token'   => integer, // The stack pointer to the type hint
549
     *                                         // or false if there is no type hint.
550
     *         'nullable_type'     => boolean, // Is the variable using a nullable type?
551
     *        )
552
     * </code>
553
     *
554
     * Parameters with default values have an additional array index of
555
     * 'default' with the value of the default as a string.
556
     *
557
     * @param int $stackPtr The position in the stack of the function token
558
     *                      to acquire the parameters for.
559
     *
560
     * @return array
561
     * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of
562
     *                                                        type T_FUNCTION or T_CLOSURE.
563
     */
564
    public function getMethodParameters($stackPtr)
565
    {
566
        return $this->__call(__FUNCTION__, func_get_args());
567
    }
568
569
570
    /**
571
     * Returns the visibility and implementation properties of a method.
572
     *
573
     * The format of the array is:
574
     * <code>
575
     *   array(
576
     *    'scope'                => 'public', // public protected or protected
577
     *    'scope_specified'      => true,     // true is scope keyword was found.
578
     *    'return_type'          => '',       // the return type of the method.
579
     *    'return_type_token'    => integer,  // The stack pointer to the start of the return type
580
     *                                        // or false if there is no return type.
581
     *    'nullable_return_type' => false,    // true if the return type is nullable.
582
     *    'is_abstract'          => false,    // true if the abstract keyword was found.
583
     *    'is_final'             => false,    // true if the final keyword was found.
584
     *    'is_static'            => false,    // true if the static keyword was found.
585
     *   );
586
     * </code>
587
     *
588
     * @param int $stackPtr The position in the stack of the function token to
589
     *                      acquire the properties for.
590
     *
591
     * @return array
592
     * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
593
     *                                                        T_FUNCTION token.
594
     */
595
    public function getMethodProperties($stackPtr)
596
    {
597
        return $this->__call(__FUNCTION__, func_get_args());
598
    }
599
600
    /**
601
     * Returns the metrics found while processing this file.
602
     *
603
     * @return array
604
     */
605
    public function getMetrics()
606
    {
607
        return $this->__call(__FUNCTION__, func_get_args());
608
    }
609
610
    /**
611
     * Returns the number of successes recorded.
612
     *
613
     * @return int
614
     */
615
    public function getSuccessCount()
616
    {
617
        return $this->__call(__FUNCTION__, func_get_args());
618
    }
619
620
    /**
621
     * Returns the token stack for this file.
622
     *
623
     * @return array
624
     */
625
    public function getTokens()
626
    {
627
        return $this->__call(__FUNCTION__, func_get_args());
628
    }
629
630
    /**
631
     * Returns the content of the tokens from the specified start position in
632
     * the token stack for the specified length.
633
     *
634
     * @param int $start The position to start from in the token stack.
635
     * @param int $length The length of tokens to traverse from the start pos.
636
     * @param int $origContent Whether the original content or the tab replaced
637
     *                         content should be used.
638
     *
639
     * @return string The token contents.
640
     */
641
    public function getTokensAsString($start, $length, $origContent = false)
642
    {
643
        return $this->__call(__FUNCTION__, func_get_args());
644
    }
645
646
    /**
647
     * Returns the number of warnings raised.
648
     *
649
     * @return int
650
     */
651
    public function getWarningCount()
652
    {
653
        return $this->__call(__FUNCTION__, func_get_args());
654
    }
655
656
    /**
657
     * Returns the warnings raised from processing this file.
658
     *
659
     * @return array
660
     */
661
    public function getWarnings()
662
    {
663
        return $this->__call(__FUNCTION__, func_get_args());
664
    }
665
666
    /**
667
     * Determine if the passed token has a condition of one of the passed types.
668
     *
669
     * @param int $stackPtr The position of the token we are checking.
670
     * @param int|array $types The type(s) of tokens to search for.
671
     *
672
     * @return boolean
673
     */
674
    public function hasCondition($stackPtr, $types)
675
    {
676
        return $this->__call(__FUNCTION__, func_get_args());
677
    }
678
679
    /**
680
     * Determine if the passed token is a reference operator.
681
     *
682
     * Returns true if the specified token position represents a reference.
683
     * Returns false if the token represents a bitwise operator.
684
     *
685
     * @param int $stackPtr The position of the T_BITWISE_AND token.
686
     *
687
     * @return boolean
688
     */
689
    public function isReference($stackPtr)
690
    {
691
        return $this->__call(__FUNCTION__, func_get_args());
692
    }
693
694
    /**
695
     * Tokenizes the file and prepares it for the test run.
696
     *
697
     * @return void
698
     */
699
    public function parse()
700
    {
701
        $this->__call(__FUNCTION__, func_get_args());
702
    }
703
704
    /**
705
     * Starts the stack traversal and tells listeners when tokens are found.
706
     *
707
     * @return void
708
     */
709
    public function process()
710
    {
711
        $this->__call(__FUNCTION__, func_get_args());
712
    }
713
714
    /**
715
     * Record a metric about the file being examined.
716
     *
717
     * @param int $stackPtr The stack position where the metric was recorded.
718
     * @param string $metric The name of the metric being recorded.
719
     * @param string $value The value of the metric being recorded.
720
     *
721
     * @return boolean
722
     */
723
    public function recordMetric($stackPtr, $metric, $value)
724
    {
725
        return $this->__call(__FUNCTION__, func_get_args());
726
    }
727
728
    /**
729
     * Reloads the content of the file.
730
     *
731
     * By default, we have no idea where our content comes from, so we can't do anything.
732
     *
733
     * @return void
734
     */
735
    public function reloadContent()
736
    {
737
        $this->__call(__FUNCTION__, func_get_args());
738
    }
739
740
    /**
741
     * Set the content of the file.
742
     *
743
     * Setting the content also calculates the EOL char being used.
744
     *
745
     * @param string $content The file content.
746
     *
747
     * @return void
748
     */
749
    public function setContent($content)
750
    {
751
        $this->__call(__FUNCTION__, func_get_args());
752
    }
753
754
    /**
755
     * We need to clone the properties of the base file to this. A magic getter on inherited props does not work.
756
     *
757
     * @param File $baseFile
758
     *
759
     * @return void
760
     */
761
    private function takeProperties(File $baseFile): void
762
    {
763
        $baseProps = get_object_vars($baseFile);
764
765
        array_walk($baseProps, function ($value, $key) {
766
            $this->$key = $value;
767
        });
768
    }
769
}