ExtendedEmulativeLexer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createDefaultInstance() 0 5 1
C getNextToken() 0 24 8
1
<?php
2
/**
3
 * ExtendedEmulativeLexer.php
4
 *
5
 * MIT LICENSE
6
 *
7
 * LICENSE: This source file is subject to the MIT license.
8
 * A copy of the licenses text was distributed alongside this
9
 * file (usually the repository or package root). The text can also
10
 * be obtained on one of the following sources:
11
 * * http://opensource.org/licenses/MIT
12
 * * https://github.com/suralc/pvra/blob/master/LICENSE
13
 *
14
 * @author     suralc <[email protected]>
15
 * @license    http://opensource.org/licenses/MIT  MIT
16
 */
17
namespace Pvra\Lexer;
18
19
20
use PhpParser\Lexer\Emulative;
21
use PhpParser\Parser\Tokens;
22
23
/**
24
 * Class ExtendedEmulativeLexer
25
 *
26
 * Extends the lexer to provide information about raw representations of numbers,
27
 * detection of here- and nowdoc syntaxes and short echo statements.
28
 *
29
 * @package Pvra\Lexer
30
 */
31
class ExtendedEmulativeLexer extends Emulative
32
{
33
    /**
34
     * Create a new Lexer instance with appropriate configuration options
35
     *
36
     * Whenever a new lexer instance is created that is meant to be passed to
37
     * a `Pvra\Analyser` or consumed by a `Pvra\AnalyserAwareInterface` this method should be used.
38
     *
39
     * @return ExtendedEmulativeLexer|static
40
     */
41 130
    public static function createDefaultInstance()
42
    {
43 130
        $class = get_called_class();
44 130
        return new $class(['usedAttributes' => ['startLine', 'endLine', 'startFilePos', 'startTokenPos']]);
45
    }
46
47
    /**
48
     * Override to the native getNextToken method
49
     *
50
     * This method override ensures that the original value of tokens that would be transformed is stored
51
     * besides them in the result ast. Depending on the token type various attributes will be added to the token
52
     * and produced ast. These modifications are required to ensure the correct behaviour of the binary number
53
     * detection, the detection of booth flavors of the doc syntax, short array syntax and short echo tags.
54
     *
55
     * @param null|string $value Value of the token
56
     * @param null|array $startAttributes
57
     * @param null|array $endAttributes
58
     * @return int Retrieved token id
59
     * @see https://github.com/nikic/PHP-Parser/issues/26#issuecomment-6150035 Original implementation
60
     */
61 128
    public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null)
62
    {
63 128
        $tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);
64
65 128
        if ($tokenId === Tokens::T_LNUMBER || $tokenId === Tokens::T_DNUMBER) {
66
            // could also use $startAttributes, doesn't really matter here
67 72
            $endAttributes['originalValue'] = $value;
68 128
        } elseif ($tokenId === Tokens::T_START_HEREDOC) {
69 10
            $startAttributes['isDocSyntax'] = true;
70 10
            if (substr($this->code, $startAttributes['startFilePos'] + 3, 1) === '\'') {
71 8
                $startAttributes['isNowDoc'] = true;
72 4
            } else {
73 10
                $startAttributes['isHereDoc'] = true;
74
            }
75 128
        } elseif ($tokenId === Tokens::T_ARRAY) {
76 26
            $startAttributes['traditionalArray'] = true;
77 128
        } elseif ($tokenId === Tokens::T_ECHO) {
78 26
            if (substr($this->code, $startAttributes['startFilePos'], 3) === '<?=') {
79 2
                $startAttributes['isShortEchoTag'] = true;
80 1
            }
81 13
        }
82
83 128
        return $tokenId;
84
    }
85
}
86