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 MySQLExpressionProvider 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 MySQLExpressionProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class MySQLExpressionProvider implements IExpressionProvider |
||
13 | { |
||
14 | const ADD = '+'; |
||
15 | const CLOSE_BRACKET = ')'; |
||
16 | const COMMA = ','; |
||
17 | const DIVIDE = '/'; |
||
18 | const SUBTRACT = '-'; |
||
19 | const EQUAL = '='; |
||
20 | const GREATER_THAN = '>'; |
||
21 | const GREATER_THAN_OR_EQUAL = '>='; |
||
22 | const LESS_THAN = '<'; |
||
23 | const LESS_THAN_OR_EQUAL = '<='; |
||
24 | const LOGICAL_AND = '&&'; |
||
25 | const LOGICAL_NOT = '!'; |
||
26 | const LOGICAL_OR = '||'; |
||
27 | const MEMBER_ACCESS = ''; |
||
28 | const MODULO = '%'; |
||
29 | const MULTIPLY = '*'; |
||
30 | const NEGATE = '-'; |
||
31 | const NOT_EQUAL = '!='; |
||
32 | const OPEN_BRACKET = '('; |
||
33 | |||
34 | /** |
||
35 | * The type of the resource pointed by the resource path segment. |
||
36 | * |
||
37 | * @var ResourceType |
||
38 | */ |
||
39 | private $resourceType; |
||
40 | |||
41 | private $entityMapping; |
||
42 | |||
43 | /** |
||
44 | * Constructs new instance of MySQLExpressionProvider. |
||
45 | */ |
||
46 | public function __construct() |
||
50 | |||
51 | /** |
||
52 | * Get the name of the iterator. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getIteratorName() |
||
60 | |||
61 | /** |
||
62 | * call-back for setting the resource type. |
||
63 | * |
||
64 | * @param ResourceType $resourceType The resource type on which the filter is going to be applied |
||
65 | */ |
||
66 | public function setResourceType(ResourceType $resourceType) |
||
70 | |||
71 | /** |
||
72 | * Call-back for logical expression. |
||
73 | * |
||
74 | * @param ExpressionType $expressionType The type of logical expression |
||
75 | * @param string $left The left expression |
||
76 | * @param string $right The left expression |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | View Code Duplication | public function onLogicalExpression($expressionType, $left, $right) |
|
93 | |||
94 | /** |
||
95 | * Call-back for arithmetic expression. |
||
96 | * |
||
97 | * @param ExpressionType $expressionType The type of arithmetic expression |
||
98 | * @param string $left The left expression |
||
99 | * @param string $right The left expression |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | View Code Duplication | public function onArithmeticExpression($expressionType, $left, $right) |
|
125 | |||
126 | /** |
||
127 | * Call-back for relational expression. |
||
128 | * |
||
129 | * @param ExpressionType $expressionType The type of relation expression |
||
130 | * @param string $left The left expression |
||
131 | * @param string $right The left expression |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | View Code Duplication | public function onRelationalExpression($expressionType, $left, $right) |
|
160 | |||
161 | /** |
||
162 | * Call-back for unary expression. |
||
163 | * |
||
164 | * @param ExpressionType $expressionType The type of unary expression |
||
165 | * @param string $child The child expression |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | View Code Duplication | public function onUnaryExpression($expressionType, $child) |
|
182 | |||
183 | /** |
||
184 | * Call-back for constant expression. |
||
185 | * |
||
186 | * @param IType $type The type of constant |
||
187 | * @param mixed $value The value of the constant |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | View Code Duplication | public function onConstantExpression(IType $type, $value) |
|
201 | |||
202 | /** |
||
203 | * Call-back for property access expression. |
||
204 | * |
||
205 | * @param PropertyAccessExpression $expression The property access expression |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function onPropertyAccessExpression($expression) |
||
240 | |||
241 | /** |
||
242 | * Call-back for function call expression. |
||
243 | * |
||
244 | * @param FunctionDescription $functionDescription Description of the function |
||
245 | * @param array<string> $params Parameters to the function |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | public function onFunctionCallExpression($functionDescription, $params) |
||
332 | |||
333 | /** |
||
334 | * To format binary expression. |
||
335 | * |
||
336 | * @param string $operator The binary operator |
||
337 | * @param string $left The left operand |
||
338 | * @param string $right The right operand |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | private function _prepareBinaryExpression($operator, $left, $right) |
||
356 | |||
357 | /** |
||
358 | * To format unary expression. |
||
359 | * |
||
360 | * @param string $operator The unary operator |
||
361 | * @param string $child The operand |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | private function _prepareUnaryExpression($operator, $child) |
||
369 | } |
||
370 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.