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) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Transforms a subtree of the AST into a concrete filter definition. |
||
| 153 | * This function recursively builds all sub-trees. |
||
| 154 | * |
||
| 155 | * @param ASTGroup|ASTAssertion|mixed $astSubtree |
||
| 156 | * |
||
| 157 | * TODO: This looks like it should not be public (it is only used in tests). |
||
| 158 | * We could move it and it's dependencies to its own class so that it can be tested |
||
| 159 | * |
||
| 160 | * @return Filter |
||
| 161 | * @throws \Exception |
||
| 162 | */ |
||
| 163 | public function buildFilter($astSubtree) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Translate <operator> tokens into Filter Methods. |
||
| 183 | * |
||
| 184 | * @param string $token |
||
| 185 | * @param FieldInterface $dataSourceElement |
||
| 186 | * |
||
| 187 | * @throws UQLInterpreterException |
||
| 188 | * @return mixed |
||
| 189 | */ |
||
| 190 | public function translateOperator($token, FieldInterface $dataSourceElement) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Translate Lexer <logic> tokens into Filter Condition Types. |
||
| 256 | * |
||
| 257 | * @param $token |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | * @throws \Exception |
||
| 261 | */ |
||
| 262 | private function translateLogic($token) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Trim and clean up the value to be set in the filter. |
||
| 279 | * |
||
| 280 | * @param mixed $value |
||
| 281 | * |
||
| 282 | * @return mixed |
||
| 283 | */ |
||
| 284 | private function parseValue($value) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param ASTFunctionCall $functionCall |
||
| 295 | * |
||
| 296 | * @return mixed |
||
| 297 | * @throws FunctionNotFoundException |
||
| 298 | * @throws UQLInterpreterException |
||
| 299 | */ |
||
| 300 | private function callFunction(ASTFunctionCall $functionCall) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param array $elements |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | private function parseArray($elements) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $identifier |
||
| 330 | * |
||
| 331 | * @return Field |
||
| 332 | * @throws UQLInterpreterException |
||
| 333 | */ |
||
| 334 | private function matchDataSourceElement($identifier) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param ASTAssertion $astSubtree |
||
| 349 | * |
||
| 350 | * @throws UQLInterpreterException |
||
| 351 | * |
||
| 352 | * @return array|mixed |
||
| 353 | */ |
||
| 354 | private function getValue(ASTAssertion $astSubtree) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param ASTAssertion $astSubtree |
||
| 384 | * |
||
| 385 | * @throws UQLInterpreterException |
||
| 386 | * |
||
| 387 | * @return FilterCondition |
||
| 388 | */ |
||
| 389 | private function buildFilterConditionFromASTAssertion(ASTAssertion $astSubtree) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param ASTGroup $astSubtree |
||
| 400 | * |
||
| 401 | * @throws UQLInterpreterException |
||
| 402 | * @throws \Exception |
||
| 403 | * |
||
| 404 | * @return Filter |
||
| 405 | */ |
||
| 406 | private function buildFilterFromASTGroup(ASTGroup $astSubtree) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param ASTFunctionCall $functionCall |
||
| 425 | * @param UqlFunctionInterface $function |
||
| 426 | * |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | private function getFunctionArguments(ASTFunctionCall $functionCall, $function) |
||
| 440 | } |
||
| 441 |