Blame view

vendor/phpunit/php-token-stream/src/Token/Stream.php 16.5 KB
c4650843   Etienne Pallier   Ajout du dossier ...
1
2
<?php
/*
d06254b2   Etienne Pallier   bugfix plugin mig...
3
 * This file is part of the PHP_TokenStream package.
c4650843   Etienne Pallier   Ajout du dossier ...
4
5
6
7
8
9
10
11
12
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * A stream of PHP tokens.
d06254b2   Etienne Pallier   bugfix plugin mig...
13
14
15
16
17
18
 *
 * @author    Sebastian Bergmann <sebastian@phpunit.de>
 * @copyright Sebastian Bergmann <sebastian@phpunit.de>
 * @license   http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 * @link      http://github.com/sebastianbergmann/php-token-stream/tree
 * @since     Class available since Release 1.0.0
c4650843   Etienne Pallier   Ajout du dossier ...
19
20
21
22
23
24
 */
class PHP_Token_Stream implements ArrayAccess, Countable, SeekableIterator
{
    /**
     * @var array
     */
d06254b2   Etienne Pallier   bugfix plugin mig...
25
    protected static $customTokens = array(
c4650843   Etienne Pallier   Ajout du dossier ...
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
        '(' => 'PHP_Token_OPEN_BRACKET',
        ')' => 'PHP_Token_CLOSE_BRACKET',
        '[' => 'PHP_Token_OPEN_SQUARE',
        ']' => 'PHP_Token_CLOSE_SQUARE',
        '{' => 'PHP_Token_OPEN_CURLY',
        '}' => 'PHP_Token_CLOSE_CURLY',
        ';' => 'PHP_Token_SEMICOLON',
        '.' => 'PHP_Token_DOT',
        ',' => 'PHP_Token_COMMA',
        '=' => 'PHP_Token_EQUAL',
        '<' => 'PHP_Token_LT',
        '>' => 'PHP_Token_GT',
        '+' => 'PHP_Token_PLUS',
        '-' => 'PHP_Token_MINUS',
        '*' => 'PHP_Token_MULT',
        '/' => 'PHP_Token_DIV',
        '?' => 'PHP_Token_QUESTION_MARK',
        '!' => 'PHP_Token_EXCLAMATION_MARK',
        ':' => 'PHP_Token_COLON',
        '"' => 'PHP_Token_DOUBLE_QUOTES',
        '@' => 'PHP_Token_AT',
        '&' => 'PHP_Token_AMPERSAND',
        '%' => 'PHP_Token_PERCENT',
        '|' => 'PHP_Token_PIPE',
        '$' => 'PHP_Token_DOLLAR',
        '^' => 'PHP_Token_CARET',
        '~' => 'PHP_Token_TILDE',
        '`' => 'PHP_Token_BACKTICK'
d06254b2   Etienne Pallier   bugfix plugin mig...
54
    );
c4650843   Etienne Pallier   Ajout du dossier ...
55
56
57
58
59
60
61
62
63

    /**
     * @var string
     */
    protected $filename;

    /**
     * @var array
     */
d06254b2   Etienne Pallier   bugfix plugin mig...
64
    protected $tokens = array();
c4650843   Etienne Pallier   Ajout du dossier ...
65
66

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
67
     * @var integer
c4650843   Etienne Pallier   Ajout du dossier ...
68
69
70
71
72
73
     */
    protected $position = 0;

    /**
     * @var array
     */
d06254b2   Etienne Pallier   bugfix plugin mig...
74
    protected $linesOfCode = array('loc' => 0, 'cloc' => 0, 'ncloc' => 0);
c4650843   Etienne Pallier   Ajout du dossier ...
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

    /**
     * @var array
     */
    protected $classes;

    /**
     * @var array
     */
    protected $functions;

    /**
     * @var array
     */
    protected $includes;

    /**
     * @var array
     */
    protected $interfaces;

    /**
     * @var array
     */
    protected $traits;

    /**
     * @var array
     */
d06254b2   Etienne Pallier   bugfix plugin mig...
104
    protected $lineToFunctionMap = array();
c4650843   Etienne Pallier   Ajout du dossier ...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

