Completed
Pull Request — master (#344)
by Soner
04:54 queued 02:14
created

TokenParser::parseUseStatementV8()   C

Complexity

Conditions 15
Paths 17

Size

Total Lines 44
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 15
eloc 37
c 1
b 1
f 0
nc 17
nop 0
dl 0
loc 44
rs 5.9166

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\Common\Annotations;
4
5
/**
6
 * Parses a file for namespaces/use/class declarations.
7
 *
8
 * @author Fabien Potencier <[email protected]>
9
 * @author Christian Kaps <[email protected]>
10
 */
11
class TokenParser
12
{
13
    /**
14
     * The token list.
15
     *
16
     * @var array
17
     */
18
    private $tokens;
19
20
    /**
21
     * The number of tokens.
22
     *
23
     * @var int
24
     */
25
    private $numTokens;
26
27
    /**
28
     * The current array pointer.
29
     *
30
     * @var int
31
     */
32
    private $pointer = 0;
33
34
    /**
35
     * @param string $contents
36
     */
37
    public function __construct($contents)
38
    {
39
        $this->tokens = token_get_all($contents);
40
41
        // The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it
42
        // saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored
43
        // doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a
44
        // docblock. If the first thing in the file is a class without a doc block this would cause calls to
45
        // getDocBlock() on said class to return our long lost doc_comment. Argh.
46
        // To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least
47
        // it's harmless to us.
48
        token_get_all("<?php\n/**\n *\n */");
49
50
        $this->numTokens = count($this->tokens);
51
52
        // Compatibility defines for PHP < 8.0.
53
        if (!defined('T_NAME_QUALIFIED')) {
54
            \define('T_NAME_QUALIFIED', -2);
55
        }
56
    }
57
58
    /**
59
     * Gets the next non whitespace and non comment token.
60
     *
61
     * @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped.
62
     *                                     If FALSE then only whitespace and normal comments are skipped.
63
     *
64
     * @return array|null The token if exists, null otherwise.
65
     */
66
    public function next($docCommentIsComment = TRUE)
67
    {
68
        for ($i = $this->pointer; $i < $this->numTokens; $i++) {
69
            $this->pointer++;
70
            if ($this->tokens[$i][0] === T_WHITESPACE ||
71
                $this->tokens[$i][0] === T_COMMENT ||
72
                ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {
73
74
                continue;
75
            }
76
77
            return $this->tokens[$i];
78
        }
79
80
        return null;
81
    }
82
83
    /**
84
     * Parses a single use statement.
85
     *
86
     * @return array A list with all found class names for a use statement.
87
     */
88
    public function parseUseStatement()
89
    {
90
        return \PHP_VERSION_ID >=80000 ? $this->parseUseStatementV8() : $this->parseUseStatementV7();
91
    }
92
93
    private function parseUseStatementV7(): array
94
    {
95
        $groupRoot = '';
96
        $class = '';
97
        $alias = '';
98
        $statements = [];
99
        $explicitAlias = false;
100
        while (($token = $this->next())) {
101
            $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
102
            if (!$explicitAlias && $isNameToken) {
103
                $class .= $token[1];
104
                $alias = $token[1];
105
            } else if ($explicitAlias && $isNameToken) {
106
                $alias .= $token[1];
107
            } else if ($token[0] === T_AS) {
108
                $explicitAlias = true;
109
                $alias = '';
110
            } else if ($token === ',') {
111
                $statements[strtolower($alias)] = $groupRoot . $class;
112
                $class = '';
113
                $alias = '';
114
                $explicitAlias = false;
115
            } else if ($token === ';') {
116
                $statements[strtolower($alias)] = $groupRoot . $class;
117
                break;
118
            } else if ($token === '{' ) {
119
                $groupRoot = $class;
120
                $class = '';
121
            } else if ($token === '}' ) {
122
                continue;
123
            } else {
124
                break;
125
            }
126
        }
127
128
        return $statements;
129
    }
130
131
    private function parseUseStatementV8(): array
132
    {
133
        $groupRoot = '';
134
        $class = '';
135
        $alias = '';
136
        $statements = [];
137
        $explicitAlias = false;
138
        while (($token = $this->next())) {
139
            $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
140
141
            if ($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED) {
0 ignored issues
show
Bug introduced by
The constant Doctrine\Common\Annotations\T_NAME_FULLY_QUALIFIED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
142
                $class .= $token[1];
143
144
                $classSplit = explode('\\', $token[1]);
145
                $alias = $classSplit[count($classSplit) - 1];
146
            } else if($token[0] === T_NS_SEPARATOR) {
147
                $class .= '\\';
148
            } else if ($token[0] === T_AS) {
149
                $explicitAlias = true;
150
                $alias = '';
151
            } else if ($isNameToken && !$explicitAlias) {
152
                $class = $token[1];
153
                $alias = $token[1];
154
            } else if ($isNameToken && $explicitAlias) {
155
                $alias = $token[1];
156
            } else if ($token === ',') {
157
                $statements[strtolower($alias)] = $groupRoot . $class;
158
                $class = '';
159
                $alias = '';
160
                $explicitAlias = false;
161
            } else if ($token === ';') {
162
                $statements[strtolower($alias)] = $groupRoot . $class;
163
                break;
164
            } else if ($token === '{' ) {
165
                $groupRoot = $class;
166
                $class = '';
167
            } else if ($token === '}' ) {
168
                continue;
169
            } else {
170
                break;
171
            }
172
        }
173
174
        return $statements;
175
    }
176
177
    /**
178
     * Gets all use statements.
179
     *
180
     * @param string $namespaceName The namespace name of the reflected class.
181
     *
182
     * @return array A list with all found use statements.
183
     */
184
    public function parseUseStatements($namespaceName)
185
    {
186
        $statements = [];
187
        while (($token = $this->next())) {
188
            if ($token[0] === T_USE) {
189
                $statements = array_merge($statements, $this->parseUseStatement());
190
                continue;
191
            }
192
            if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
193
                continue;
194
            }
195
196
            // Get fresh array for new namespace. This is to prevent the parser to collect the use statements
197
            // for a previous namespace with the same name. This is the case if a namespace is defined twice
198
            // or if a namespace with the same name is commented out.
199
            $statements = [];
200
        }
201
202
        return $statements;
203
    }
204
205
    /**
206
     * Gets the namespace.
207
     *
208
     * @return string The found namespace.
209
     */
210
    public function parseNamespace()
211
    {
212
        $name = '';
213
        while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR || $token[0] === T_NAME_QUALIFIED)) {
214
            $name .= $token[1];
215
        }
216
217
        return $name;
218
    }
219
220
    /**
221
     * Gets the class name.
222
     *
223
     * @return string The found class name.
224
     */
225
    public function parseClass()
226
    {
227
        // Namespaces and class names are tokenized the same: T_STRINGs
228
        // separated by T_NS_SEPARATOR so we can use one function to provide
229
        // both.
230
        return $this->parseNamespace();
231
    }
232
}
233