Completed
Push — master ( 8a6821...f9deab )
by Andreas
04:47
created

DocLexer::getType()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 31
rs 7.6666
c 0
b 0
f 0
cc 10
nc 9
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Common\Annotations;
21
22
use Doctrine\Common\Lexer\AbstractLexer;
23
24
/**
25
 * Simple lexer for docblock annotations.
26
 *
27
 * @author Benjamin Eberlei <[email protected]>
28
 * @author Guilherme Blanco <[email protected]>
29
 * @author Jonathan Wage <[email protected]>
30
 * @author Roman Borschel <[email protected]>
31
 * @author Johannes M. Schmitt <[email protected]>
32
 */
33
final class DocLexer extends AbstractLexer
34
{
35
    const T_NONE                = 1;
36
    const T_INTEGER             = 2;
37
    const T_STRING              = 3;
38
    const T_FLOAT               = 4;
39
40
    // All tokens that are also identifiers should be >= 100
41
    const T_IDENTIFIER          = 100;
42
    const T_AT                  = 101;
43
    const T_CLOSE_CURLY_BRACES  = 102;
44
    const T_CLOSE_PARENTHESIS   = 103;
45
    const T_COMMA               = 104;
46
    const T_EQUALS              = 105;
47
    const T_FALSE               = 106;
48
    const T_NAMESPACE_SEPARATOR = 107;
49
    const T_OPEN_CURLY_BRACES   = 108;
50
    const T_OPEN_PARENTHESIS    = 109;
51
    const T_TRUE                = 110;
52
    const T_NULL                = 111;
53
    const T_COLON               = 112;
54
    const T_MINUS               = 113;
55
56
    /**
57
     * @var array
58
     */
59
    protected $noCase = [
60
        '@'  => self::T_AT,
61
        ','  => self::T_COMMA,
62
        '('  => self::T_OPEN_PARENTHESIS,
63
        ')'  => self::T_CLOSE_PARENTHESIS,
64
        '{'  => self::T_OPEN_CURLY_BRACES,
65
        '}'  => self::T_CLOSE_CURLY_BRACES,
66
        '='  => self::T_EQUALS,
67
        ':'  => self::T_COLON,
68
        '-'  => self::T_MINUS,
69
        '\\' => self::T_NAMESPACE_SEPARATOR
70
    ];
71
72
    /**
73
     * @var array
74
     */
75
    protected $withCase = [
76
        'true'  => self::T_TRUE,
77
        'false' => self::T_FALSE,
78
        'null'  => self::T_NULL
79
    ];
80
81
    /**
82
     * Whether the next token starts immediately, or if there were
83
     * non-captured symbols before that
84
     */
85
    public function nextTokenIsAdjacent() : bool
86
    {
87
        return $this->token === null
88
            || ($this->lookahead !== null
89
                && ($this->lookahead['position'] - $this->token['position']) === strlen($this->token['value']));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    protected function getCatchablePatterns()
96
    {
97
        return [
98
            '[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*',
99
            '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
100
            '"(?:""|[^"])*+"',
101
        ];
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function getNonCatchablePatterns()
108
    {
109
        return ['\s+', '\*+', '(.)'];
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    protected function getType(&$value)
116
    {
117
        $type = self::T_NONE;
118
119
        if ($value[0] === '"') {
120
            $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
121
122
            return self::T_STRING;
123
        }
124
125
        if (isset($this->noCase[$value])) {
126
            return $this->noCase[$value];
127
        }
128
129
        if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
130
            return self::T_IDENTIFIER;
131
        }
132
133
        $lowerValue = strtolower($value);
134
135
        if (isset($this->withCase[$lowerValue])) {
136
            return $this->withCase[$lowerValue];
137
        }
138
139
        // Checking numeric value
140
        if (is_numeric($value)) {
141
            return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
142
                ? self::T_FLOAT : self::T_INTEGER;
143
        }
144
145
        return $type;
146
    }
147
}
148