Conditions | 36 |
Paths | 738 |
Total Lines | 202 |
Code Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
64 | protected function tokenize($str) |
||
65 | { |
||
66 | if ($str{0} == '@') |
||
67 | { |
||
68 | $str = 'this.' . substr($str, 1); |
||
69 | } |
||
70 | |||
71 | $str .= '.'; |
||
72 | |||
73 | $length = strlen($str); |
||
74 | |||
75 | $quote = null; |
||
76 | $quote_closed = null; |
||
77 | $part = null; |
||
78 | $escape = false; |
||
79 | |||
80 | $function = null; |
||
81 | $args = []; |
||
82 | $args_evaluate = []; |
||
83 | $args_count = 0; |
||
84 | |||
85 | $parts = []; |
||
86 | |||
87 | for ($i = 0 ; $i < $length ; $i++) |
||
88 | { |
||
89 | $c = $str{$i}; |
||
90 | |||
91 | if ($escape) |
||
92 | { |
||
93 | $part .= $c; |
||
94 | |||
95 | $escape = false; |
||
96 | |||
97 | continue; |
||
98 | } |
||
99 | |||
100 | if ($c == '\\') |
||
101 | { |
||
102 | $escape = true; |
||
103 | |||
104 | continue; |
||
105 | } |
||
106 | |||
107 | if ($c == '"' || $c == '\'' || $c == '`') |
||
108 | { |
||
109 | if ($quote && $quote == $c) |
||
|
|||
110 | { |
||
111 | $quote = null; |
||
112 | $quote_closed = $c; |
||
113 | |||
114 | if ($function) |
||
115 | { |
||
116 | continue; |
||
117 | } |
||
118 | } |
||
119 | else if (!$quote) |
||
120 | { |
||
121 | $quote = $c; |
||
122 | |||
123 | if ($function) |
||
124 | { |
||
125 | continue; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | if ($quote) |
||
131 | { |
||
132 | $part .= $c; |
||
133 | |||
134 | continue; |
||
135 | } |
||
136 | |||
137 | # |
||
138 | # we are not in a quote |
||
139 | # |
||
140 | |||
141 | if ($c == '.') |
||
142 | { |
||
143 | if (strlen($part)) |
||
144 | { |
||
145 | $parts[] = [ |
||
146 | |||
147 | self::TOKEN_TYPE => self::TOKEN_TYPE_IDENTIFIER, |
||
148 | self::TOKEN_VALUE => $part |
||
149 | |||
150 | ]; |
||
151 | } |
||
152 | |||
153 | $part = null; |
||
154 | |||
155 | continue; |
||
156 | } |
||
157 | |||
158 | if ($c == '(') |
||
159 | { |
||
160 | $function = $part; |
||
161 | |||
162 | $args = []; |
||
163 | $args_count = 0; |
||
164 | |||
165 | $part = null; |
||
166 | |||
167 | continue; |
||
168 | } |
||
169 | |||
170 | if (($c == ',' || $c == ')') && $function) |
||
171 | { |
||
172 | if ($part !== null) |
||
173 | { |
||
174 | if ($quote_closed == '`') |
||
175 | { |
||
176 | $args_evaluate[] = $args_count; |
||
177 | } |
||
178 | |||
179 | if (!$quote_closed) |
||
180 | { |
||
181 | # |
||
182 | # end of an unquoted part. |
||
183 | # it might be an integer, a float, or maybe a constant ! |
||
184 | # |
||
185 | |||
186 | switch ($part) |
||
187 | { |
||
188 | case 'true': |
||
189 | case 'TRUE': |
||
190 | { |
||
191 | $part = true; |
||
192 | } |
||
193 | break; |
||
194 | |||
195 | case 'false': |
||
196 | case 'FALSE': |
||
197 | { |
||
198 | $part = false; |
||
199 | } |
||
200 | break; |
||
201 | |||
202 | case 'null': |
||
203 | case 'NULL': |
||
204 | { |
||
205 | $part = null; |
||
206 | } |
||
207 | break; |
||
208 | |||
209 | default: |
||
210 | { |
||
211 | if (is_numeric($part)) |
||
212 | { |
||
213 | $part = (int) $part; |
||
214 | } |
||
215 | else if (is_float($part)) |
||
216 | { |
||
217 | $part = (float) $part; |
||
218 | } |
||
219 | else |
||
220 | { |
||
221 | $part = constant($part); |
||
222 | } |
||
223 | } |
||
224 | break; |
||
225 | } |
||
226 | } |
||
227 | |||
228 | $args[] = $part; |
||
229 | $args_count++; |
||
230 | |||
231 | $part = null; |
||
232 | } |
||
233 | |||
234 | $quote_closed = null; |
||
235 | |||
236 | if ($c != ')') |
||
237 | { |
||
238 | continue; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | if ($c == ')' && $function) |
||
243 | { |
||
244 | $parts[] = [ |
||
245 | |||
246 | self::TOKEN_TYPE => self::TOKEN_TYPE_FUNCTION, |
||
247 | self::TOKEN_VALUE => $function, |
||
248 | self::TOKEN_ARGS => $args, |
||
249 | self::TOKEN_ARGS_EVALUATE => $args_evaluate |
||
250 | |||
251 | ]; |
||
252 | |||
253 | continue; |
||
254 | } |
||
255 | |||
256 | if ($c == ' ' && $function) |
||
257 | { |
||
258 | continue; |
||
259 | } |
||
260 | |||
261 | $part .= $c; |
||
262 | } |
||
263 | |||
264 | return $parts; |
||
265 | } |
||
266 | |||
512 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: