Complex classes like Interpreter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Interpreter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Interpreter |
||
27 | { |
||
28 | /** |
||
29 | * @var UqlExtensionContainer |
||
30 | */ |
||
31 | private $extensionContainer; |
||
32 | |||
33 | /** |
||
34 | * @var DataSourceInterface |
||
35 | */ |
||
36 | private $dataSource; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $dataSourceElements; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $caseSensitive; |
||
47 | |||
48 | /** |
||
49 | * @var FilterConditionFactory |
||
50 | */ |
||
51 | private $filterConditionFactory; |
||
52 | |||
53 | /** |
||
54 | * @var ContextFactory |
||
55 | */ |
||
56 | private $contextFactory; |
||
57 | |||
58 | /** |
||
59 | * Constructor needs the columns descriptor to figure out appropriate filtering methods |
||
60 | * and translate identifiers. |
||
61 | * |
||
62 | * @param UqlExtensionContainer $extensionContainer |
||
63 | * @param DataSourceInterface $dataSource |
||
64 | * @param FilterConditionFactory $filterConditionFactory |
||
65 | * @param ContextFactory $contextFactory |
||
66 | * @param bool $caseSensitive |
||
67 | */ |
||
68 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * Generate the filter objects corresponding to a UQL string. |
||
95 | * |
||
96 | * @param string $uql |
||
97 | * |
||
98 | * @return Filter |
||
99 | */ |
||
100 | public function interpret($uql) |
||
111 | |||
112 | /** |
||
113 | * Helper method: matches filtering operators to valid UQL operators |
||
114 | * in order to do Filter to UQL transformations |
||
115 | * |
||
116 | * @param string $method |
||
117 | * |
||
118 | * @throws UQLInterpreterException |
||
119 | * @return string |
||
120 | */ |
||
121 | public static function methodToUQLOperator($method) |
||
151 | |||
152 | /** |
||
153 | * Transforms a subtree of the AST into a concrete filter definition. |
||
154 | * This function recursively builds all sub-trees. |
||
155 | * |
||
156 | * @param ASTGroup|ASTAssertion|mixed $astSubtree |
||
157 | * |
||
158 | * TODO: This looks like it should not be public (it is only used in tests). |
||
159 | * We could move it and it's dependencies to its own class so that it can be tested |
||
160 | * |
||
161 | * @return Filter |
||
162 | * @throws \Exception |
||
163 | */ |
||
164 | public function buildFilter($astSubtree) |
||
181 | |||
182 | /** |
||
183 | * Translate <operator> tokens into Filter Methods. |
||
184 | * |
||
185 | * @param string $token |
||
186 | * @param FieldInterface $dataSourceElement |
||
187 | * |
||
188 | * @throws UQLInterpreterException |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function translateOperator($token, FieldInterface $dataSourceElement) |
||
254 | |||
255 | /** |
||
256 | * Translate Lexer <logic> tokens into Filter Condition Types. |
||
257 | * |
||
258 | * @param $token |
||
259 | * |
||
260 | * @return string |
||
261 | * @throws \Exception |
||
262 | */ |
||
263 | private function translateLogic($token) |
||
277 | |||
278 | /** |
||
279 | * Trim and clean up the value to be set in the filter. |
||
280 | * |
||
281 | * @param mixed $value |
||
282 | * |
||
283 | * @return mixed |
||
284 | */ |
||
285 | private function parseValue($value) |
||
293 | |||
294 | /** |
||
295 | * @param ASTFunctionCall $functionCall |
||
296 | * |
||
297 | * @return mixed |
||
298 | * @throws FunctionNotFoundException |
||
299 | * @throws UQLInterpreterException |
||
300 | */ |
||
301 | private function callFunction(ASTFunctionCall $functionCall) |
||
313 | |||
314 | /** |
||
315 | * @param array $elements |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | private function parseArray($elements) |
||
328 | |||
329 | /** |
||
330 | * @param string $identifier |
||
331 | * |
||
332 | * @return Field |
||
333 | * @throws UQLInterpreterException |
||
334 | */ |
||
335 | private function matchDataSourceElement($identifier) |
||
347 | |||
348 | /** |
||
349 | * @param ASTAssertion $astSubtree |
||
350 | * |
||
351 | * @throws UQLInterpreterException |
||
352 | * |
||
353 | * @return array|mixed |
||
354 | */ |
||
355 | private function getValue(ASTAssertion $astSubtree) |
||
382 | |||
383 | /** |
||
384 | * @param ASTAssertion $astSubtree |
||
385 | * |
||
386 | * @throws UQLInterpreterException |
||
387 | * |
||
388 | * @return FilterCondition |
||
389 | */ |
||
390 | private function buildFilterConditionFromASTAssertion(ASTAssertion $astSubtree) |
||
398 | |||
399 | /** |
||
400 | * @param ASTGroup $astSubtree |
||
401 | * |
||
402 | * @throws UQLInterpreterException |
||
403 | * @throws \Exception |
||
404 | * |
||
405 | * @return Filter |
||
406 | */ |
||
407 | private function buildFilterFromASTGroup(ASTGroup $astSubtree) |
||
423 | |||
424 | /** |
||
425 | * @param ASTFunctionCall $functionCall |
||
426 | * @param UqlFunctionInterface $function |
||
427 | * |
||
428 | * @return array |
||
429 | */ |
||
430 | private function getFunctionArguments(ASTFunctionCall $functionCall, $function) |
||
441 | } |
||
442 |