GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Lexer::getType()   C
last analyzed

Complexity

Conditions 15
Paths 15

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 55
rs 6.7239
c 1
b 0
f 0
cc 15
eloc 32
nc 15
nop 1

How to fix   Long Method    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 Coduo\PHPMatcher;
4
5
use Doctrine\Common\Lexer\AbstractLexer;
6
7
final class Lexer extends AbstractLexer
8
{
9
    const T_NONE = 1;
10
    const T_EXPANDER_NAME = 2;
11
    const T_CLOSE_PARENTHESIS = 3;
12
    const T_OPEN_CURLY_BRACE    = 4;
13
    const T_CLOSE_CURLY_BRACE   = 5;
14
    const T_STRING = 6;
15
    const T_NUMBER = 7;
16
    const T_BOOLEAN = 8;
17
    const T_NULL = 9;
18
    const T_COMMA  = 10;
19
    const T_COLON = 11;
20
    const T_TYPE_PATTERN = 12;
21
22
    /**
23
     * Lexical catchable patterns.
24
     *
25
     * @return array
26
     */
27
    protected function getCatchablePatterns()
28
    {
29
        return array(
30
            "\\.?[a-zA-Z0-9_]+\\(", // expander name
31
            "[a-zA-Z0-9.]*", // words
32
            "\\-?[0-9]*\\.?[0-9]*", // numbers
33
            "'(?:[^']|'')*'", // string between ' character
34
            "\"(?:[^\"]|\"\")*\"", // string between " character,
35
            "@[a-zA-Z0-9\\*]+@", // type pattern
36
        );
37
    }
38
39
    /**
40
     * Lexical non-catchable patterns.
41
     *
42
     * @return array
43
     */
44
    protected function getNonCatchablePatterns()
45
    {
46
        return array(
47
            "\\s+",
48
        );
49
    }
50
51
    /**
52
     * Retrieve token type. Also processes the token value if necessary.
53
     *
54
     * @param string $value
55
     * @return integer
56
     */
57
    protected function getType(&$value)
58
    {
59
        $type = self::T_NONE;
60
61
        if (')' === $value) {
62
            return self::T_CLOSE_PARENTHESIS;
63
        }
64
        if ('{' === $value) {
65
            return self::T_OPEN_CURLY_BRACE;
66
        }
67
        if ('}' === $value) {
68
            return self::T_CLOSE_CURLY_BRACE;
69
        }
70
        if (':' === $value) {
71
            return self::T_COLON;
72
        }
73
        if (',' === $value) {
74
            return self::T_COMMA;
75
        }
76
77
        if ($this->isTypePatternToken($value)) {
78
            $value = trim($value, '@');
79
            return self::T_TYPE_PATTERN;
80
        }
81
82
        if ($this->isStringToken($value)) {
83
            $value = $this->extractStringValue($value);
84
            return self::T_STRING;
85
        }
86
87
        if ($this->isBooleanToken($value)) {
88
            $value = (strtolower($value) === 'true') ? true : false;
89
            return self::T_BOOLEAN;
90
        }
91
92
        if ($this->isNullToken($value)) {
93
            $value = null;
94
            return self::T_NULL;
95
        }
96
97
        if (is_numeric($value)) {
98
            if (is_string($value)) {
99
                $value = (strpos($value, '.') === false) ? (int) $value : (float) $value;
100
            }
101
102
            return self::T_NUMBER;
103
        }
104
105
        if ($this->isExpanderNameToken($value)) {
106
            $value = rtrim(ltrim($value, '.'), '(');
107
            return self::T_EXPANDER_NAME;
108
        }
109
110
        return $type;
111
    }
112
113
    /**
114
     * @param $value
115
     * @return bool
116
     */
117
    protected function isStringToken($value)
118
    {
119
        return in_array(substr($value, 0, 1), array("\"", "'"));
120
    }
121
122
    /**
123
     * @param string $value
124
     * @return bool
125
     */
126
    protected function isBooleanToken($value)
127
    {
128
        return in_array(strtolower($value), array('true', 'false'), true);
129
    }
130
131
    /**
132
     * @param string $value
133
     * @return bool
134
     */
135
    protected function isNullToken($value)
136
    {
137
        return strtolower($value) === 'null';
138
    }
139
140
    /**
141
     * @param $value
142
     * @return string
143
     */
144
    protected function extractStringValue($value)
145
    {
146
        return trim(trim($value, "'"), '"');
147
    }
148
149
    /**
150
     * @param $value
151
     * @return bool
152
     */
153
    protected function isExpanderNameToken($value)
154
    {
155
        return substr($value, -1) === '(' && strlen($value) > 1;
156
    }
157
158
    /**
159
     * @param $value
160
     * @return bool
161
     */
162
    protected function isTypePatternToken($value)
163
    {
164
        return substr($value, 0, 1) === '@' && substr($value, strlen($value) - 1, 1) === '@' && strlen($value) > 1;
165
    }
166
}
167