Conditions | 32 |
Paths | 67 |
Total Lines | 118 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function nextToken(): int |
||
70 | { |
||
71 | $tok = -1; |
||
72 | ++$this->it; |
||
73 | if ($this->is->isEmpty()) { |
||
74 | if ($this->recurse) { |
||
75 | throw new \QueryPath\Exception('Recursion error detected at iteration ' . $this->it . '.'); |
||
76 | } |
||
77 | //print "{$this->it}: All done\n"; |
||
78 | $this->recurse = true; |
||
79 | $this->token = false; |
||
80 | |||
81 | return false; |
||
|
|||
82 | } |
||
83 | $ch = $this->is->consume(); |
||
84 | //print __FUNCTION__ . " Testing $ch.\n"; |
||
85 | if (ctype_space($ch)) { |
||
86 | $this->value = ' '; // Collapse all WS to a space. |
||
87 | $this->token = $tok = Token::WHITE; |
||
88 | |||
89 | //$ch = $this->is->consume(); |
||
90 | return $tok; |
||
91 | } |
||
92 | |||
93 | if ($ch === '-' || $ch === '_' || ctype_alnum($ch)) { |
||
94 | // It's a character |
||
95 | $this->value = $ch; //strtolower($ch); |
||
96 | $this->token = $tok = Token::CHAR; |
||
97 | |||
98 | return $tok; |
||
99 | } |
||
100 | |||
101 | $this->value = $ch; |
||
102 | |||
103 | switch ($ch) { |
||
104 | case '*': |
||
105 | $tok = Token::STAR; |
||
106 | break; |
||
107 | case chr(ord('>')): |
||
108 | $tok = Token::RANGLE; |
||
109 | break; |
||
110 | case '.': |
||
111 | $tok = Token::DOT; |
||
112 | break; |
||
113 | case '#': |
||
114 | $tok = Token::OCTO; |
||
115 | break; |
||
116 | case '[': |
||
117 | $tok = Token::LSQUARE; |
||
118 | break; |
||
119 | case ']': |
||
120 | $tok = Token::RSQUARE; |
||
121 | break; |
||
122 | case ':': |
||
123 | $tok = Token::COLON; |
||
124 | break; |
||
125 | case '(': |
||
126 | $tok = Token::LPAREN; |
||
127 | break; |
||
128 | case ')': |
||
129 | $tok = Token::RPAREN; |
||
130 | break; |
||
131 | case '+': |
||
132 | $tok = Token::PLUS; |
||
133 | break; |
||
134 | case '~': |
||
135 | $tok = Token::TILDE; |
||
136 | break; |
||
137 | case '=': |
||
138 | $tok = Token::EQ; |
||
139 | break; |
||
140 | case '|': |
||
141 | $tok = Token::PIPE; |
||
142 | break; |
||
143 | case ',': |
||
144 | $tok = Token::COMMA; |
||
145 | break; |
||
146 | case chr(34): |
||
147 | $tok = Token::QUOTE; |
||
148 | break; |
||
149 | case "'": |
||
150 | $tok = Token::SQUOTE; |
||
151 | break; |
||
152 | case '\\': |
||
153 | $tok = Token::BSLASH; |
||
154 | break; |
||
155 | case '^': |
||
156 | $tok = Token::CARAT; |
||
157 | break; |
||
158 | case '$': |
||
159 | $tok = Token::DOLLAR; |
||
160 | break; |
||
161 | case '@': |
||
162 | $tok = Token::AT; |
||
163 | break; |
||
164 | } |
||
165 | |||
166 | |||
167 | // Catch all characters that are legal within strings. |
||
168 | if ($tok === -1) { |
||
169 | // TODO: This should be UTF-8 compatible, but PHP doesn't |
||
170 | // have a native UTF-8 string. Should we use external |
||
171 | // mbstring library? |
||
172 | |||
173 | $ord = ord($ch); |
||
174 | // Characters in this pool are legal for use inside of |
||
175 | // certain strings. Extended ASCII is used here, though I |
||
176 | // Don't know if these are really legal. |
||
177 | if (($ord >= 32 && $ord <= 126) || ($ord >= 128 && $ord <= 255)) { |
||
178 | $tok = Token::STRING_LEGAL; |
||
179 | } else { |
||
180 | throw new ParseException('Illegal character found in stream: ' . $ord); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | $this->token = $tok; |
||
185 | |||
186 | return $tok; |
||
187 | } |
||
318 |