Passed
Push — type ( 27fad3...51f4ae )
by Michael
03:43
created

DocLexer::nextTokenIsAdjacent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Doctrine\Annotations;
4
5
use Doctrine\Common\Lexer\AbstractLexer;
6
7
/**
8
 * Simple lexer for docblock annotations.
9
 *
10
 * @author Benjamin Eberlei <[email protected]>
11
 * @author Guilherme Blanco <[email protected]>
12
 * @author Jonathan Wage <[email protected]>
13
 * @author Roman Borschel <[email protected]>
14
 * @author Johannes M. Schmitt <[email protected]>
15
 */
16
final class DocLexer extends AbstractLexer
17
{
18
    const T_NONE                = 1;
19
    const T_INTEGER             = 2;
20
    const T_STRING              = 3;
21
    const T_FLOAT               = 4;
22
23
    // All tokens that are also identifiers should be >= 100
24
    const T_IDENTIFIER          = 100;
25
    const T_AT                  = 101;
26
    const T_CLOSE_CURLY_BRACES  = 102;
27
    const T_CLOSE_PARENTHESIS   = 103;
28
    const T_COMMA               = 104;
29
    const T_EQUALS              = 105;
30
    const T_FALSE               = 106;
31
    const T_NAMESPACE_SEPARATOR = 107;
32
    const T_OPEN_CURLY_BRACES   = 108;
33
    const T_OPEN_PARENTHESIS    = 109;
34
    const T_TRUE                = 110;
35
    const T_NULL                = 111;
36
    const T_COLON               = 112;
37
    const T_MINUS               = 113;
38
39
    /**
40
     * @var array
41
     */
42
    protected $noCase = [
43
        '@'  => self::T_AT,
44
        ','  => self::T_COMMA,
45
        '('  => self::T_OPEN_PARENTHESIS,
46
        ')'  => self::T_CLOSE_PARENTHESIS,
47
        '{'  => self::T_OPEN_CURLY_BRACES,
48
        '}'  => self::T_CLOSE_CURLY_BRACES,
49
        '='  => self::T_EQUALS,
50
        ':'  => self::T_COLON,
51
        '-'  => self::T_MINUS,
52
        '\\' => self::T_NAMESPACE_SEPARATOR
53
    ];
54
55
    /**
56
     * @var array
57
     */
58
    protected $withCase = [
59
        'true'  => self::T_TRUE,
60
        'false' => self::T_FALSE,
61
        'null'  => self::T_NULL
62
    ];
63
64
    /**
65
     * Whether the next token starts immediately, or if there were
66
     * non-captured symbols before that
67
     */
68 5
    public function nextTokenIsAdjacent() : bool
69
    {
70 5
        return $this->token === null
71 5
            || ($this->lookahead !== null
72 5
                && ($this->lookahead['position'] - $this->token['position']) === strlen($this->token['value']));
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 4
    protected function getCatchablePatterns()
79
    {
80
        return [
81 4
            '[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*',
82
            '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
83
            '"(?:""|[^"])*+"',
84
        ];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 4
    protected function getNonCatchablePatterns()
91
    {
92 4
        return ['\s+', '\*+', '(.)'];
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 301
    protected function getType(&$value)
99
    {
100 301
        $type = self::T_NONE;
101
102 301
        if ($value[0] === '"') {
103 230
            $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
104
105 230
            return self::T_STRING;
106
        }
107
108 300
        if (isset($this->noCase[$value])) {
109 299
            return $this->noCase[$value];
110
        }
111
112 299
        if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
113 298
            return self::T_IDENTIFIER;
114
        }
115
116 98
        $lowerValue = strtolower($value);
117
118 98
        if (isset($this->withCase[$lowerValue])) {
119
            return $this->withCase[$lowerValue];
120
        }
121
122
        // Checking numeric value
123 98
        if (is_numeric($value)) {
124 87
            return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
125 87
                ? self::T_FLOAT : self::T_INTEGER;
126
        }
127
128 17
        return $type;
129
    }
130
}
131