Conditions | 5 |
Paths | 4 |
Total Lines | 68 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
122 | public static function tokenizer($query) { |
||
123 | /* |
||
124 | query syntax: |
||
125 | part = ( name '.' )* name compare value |
||
126 | query = part | part operator (not) part | (not) '(' query ')' |
||
127 | operator = 'and' | 'or' |
||
128 | not = 'not' |
||
129 | compare = '<' | '>' | '=' | '>=' | '<=' | 'like' | 'not like' | '?' |
||
130 | value = number | string |
||
131 | number = [0-9]* ('.' [0-9]+)? |
||
132 | string = \' [^\\1]* \' |
||
133 | |||
134 | e.g: "contact.address.street like '%Crescent%' and ( name.firstname = 'Foo' or name.lastname = 'Bar')" |
||
135 | */ |
||
136 | $token = <<<'REGEX' |
||
137 | /^\s* |
||
138 | ( |
||
139 | (?<compare> |
||
140 | <= | >= | <> | < | > | = | != | ~= | !~ | \? |
||
141 | ) |
||
142 | | |
||
143 | (?<operator> |
||
144 | and | or |
||
145 | ) |
||
146 | | |
||
147 | (?<not> |
||
148 | not |
||
149 | ) |
||
150 | | |
||
151 | (?<name> |
||
152 | [a-z]+[a-z0-9_-]* |
||
153 | (?: \. [a-z]+[a-z0-9_-]* )* |
||
154 | ) |
||
155 | | |
||
156 | (?<number> |
||
157 | [+-]?[0-9](\.[0-9]+)? |
||
158 | ) |
||
159 | | |
||
160 | (?<string> |
||
161 | '(?:\\.|[^\\'])*' |
||
162 | ) |
||
163 | | |
||
164 | (?<parenthesis_open> |
||
165 | \( |
||
166 | ) |
||
167 | | |
||
168 | (?<parenthesis_close> |
||
169 | \) |
||
170 | ) |
||
171 | )/xi |
||
172 | REGEX; |
||
173 | do { |
||
174 | $result = preg_match($token, $query, $matches, PREG_OFFSET_CAPTURE); |
||
175 | if ($result) { |
||
176 | $value = $matches[0][0]; |
||
177 | $offset = $matches[0][1]; |
||
178 | $query = substr($query, strlen($value) + $offset); |
||
179 | yield array_filter( |
||
180 | $matches, |
||
181 | function($val, $key) { |
||
182 | return !is_int($key) && $val[0]; |
||
183 | }, |
||
184 | ARRAY_FILTER_USE_BOTH |
||
185 | ); |
||
186 | } |
||
187 | } while($result); |
||
188 | if ( trim($query) ) { |
||
189 | throw new \LogicException('Could not parse '.$query); |
||
190 | } |
||
194 |