Total Complexity | 58 |
Total Lines | 349 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PHPExpressionProvider 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 PHPExpressionProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class PHPExpressionProvider implements IExpressionProvider |
||
19 | { |
||
20 | const ADD = '+'; |
||
21 | const CLOSE_BRACKET = ')'; |
||
22 | const COMMA = ','; |
||
23 | const DIVIDE = '/'; |
||
24 | const SUBTRACT = '-'; |
||
25 | const EQUAL = '=='; |
||
26 | const GREATER_THAN = '>'; |
||
27 | const GREATER_THAN_OR_EQUAL = '>='; |
||
28 | const LESS_THAN = '<'; |
||
29 | const LESS_THAN_OR_EQUAL = '<='; |
||
30 | const LOGICAL_AND = '&&'; |
||
31 | const LOGICAL_NOT = '!'; |
||
32 | const LOGICAL_OR = '||'; |
||
33 | const MEMBER_ACCESS = '->'; |
||
34 | const MODULO = '%'; |
||
35 | const MULTIPLY = '*'; |
||
36 | const NEGATE = '-'; |
||
37 | const NOT_EQUAL = '!='; |
||
38 | const OPEN_BRACKET = '('; |
||
39 | const TYPE_NAMESPACE = 'POData\\Providers\\Metadata\\Type\\'; |
||
40 | |||
41 | /** |
||
42 | * The name of iterator. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $iteratorName; |
||
47 | |||
48 | /** |
||
49 | * The type of the resource pointed by the resource path segment. |
||
50 | * |
||
51 | * @var ResourceType |
||
52 | */ |
||
53 | private $resourceType; |
||
54 | |||
55 | /** |
||
56 | * @param string $iteratorName The name of the iterator |
||
57 | */ |
||
58 | public function __construct($iteratorName) |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * call-back for setting the resource type. |
||
65 | * |
||
66 | * @param ResourceType $resourceType The resource type on which the filter is going to be applied |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | public function setResourceType(ResourceType $resourceType) |
||
71 | { |
||
72 | $this->resourceType = $resourceType; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Call-back for logical expression. |
||
77 | * |
||
78 | * @param ExpressionType $expressionType The type of logical expression |
||
79 | * @param string $left The left expression |
||
80 | * @param string $right The left expression |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function onLogicalExpression(ExpressionType $expressionType, $left, $right) |
||
85 | { |
||
86 | assert($expressionType instanceof ExpressionType, get_class($expressionType)); |
||
87 | switch ($expressionType) { |
||
88 | case ExpressionType::AND_LOGICAL(): |
||
89 | return $this->prepareBinaryExpression(self::LOGICAL_AND, $left, $right); |
||
90 | |||
91 | case ExpressionType::OR_LOGICAL(): |
||
92 | return $this->prepareBinaryExpression(self::LOGICAL_OR, $left, $right); |
||
93 | |||
94 | default: |
||
95 | throw new InvalidArgumentException('onLogicalExpression'); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * To format binary expression. |
||
101 | * |
||
102 | * @param string $operator The binary operator |
||
103 | * @param string $left The left operand |
||
104 | * @param string $right The right operand |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | private function prepareBinaryExpression($operator, $left, $right) |
||
109 | { |
||
110 | return |
||
111 | self::OPEN_BRACKET . $left . ' ' . $operator . ' ' . $right . self::CLOSE_BRACKET; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Call-back for arithmetic expression. |
||
116 | * |
||
117 | * @param ExpressionType $expressionType The type of arithmetic expression |
||
118 | * @param string $left The left expression |
||
119 | * @param string $right The left expression |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function onArithmeticExpression(ExpressionType $expressionType, $left, $right) |
||
124 | { |
||
125 | switch ($expressionType) { |
||
126 | case ExpressionType::MULTIPLY(): |
||
127 | return $this->prepareBinaryExpression(self::MULTIPLY, $left, $right); |
||
128 | |||
129 | case ExpressionType::DIVIDE(): |
||
130 | return $this->prepareBinaryExpression(self::DIVIDE, $left, $right); |
||
131 | |||
132 | case ExpressionType::MODULO(): |
||
133 | return $this->prepareBinaryExpression(self::MODULO, $left, $right); |
||
134 | |||
135 | case ExpressionType::ADD(): |
||
136 | return $this->prepareBinaryExpression(self::ADD, $left, $right); |
||
137 | |||
138 | case ExpressionType::SUBTRACT(): |
||
139 | return $this->prepareBinaryExpression(self::SUBTRACT, $left, $right); |
||
140 | |||
141 | default: |
||
142 | throw new InvalidArgumentException('onArithmeticExpression'); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Call-back for relational expression. |
||
148 | * |
||
149 | * @param ExpressionType $expressionType The type of relation expression |
||
150 | * @param string $left The left expression |
||
151 | * @param string $right The left expression |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function onRelationalExpression(ExpressionType $expressionType, $left, $right) |
||
156 | { |
||
157 | switch ($expressionType) { |
||
158 | case ExpressionType::GREATERTHAN(): |
||
159 | return $this->prepareBinaryExpression(self::GREATER_THAN, $left, $right); |
||
160 | |||
161 | case ExpressionType::GREATERTHAN_OR_EQUAL(): |
||
162 | return $this->prepareBinaryExpression(self::GREATER_THAN_OR_EQUAL, $left, $right); |
||
163 | |||
164 | case ExpressionType::LESSTHAN(): |
||
165 | return $this->prepareBinaryExpression(self::LESS_THAN, $left, $right); |
||
166 | |||
167 | case ExpressionType::LESSTHAN_OR_EQUAL(): |
||
168 | return $this->prepareBinaryExpression(self::LESS_THAN_OR_EQUAL, $left, $right); |
||
169 | |||
170 | case ExpressionType::EQUAL(): |
||
171 | return $this->prepareBinaryExpression(self::EQUAL, $left, $right); |
||
172 | |||
173 | case ExpressionType::NOTEQUAL(): |
||
174 | return $this->prepareBinaryExpression(self::NOT_EQUAL, $left, $right); |
||
175 | |||
176 | default: |
||
177 | throw new InvalidArgumentException('onRelationalExpression'); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Call-back for unary expression. |
||
183 | * |
||
184 | * @param ExpressionType $expressionType The type of unary expression |
||
185 | * @param string $child The child expression |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function onUnaryExpression(ExpressionType $expressionType, $child) |
||
190 | { |
||
191 | switch ($expressionType) { |
||
192 | case ExpressionType::NEGATE(): |
||
193 | return $this->prepareUnaryExpression(self::NEGATE, $child); |
||
194 | |||
195 | case ExpressionType::NOT_LOGICAL(): |
||
196 | return $this->prepareUnaryExpression(self::LOGICAL_NOT, $child); |
||
197 | |||
198 | default: |
||
199 | throw new InvalidArgumentException('onUnaryExpression'); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * To format unary expression. |
||
205 | * |
||
206 | * @param string $operator The unary operator |
||
207 | * @param string $child The operand |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | private function prepareUnaryExpression($operator, $child) |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Call-back for constant expression. |
||
218 | * |
||
219 | * @param IType $type The type of constant |
||
220 | * @param mixed $value The value of the constant |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | public function onConstantExpression(IType $type, $value) |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Call-back for property access expression. |
||
237 | * |
||
238 | * @param PropertyAccessExpression $expression The property access expression |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public function onPropertyAccessExpression(PropertyAccessExpression $expression): string |
||
243 | { |
||
244 | if (null == $this->resourceType) { |
||
245 | throw new InvalidArgumentException('onPropertyAccessExpression - resourceType null'); |
||
246 | } |
||
247 | if (empty($this->resourceType->getName())) { |
||
248 | throw new InvalidArgumentException('onPropertyAccessExpression - resourceType has no name'); |
||
249 | } |
||
250 | |||
251 | $parent = $expression; |
||
252 | $variable = null; |
||
253 | |||
254 | do { |
||
255 | $variable = $parent->getResourceProperty()->getName() . self::MEMBER_ACCESS . $variable; |
||
256 | $parent = $parent->getParent(); |
||
257 | } while ($parent != null); |
||
258 | |||
259 | $variable = rtrim($variable, self::MEMBER_ACCESS); |
||
260 | $variable = $this->getIteratorName() . self::MEMBER_ACCESS . $variable; |
||
261 | |||
262 | return $variable; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Get the name of the iterator. |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | public function getIteratorName() |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Call-back for function call expression. |
||
277 | * |
||
278 | * @param FunctionDescription $functionDescription Description of the function |
||
279 | * @param array<string> $params Parameters to the function |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function onFunctionCallExpression($functionDescription, $params) |
||
370 |