DocLexer::getType()   B
last analyzed

Complexity

Conditions 10
Paths 9

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.0244

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 31
ccs 15
cts 16
cp 0.9375
rs 7.6666
c 0
b 0
f 0
cc 10
nc 9
nop 1
crap 10.0244

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
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
38
    /**
39
     * @var array
40
     */
41
    protected $noCase = [
42
        '@'  => self::T_AT,
43
        ','  => self::T_COMMA,
44
        '('  => self::T_OPEN_PARENTHESIS,
45
        ')'  => self::T_CLOSE_PARENTHESIS,
46
        '{'  => self::T_OPEN_CURLY_BRACES,
47
        '}'  => self::T_CLOSE_CURLY_BRACES,
48
        '='  => self::T_EQUALS,
49
        ':'  => self::T_COLON,
50
        '\\' => self::T_NAMESPACE_SEPARATOR
51
    ];
52
53
    /**
54
     * @var array
55
     */
56
    protected $withCase = [
57
        'true'  => self::T_TRUE,
58
        'false' => self::T_FALSE,
59
        'null'  => self::T_NULL
60
    ];
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 4
    protected function getCatchablePatterns()
66
    {
67
        return [
68 4
            '[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*',
69
            '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
70
            '"(?:""|[^"])*+"',
71
        ];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 4
    protected function getNonCatchablePatterns()
78
    {
79 4
        return ['\s+', '\*+', '(.)'];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 331
    protected function getType(&$value)
86
    {
87 331
        $type = self::T_NONE;
88
89 331
        if ($value[0] === '"') {
90 101
            $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
91
92 101
            return self::T_STRING;
93
        }
94
95 330
        if (isset($this->noCase[$value])) {
96 330
            return $this->noCase[$value];
97
        }
98
99 330
        if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
100 330
            return self::T_IDENTIFIER;
101
        }
102
103 103
        $lowerValue = strtolower($value);
104
105 103
        if (isset($this->withCase[$lowerValue])) {
106
            return $this->withCase[$lowerValue];
107
        }
108
109
        // Checking numeric value
110 103
        if (is_numeric($value)) {
111 90
            return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
112 90
                ? self::T_FLOAT : self::T_INTEGER;
113
        }
114
115 19
        return $type;
116
    }
117
}
118