Total Complexity | 60 |
Total Lines | 344 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 |
||
19 | class MySQLExpressionProvider implements IExpressionProvider |
||
20 | { |
||
21 | const ADD = '+'; |
||
22 | const CLOSE_BRACKET = ')'; |
||
23 | const COMMA = ','; |
||
24 | const DIVIDE = '/'; |
||
25 | const SUBTRACT = '-'; |
||
26 | const EQUAL = '='; |
||
27 | const GREATER_THAN = '>'; |
||
28 | const GREATER_THAN_OR_EQUAL = '>='; |
||
29 | const LESS_THAN = '<'; |
||
30 | const LESS_THAN_OR_EQUAL = '<='; |
||
31 | const LOGICAL_AND = '&&'; |
||
32 | const LOGICAL_NOT = '!'; |
||
33 | const LOGICAL_OR = '||'; |
||
34 | const MEMBER_ACCESS = ''; |
||
35 | const MODULO = '%'; |
||
36 | const MULTIPLY = '*'; |
||
37 | const NEGATE = '-'; |
||
38 | const NOT_EQUAL = '!='; |
||
39 | const OPEN_BRACKET = '('; |
||
40 | |||
41 | /** |
||
42 | * The type of the resource pointed by the resource path segment. |
||
43 | * |
||
44 | * @var ResourceType |
||
45 | */ |
||
46 | private $resourceType; |
||
47 | |||
48 | /** @var array */ |
||
49 | private $entityMapping = []; |
||
50 | |||
51 | /** |
||
52 | * Constructs new instance of MySQLExpressionProvider. |
||
53 | */ |
||
54 | public function __construct() |
||
55 | { |
||
56 | $this->entityMapping = []; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get the name of the iterator. |
||
61 | * |
||
62 | * @return string|null |
||
63 | */ |
||
64 | public function getIteratorName() |
||
65 | { |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * call-back for setting the resource type. |
||
70 | * |
||
71 | * @param ResourceType $resourceType The resource type on which the filter is going to be applied |
||
72 | */ |
||
73 | public function setResourceType(ResourceType $resourceType) |
||
74 | { |
||
75 | $this->resourceType = $resourceType; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Call-back for logical expression. |
||
80 | * |
||
81 | * @param ExpressionType $expressionType The type of logical expression |
||
82 | * @param string $left The left expression |
||
83 | * @param string $right The left expression |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function onLogicalExpression(ExpressionType $expressionType, $left, $right) |
||
88 | { |
||
89 | switch ($expressionType) { |
||
90 | case ExpressionType::AND_LOGICAL(): |
||
91 | return $this->prepareBinaryExpression(self::LOGICAL_AND, $left, $right); |
||
92 | |||
93 | case ExpressionType::OR_LOGICAL(): |
||
94 | return $this->prepareBinaryExpression(self::LOGICAL_OR, $left, $right); |
||
95 | |||
96 | default: |
||
97 | throw new InvalidArgumentException('onLogicalExpression'); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * To format binary expression. |
||
103 | * |
||
104 | * @param string $operator The binary operator |
||
105 | * @param string $left The left operand |
||
106 | * @param string $right The right operand |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | private function prepareBinaryExpression($operator, $left, $right) |
||
111 | { |
||
112 | //DATETIMECMP |
||
113 | if (0 == substr_compare($left, 'DATETIMECMP', 0, 11)) { |
||
114 | $str = explode(';', $left, 2); |
||
115 | $str[0] = str_replace('DATETIMECMP', '', $str[0]); |
||
116 | |||
117 | return self::OPEN_BRACKET |
||
118 | . $str[0] . ' ' . $operator |
||
119 | . ' ' . $str[1] . self::CLOSE_BRACKET; |
||
120 | } |
||
121 | |||
122 | return self::OPEN_BRACKET . $left . ' ' . $operator . ' ' . $right . self::CLOSE_BRACKET; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Call-back for arithmetic expression. |
||
127 | * |
||
128 | * @param ExpressionType $expressionType The type of arithmetic expression |
||
129 | * @param string $left The left expression |
||
130 | * @param string $right The left expression |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function onArithmeticExpression(ExpressionType $expressionType, $left, $right) |
||
135 | { |
||
136 | switch ($expressionType) { |
||
137 | case ExpressionType::MULTIPLY(): |
||
138 | return $this->prepareBinaryExpression(self::MULTIPLY, $left, $right); |
||
139 | |||
140 | case ExpressionType::DIVIDE(): |
||
141 | return $this->prepareBinaryExpression(self::DIVIDE, $left, $right); |
||
142 | |||
143 | case ExpressionType::MODULO(): |
||
144 | return $this->prepareBinaryExpression(self::MODULO, $left, $right); |
||
145 | |||
146 | case ExpressionType::ADD(): |
||
147 | return $this->prepareBinaryExpression(self::ADD, $left, $right); |
||
148 | |||
149 | case ExpressionType::SUBTRACT(): |
||
150 | return $this->prepareBinaryExpression(self::SUBTRACT, $left, $right); |
||
151 | |||
152 | default: |
||
153 | throw new InvalidArgumentException('onArithmeticExpression'); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Call-back for relational expression. |
||
159 | * |
||
160 | * @param ExpressionType $expressionType The type of relation expression |
||
161 | * @param string $left The left expression |
||
162 | * @param string $right The left expression |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public function onRelationalExpression(ExpressionType $expressionType, $left, $right) |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Call-back for unary expression. |
||
194 | * |
||
195 | * @param ExpressionType $expressionType The type of unary expression |
||
196 | * @param string $child The child expression |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | public function onUnaryExpression(ExpressionType $expressionType, $child) |
||
201 | { |
||
202 | switch ($expressionType) { |
||
203 | case ExpressionType::NEGATE(): |
||
204 | return $this->prepareUnaryExpression(self::NEGATE, $child); |
||
205 | |||
206 | case ExpressionType::NOT_LOGICAL(): |
||
207 | return $this->prepareUnaryExpression(self::LOGICAL_NOT, $child); |
||
208 | |||
209 | default: |
||
210 | throw new InvalidArgumentException('onUnaryExpression'); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * To format unary expression. |
||
216 | * |
||
217 | * @param string $operator The unary operator |
||
218 | * @param string $child The operand |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | private function prepareUnaryExpression($operator, $child) |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Call-back for constant expression. |
||
229 | * |
||
230 | * @param IType $type The type of constant |
||
231 | * @param mixed $value The value of the constant |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function onConstantExpression(IType $type, $value) |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Call-back for property access expression. |
||
248 | * |
||
249 | * @param PropertyAccessExpression $expression The property access expression |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function onPropertyAccessExpression(PropertyAccessExpression $expression): string |
||
254 | { |
||
255 | if (null == $this->resourceType) { |
||
256 | throw new InvalidArgumentException('onPropertyAccessExpression - resourceType null'); |
||
257 | } |
||
258 | if (empty($this->resourceType->getName())) { |
||
259 | throw new InvalidArgumentException('onPropertyAccessExpression - resourceType has no name'); |
||
260 | } |
||
261 | $parent = $expression; |
||
262 | $entityTypeName = $this->resourceType->getName(); |
||
263 | $propertyName = $parent->getResourceProperty()->getName(); |
||
264 | if (array_key_exists($entityTypeName, $this->entityMapping)) { |
||
265 | if (array_key_exists($propertyName, $this->entityMapping[$entityTypeName])) { |
||
266 | return $this->entityMapping[$entityTypeName][$propertyName]; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | return $propertyName; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Call-back for function call expression. |
||
275 | * |
||
276 | * @param FunctionDescription $functionDescription Description of the function |
||
277 | * @param array<string> $params Parameters to the function |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function onFunctionCallExpression($functionDescription, $params) |
||
363 | } |
||
364 | } |
||
366 |