Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ExpressionParser2 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 ExpressionParser2, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ExpressionParser2 extends ExpressionParser |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | private $_mapTable; |
||
40 | |||
41 | /** |
||
42 | * Collection of navigation properties used in the expression. |
||
43 | * |
||
44 | * @var array(array(ResourceProperty)) |
||
45 | */ |
||
46 | private $_navigationPropertiesUsedInTheExpression; |
||
47 | |||
48 | /** |
||
49 | * Indicates whether the end user has implemented IExpressionProvider or not. |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $_isPHPExpressionProvider; |
||
|
|||
54 | |||
55 | /** |
||
56 | * Create new instance of ExpressionParser2 |
||
57 | * |
||
58 | * @param string $text The text expression to parse. |
||
59 | * @param ResourceType $resourceType The resource type in which |
||
60 | * expression will be applied. |
||
61 | * @param Bool $isPHPExpressionProvider True if IExpressionProvider provider is |
||
62 | * implemented by user, False otherwise |
||
63 | */ |
||
64 | public function __construct($text, ResourceType $resourceType, $isPHPExpressionProvider |
||
70 | |||
71 | /** |
||
72 | * Parse and generate expression from the the given odata expression. |
||
73 | * |
||
74 | * |
||
75 | * @param string $text The text expression to parse |
||
76 | * @param ResourceType $resourceType The resource type in which |
||
77 | * @param IExpressionProvider $expressionProvider Implementation of IExpressionProvider |
||
78 | * |
||
79 | * @return FilterInfo |
||
80 | * |
||
81 | * @throws ODataException If any error occurs while parsing the odata expression or building the php/custom expression. |
||
82 | * |
||
83 | */ |
||
84 | public static function parseExpression2($text, ResourceType $resourceType, \POData\Providers\Expression\IExpressionProvider $expressionProvider) { |
||
107 | |||
108 | /** |
||
109 | * Parse the expression |
||
110 | * |
||
111 | * @see library/POData/QueryProcessor/ExpressionParser::parseFilter() |
||
112 | * |
||
113 | * @return AbstractExpression |
||
114 | * |
||
115 | * @throws ODataException |
||
116 | */ |
||
117 | public function parseFilter() |
||
133 | |||
134 | /** |
||
135 | * Process the expression node for nullability |
||
136 | * |
||
137 | * @param AbstractExpression $expression The expression node to process. |
||
138 | * @param AbstractExpression $parentExpression The parent expression of expression node to process. |
||
139 | * @param boolean $checkNullForMostChild whether to include null check for current property. |
||
140 | * |
||
141 | * @return AbstractExpression New expression tree with nullability check |
||
142 | * |
||
143 | * @throws ODataException |
||
144 | */ |
||
145 | private function _processNodeForNullability($expression, $parentExpression, |
||
172 | |||
173 | /** |
||
174 | * Process an arithmetic expression node for nullability |
||
175 | * |
||
176 | * @param ArithmeticExpression $expression The arithmetic expression node |
||
177 | * to process. |
||
178 | * |
||
179 | * @return LogicalExpression|null |
||
180 | */ |
||
181 | private function _processArithmeticNode(ArithmeticExpression $expression) |
||
204 | |||
205 | /** |
||
206 | * Process an arithmetic expression node for nullability |
||
207 | * |
||
208 | * @param FunctionCallExpression $expression The function call expression |
||
209 | * node to process. |
||
210 | * @param AbstractExpression $parentExpression The parent expression of |
||
211 | * expression node to process. |
||
212 | * |
||
213 | * @return LogicalExpression|null |
||
214 | */ |
||
215 | private function _processFunctionCallNode(FunctionCallExpression $expression, |
||
255 | |||
256 | /** |
||
257 | * Process an logical expression node for nullability. |
||
258 | * |
||
259 | * @param LogicalExpression $expression The logical expression node |
||
260 | * to process. |
||
261 | * @param AbstractExpression $parentExpression The parent expression of |
||
262 | * expression node to process. |
||
263 | * |
||
264 | * @return LogicalExpression|null |
||
265 | */ |
||
266 | private function _processLogicalNode( |
||
324 | |||
325 | /** |
||
326 | * Process an property access expression node for nullability |
||
327 | * |
||
328 | * @param PropertyAccessExpression $expression The property access |
||
329 | * expression node to process. |
||
330 | * @param AbstractExpression $parentExpression The parent expression of |
||
331 | * expression node to process. |
||
332 | * @param boolean $checkNullForMostChild Wheter to check null for |
||
333 | * most child node or not. |
||
334 | * |
||
335 | * @return LogicalExpression|RelationalExpression|null |
||
336 | */ |
||
337 | private function _processPropertyAccessNode( |
||
358 | |||
359 | /** |
||
360 | * Process a releational expression node for nullability. |
||
361 | * |
||
362 | * @param RelationalExpression $expression The relational expression node |
||
363 | * to process. |
||
364 | * @param AbstractExpression $parentExpression The parent expression of |
||
365 | * expression node to process. |
||
366 | * |
||
367 | * @return LogicalExpression|null |
||
368 | */ |
||
369 | private function _processRelationalNode(RelationalExpression $expression, |
||
405 | |||
406 | /** |
||
407 | * Process an unary expression node for nullability |
||
408 | * |
||
409 | * @param UnaryExpression $expression The unary expression node |
||
410 | * to process. |
||
411 | * @param AbstractExpression $parentExpression The parent expression of |
||
412 | * expression node to process. |
||
413 | * |
||
414 | * @return LogicalExpression|null |
||
415 | */ |
||
416 | private function _processUnaryNode(UnaryExpression $expression, |
||
450 | |||
451 | /** |
||
452 | * Merge two null check expression trees by removing duplicate nodes. |
||
453 | * |
||
454 | * @param AbstractExpression $nullCheckExpTree1 First expression. |
||
455 | * @param AbstractExpression $nullCheckExpTree2 Second expression. |
||
456 | * |
||
457 | * @return UnaryExpression|LogicalExpression |
||
458 | */ |
||
459 | private function _mergeNullableExpressionTrees($nullCheckExpTree1, |
||
497 | |||
498 | /** |
||
499 | * Populate map table |
||
500 | * |
||
501 | * @param AbstractExpression $nullCheckExpTree The expression to verfiy. |
||
502 | * |
||
503 | * @return void |
||
504 | * |
||
505 | * @throws ODataException |
||
506 | */ |
||
507 | private function _map($nullCheckExpTree) |
||
533 | } |