    /**
     * Constructor.
     *
     * @param string $sourceCode
     */
    public function __construct($sourceCode)
    {
        if (is_file($sourceCode)) {
            $this->filename = $sourceCode;
            $sourceCode     = file_get_contents($sourceCode);
        }

        $this->scan($sourceCode);
    }

    /**
     * Destructor.
     */
    public function __destruct()
    {
d06254b2   Etienne Pallier   bugfix plugin mig...
126
        $this->tokens = array();
c4650843   Etienne Pallier   Ajout du dossier ...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    }

    /**
     * @return string
     */
    public function __toString()
    {
        $buffer = '';

        foreach ($this as $token) {
            $buffer .= $token;
        }

        return $buffer;
    }

    /**
     * @return string
d06254b2   Etienne Pallier   bugfix plugin mig...
145
     * @since  Method available since Release 1.1.0
c4650843   Etienne Pallier   Ajout du dossier ...
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
     */
    public function getFilename()
    {
        return $this->filename;
    }

    /**
     * Scans the source for sequences of characters and converts them into a
     * stream of tokens.
     *
     * @param string $sourceCode
     */
    protected function scan($sourceCode)
    {
        $id        = 0;
        $line      = 1;
        $tokens    = token_get_all($sourceCode);
        $numTokens = count($tokens);

        $lastNonWhitespaceTokenWasDoubleColon = false;

        for ($i = 0; $i < $numTokens; ++$i) {
            $token = $tokens[$i];
            $skip  = 0;

            if (is_array($token)) {
                $name = substr(token_name($token[0]), 2);
                $text = $token[1];

                if ($lastNonWhitespaceTokenWasDoubleColon && $name == 'CLASS') {
                    $name = 'CLASS_NAME_CONSTANT';
d06254b2   Etienne Pallier   bugfix plugin mig...
177
                } elseif ($name == 'USE' && isset($tokens[$i+2][0]) && $tokens[$i+2][0] == T_FUNCTION) {
c4650843   Etienne Pallier   Ajout du dossier ...
178
                    $name = 'USE_FUNCTION';
d06254b2   Etienne Pallier   bugfix plugin mig...
179
                    $text .= $tokens[$i+1][1] . $tokens[$i+2][1];
c4650843   Etienne Pallier   Ajout du dossier ...
180
181
182
183
184
185
186
187
188
189
190
                    $skip = 2;
                }

                $tokenClass = 'PHP_Token_' . $name;
            } else {
                $text       = $token;
                $tokenClass = self::$customTokens[$token];
            }

            $this->tokens[] = new $tokenClass($text, $line, $this, $id++);
            $lines          = substr_count($text, "\n");
d06254b2   Etienne Pallier   bugfix plugin mig...
191
            $line          += $lines;
c4650843   Etienne Pallier   Ajout du dossier ...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

            if ($tokenClass == 'PHP_Token_HALT_COMPILER') {
                break;
            } elseif ($tokenClass == 'PHP_Token_COMMENT' ||
                $tokenClass == 'PHP_Token_DOC_COMMENT') {
                $this->linesOfCode['cloc'] += $lines + 1;
            }

            if ($name == 'DOUBLE_COLON') {
                $lastNonWhitespaceTokenWasDoubleColon = true;
            } elseif ($name != 'WHITESPACE') {
                $lastNonWhitespaceTokenWasDoubleColon = false;
            }

            $i += $skip;
        }

        $this->linesOfCode['loc']   = substr_count($sourceCode, "\n");
        $this->linesOfCode['ncloc'] = $this->linesOfCode['loc'] -
                                      $this->linesOfCode['cloc'];
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
215
     * @return integer
c4650843   Etienne Pallier   Ajout du dossier ...
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
     */
    public function count()
    {
        return count($this->tokens);
    }

    /**
     * @return PHP_Token[]
     */
    public function tokens()
    {
        return $this->tokens;
    }

    /**
     * @return array
     */
    public function getClasses()
    {
        if ($this->classes !== null) {
            return $this->classes;
        }

        $this->parse();

        return $this->classes;
    }

    /**
     * @return array
     */
    public function getFunctions()
    {
        if ($this->functions !== null) {
            return $this->functions;
        }

        $this->parse();

        return $this->functions;
    }

    /**
     * @return array
     */
    public function getInterfaces()
    {
        if ($this->interfaces !== null) {
            return $this->interfaces;
        }

        $this->parse();

        return $this->interfaces;
    }

    /**
     * @return array
d06254b2   Etienne Pallier   bugfix plugin mig...
274
     * @since  Method available since Release 1.1.0
c4650843   Etienne Pallier   Ajout du dossier ...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
     */
    public function getTraits()
    {
        if ($this->traits !== null) {
            return $this->traits;
        }

        $this->parse();

        return $this->traits;
    }

    /**
     * Gets the names of all files that have been included
     * using include(), include_once(), require() or require_once().
     *
     * Parameter $categorize set to TRUE causing this function to return a
     * multi-dimensional array with categories in the keys of the first dimension
     * and constants and their values in the second dimension.
     *
     * Parameter $category allow to filter following specific inclusion type
     *
     * @param bool   $categorize OPTIONAL
     * @param string $category   OPTIONAL Either 'require_once', 'require',
d06254b2   Etienne Pallier   bugfix plugin mig...
299
     *                                           'include_once', 'include'.
c4650843   Etienne Pallier   Ajout du dossier ...
300
     * @return array
d06254b2   Etienne Pallier   bugfix plugin mig...
301
     * @since  Method available since Release 1.1.0
c4650843   Etienne Pallier   Ajout du dossier ...
302
303
304
305
     */
    public function getIncludes($categorize = false, $category = null)
    {
        if ($this->includes === null) {
d06254b2   Etienne Pallier   bugfix plugin mig...
306
307
308
309
310
311
            $this->includes = array(
              'require_once' => array(),
              'require'      => array(),
              'include_once' => array(),
              'include'      => array()
            );
c4650843   Etienne Pallier   Ajout du dossier ...
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

            foreach ($this->tokens as $token) {
                switch (get_class($token)) {
                    case 'PHP_Token_REQUIRE_ONCE':
                    case 'PHP_Token_REQUIRE':
                    case 'PHP_Token_INCLUDE_ONCE':
                    case 'PHP_Token_INCLUDE':
                        $this->includes[$token->getType()][] = $token->getName();
                        break;
                }
            }
        }

        if (isset($this->includes[$category])) {
            $includes = $this->includes[$category];
        } elseif ($categorize === false) {
            $includes = array_merge(
                $this->includes['require_once'],
                $this->includes['require'],
                $this->includes['include_once'],
                $this->includes['include']
            );
        } else {
            $includes = $this->includes;
        }

        return $includes;
    }

    /**
     * Returns the name of the function or method a line belongs to.
     *
     * @return string or null if the line is not in a function or method
d06254b2   Etienne Pallier   bugfix plugin mig...
345
     * @since  Method available since Release 1.2.0
c4650843   Etienne Pallier   Ajout du dossier ...
346
347
348
349
350
351
352
353
354
355
356
357
     */
    public function getFunctionForLine($line)
    {
        $this->parse();

        if (isset($this->lineToFunctionMap[$line])) {
            return $this->lineToFunctionMap[$line];
        }
    }

    protected function parse()
    {
d06254b2   Etienne Pallier   bugfix plugin mig...
358
359
360
361
362
363
        $this->interfaces = array();
        $this->classes    = array();
        $this->traits     = array();
        $this->functions  = array();
        $class            = array();
        $classEndLine     = array();
c4650843   Etienne Pallier   Ajout du dossier ...
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        $trait            = false;
        $traitEndLine     = false;
        $interface        = false;
        $interfaceEndLine = false;

        foreach ($this->tokens as $token) {
            switch (get_class($token)) {
                case 'PHP_Token_HALT_COMPILER':
                    return;

                case 'PHP_Token_INTERFACE':
                    $interface        = $token->getName();
                    $interfaceEndLine = $token->getEndLine();

d06254b2   Etienne Pallier   bugfix plugin mig...
378
379
                    $this->interfaces[$interface] = array(
                      'methods'   => array(),
c4650843   Etienne Pallier   Ajout du dossier ...
380
381
382
383
384
385
386
                      'parent'    => $token->getParent(),
                      'keywords'  => $token->getKeywords(),
                      'docblock'  => $token->getDocblock(),
                      'startLine' => $token->getLine(),
                      'endLine'   => $interfaceEndLine,
                      'package'   => $token->getPackage(),
                      'file'      => $this->filename
d06254b2   Etienne Pallier   bugfix plugin mig...
387
                    );
c4650843   Etienne Pallier   Ajout du dossier ...
388
389
390
391
                    break;

                case 'PHP_Token_CLASS':
                case 'PHP_Token_TRAIT':
d06254b2   Etienne Pallier   bugfix plugin mig...
392
393
                    $tmp = array(
                      'methods'   => array(),
c4650843   Etienne Pallier   Ajout du dossier ...
394
395
396
397
398
399
400
401
                      'parent'    => $token->getParent(),
                      'interfaces'=> $token->getInterfaces(),
                      'keywords'  => $token->getKeywords(),
                      'docblock'  => $token->getDocblock(),
                      'startLine' => $token->getLine(),
                      'endLine'   => $token->getEndLine(),
                      'package'   => $token->getPackage(),
                      'file'      => $this->filename
d06254b2   Etienne Pallier   bugfix plugin mig...
402
                    );
c4650843   Etienne Pallier   Ajout du dossier ...
403
404
405
406
407

                    if ($token instanceof PHP_Token_CLASS) {
                        $class[]        = $token->getName();
                        $classEndLine[] = $token->getEndLine();

d06254b2   Etienne Pallier   bugfix plugin mig...
408
409
410
                        if ($class[count($class)-1] != 'anonymous class') {
                            $this->classes[$class[count($class)-1]] = $tmp;
                        }
c4650843   Etienne Pallier   Ajout du dossier ...
411
412
413
414
415
416
417
418
419
                    } else {
                        $trait                = $token->getName();
                        $traitEndLine         = $token->getEndLine();
                        $this->traits[$trait] = $tmp;
                    }
                    break;

                case 'PHP_Token_FUNCTION':
                    $name = $token->getName();
d06254b2   Etienne Pallier   bugfix plugin mig...
420
                    $tmp  = array(
c4650843   Etienne Pallier   Ajout du dossier ...
421
422
423
424
425
426
427
428
                      'docblock'  => $token->getDocblock(),
                      'keywords'  => $token->getKeywords(),
                      'visibility'=> $token->getVisibility(),
                      'signature' => $token->getSignature(),
                      'startLine' => $token->getLine(),
                      'endLine'   => $token->getEndLine(),
                      'ccn'       => $token->getCCN(),
                      'file'      => $this->filename
d06254b2   Etienne Pallier   bugfix plugin mig...
429
                    );
c4650843   Etienne Pallier   Ajout du dossier ...
430
431
432
433
434
435
436
437
438
439
440

                    if (empty($class) &&
                        $trait === false &&
                        $interface === false) {
                        $this->functions[$name] = $tmp;

                        $this->addFunctionToMap(
                            $name,
                            $tmp['startLine'],
                            $tmp['endLine']
                        );
d06254b2   Etienne Pallier   bugfix plugin mig...
441
442
                    } elseif (!empty($class) && $class[count($class)-1] != 'anonymous class') {
                        $this->classes[$class[count($class)-1]]['methods'][$name] = $tmp;
c4650843   Etienne Pallier   Ajout du dossier ...
443
444

                        $this->addFunctionToMap(
d06254b2   Etienne Pallier   bugfix plugin mig...
445
                            $class[count($class)-1] . '::' . $name,
c4650843   Etienne Pallier   Ajout du dossier ...
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
                            $tmp['startLine'],
                            $tmp['endLine']
                        );
                    } elseif ($trait !== false) {
                        $this->traits[$trait]['methods'][$name] = $tmp;

                        $this->addFunctionToMap(
                            $trait . '::' . $name,
                            $tmp['startLine'],
                            $tmp['endLine']
                        );
                    } else {
                        $this->interfaces[$interface]['methods'][$name] = $tmp;
                    }
                    break;

                case 'PHP_Token_CLOSE_CURLY':
                    if (!empty($classEndLine) &&
d06254b2   Etienne Pallier   bugfix plugin mig...
464
                        $classEndLine[count($classEndLine)-1] == $token->getLine()) {
c4650843   Etienne Pallier   Ajout du dossier ...
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
                        array_pop($classEndLine);
                        array_pop($class);
                    } elseif ($traitEndLine !== false &&
                        $traitEndLine == $token->getLine()) {
                        $trait        = false;
                        $traitEndLine = false;
                    } elseif ($interfaceEndLine !== false &&
                        $interfaceEndLine == $token->getLine()) {
                        $interface        = false;
                        $interfaceEndLine = false;
                    }
                    break;
            }
        }
    }

    /**
     * @return array
     */
    public function getLinesOfCode()
    {
        return $this->linesOfCode;
    }

    /**
     */
    public function rewind()
    {
        $this->position = 0;
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
497
     * @return boolean
c4650843   Etienne Pallier   Ajout du dossier ...
498
499
500
501
502
503
504
     */
    public function valid()
    {
        return isset($this->tokens[$this->position]);
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
505
     * @return integer
c4650843   Etienne Pallier   Ajout du dossier ...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
     */
    public function key()
    {
        return $this->position;
    }

    /**
     * @return PHP_Token
     */
    public function current()
    {
        return $this->tokens[$this->position];
    }

    /**
     */
    public function next()
    {
        $this->position++;
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
528
529
     * @param  integer $offset
     * @return boolean
c4650843   Etienne Pallier   Ajout du dossier ...
530
531
532
533
534
535
536
     */
    public function offsetExists($offset)
    {
        return isset($this->tokens[$offset]);
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
537
     * @param  integer $offset
c4650843   Etienne Pallier   Ajout du dossier ...
538
     * @return mixed
c4650843   Etienne Pallier   Ajout du dossier ...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
     * @throws OutOfBoundsException
     */
    public function offsetGet($offset)
    {
        if (!$this->offsetExists($offset)) {
            throw new OutOfBoundsException(
                sprintf(
                    'No token at position "%s"',
                    $offset
                )
            );
        }

        return $this->tokens[$offset];
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
556
557
     * @param integer $offset
     * @param mixed   $value
c4650843   Etienne Pallier   Ajout du dossier ...
558
559
560
561
562
563
564
     */
    public function offsetSet($offset, $value)
    {
        $this->tokens[$offset] = $value;
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
565
     * @param  integer $offset
c4650843   Etienne Pallier   Ajout du dossier ...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
     * @throws OutOfBoundsException
     */
    public function offsetUnset($offset)
    {
        if (!$this->offsetExists($offset)) {
            throw new OutOfBoundsException(
                sprintf(
                    'No token at position "%s"',
                    $offset
                )
            );
        }

        unset($this->tokens[$offset]);
    }

    /**
     * Seek to an absolute position.
     *
d06254b2   Etienne Pallier   bugfix plugin mig...
585
     * @param  integer $position
c4650843   Etienne Pallier   Ajout du dossier ...
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
     * @throws OutOfBoundsException
     */
    public function seek($position)
    {
        $this->position = $position;

        if (!$this->valid()) {
            throw new OutOfBoundsException(
                sprintf(
                    'No token at position "%s"',
                    $this->position
                )
            );
        }
    }

    /**
d06254b2   Etienne Pallier   bugfix plugin mig...
603
604
605
     * @param string  $name
     * @param integer $startLine
     * @param integer $endLine
c4650843   Etienne Pallier   Ajout du dossier ...
606
607
608
609
610
611
612
613
     */
    private function addFunctionToMap($name, $startLine, $endLine)
    {
        for ($line = $startLine; $line <= $endLine; $line++) {
            $this->lineToFunctionMap[$line] = $name;
        }
    }
}