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 AdvancedEditorData 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 AdvancedEditorData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class AdvancedEditorData |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * @var string $namespace The graphql namespace/prefix. |
||
40 | */ |
||
41 | protected $namespace = 'Espresso'; |
||
42 | |||
43 | /** |
||
44 | * @var EE_Event |
||
45 | */ |
||
46 | protected $event; |
||
47 | |||
48 | /** |
||
49 | * @var EE_Admin_Config |
||
50 | */ |
||
51 | protected $admin_config; |
||
52 | /** |
||
53 | * @var EEM_Datetime $datetime_model |
||
54 | */ |
||
55 | protected $datetime_model; |
||
56 | /** |
||
57 | * @var EEM_Price $price_model |
||
58 | */ |
||
59 | protected $price_model; |
||
60 | /** |
||
61 | * @var EEM_Ticket $ticket_model |
||
62 | */ |
||
63 | protected $ticket_model; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * AdvancedEditorAdminForm constructor. |
||
68 | * |
||
69 | * @param EE_Event $event |
||
70 | * @param EE_Admin_Config $admin_config |
||
71 | * @param EEM_Datetime $datetime_model |
||
72 | * @param EEM_Price $price_model |
||
73 | * @param EEM_Ticket $ticket_model |
||
74 | */ |
||
75 | public function __construct( |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @throws EE_Error |
||
93 | * @throws InvalidArgumentException |
||
94 | * @throws InvalidDataTypeException |
||
95 | * @throws InvalidInterfaceException |
||
96 | * @throws ModelConfigurationException |
||
97 | * @throws ReflectionException |
||
98 | * @throws UnexpectedEntityException |
||
99 | * @throws DomainException |
||
100 | * @since $VID:$ |
||
101 | */ |
||
102 | public function loadScriptsStyles() |
||
131 | |||
132 | |||
133 | /** |
||
134 | * @param int $eventId |
||
135 | * @return array |
||
136 | * @throws EE_Error |
||
137 | * @throws InvalidDataTypeException |
||
138 | * @throws InvalidInterfaceException |
||
139 | * @throws ModelConfigurationException |
||
140 | * @throws UnexpectedEntityException |
||
141 | * @throws InvalidArgumentException |
||
142 | * @throws ReflectionException |
||
143 | * @throws DomainException |
||
144 | * @since $VID:$ |
||
145 | */ |
||
146 | protected function getEditorData($eventId) |
||
147 | { |
||
148 | $event = $this->getEventGraphQLData($eventId); |
||
149 | $event['dbId'] = $eventId; |
||
150 | |||
151 | $graphqlEndpoint = class_exists('WPGraphQL') ? trailingslashit(site_url()) . Router::$route : ''; |
||
152 | $graphqlEndpoint = esc_url($graphqlEndpoint); |
||
153 | |||
154 | $currentUser = $this->getGraphQLCurrentUser(); |
||
155 | |||
156 | $generalSettings = $this->getGraphQLGeneralSettings(); |
||
157 | |||
158 | $i18n = self::getJedLocaleData('event_espresso'); |
||
159 | |||
160 | $assetsUrl = EE_PLUGIN_DIR_URL . 'assets/dist/'; |
||
161 | |||
162 | return compact('event', 'graphqlEndpoint', 'currentUser', 'generalSettings', 'i18n', 'assetsUrl'); |
||
163 | } |
||
164 | |||
165 | |||
166 | /** |
||
167 | * @param int $eventId |
||
168 | * @return array |
||
169 | * @since $VID:$ |
||
170 | */ |
||
171 | protected function getEventGraphQLData($eventId) |
||
172 | { |
||
173 | $datetimes = $this->getGraphQLDatetimes($eventId); |
||
174 | |||
175 | // Avoid undefined variable warning in PHP >= 7.3 |
||
176 | $tickets = null; |
||
177 | $prices = null; |
||
178 | |||
179 | if (empty($datetimes['nodes']) || (isset($_REQUEST['action']) && $_REQUEST['action'] === 'create_new')) { |
||
180 | $this->addDefaultEntities($eventId); |
||
181 | $datetimes = $this->getGraphQLDatetimes($eventId); |
||
182 | } |
||
183 | |||
184 | if (! empty($datetimes['nodes'])) { |
||
185 | $datetimeIn = wp_list_pluck($datetimes['nodes'], 'id'); |
||
186 | |||
187 | if (! empty($datetimeIn)) { |
||
188 | $tickets = $this->getGraphQLTickets($datetimeIn); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | if (! empty($tickets['nodes'])) { |
||
193 | $ticketIn = wp_list_pluck($tickets['nodes'], 'id'); |
||
194 | |||
195 | if (! empty($ticketIn)) { |
||
196 | $prices = $this->getGraphQLPrices($ticketIn); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | $priceTypes = $this->getGraphQLPriceTypes(); |
||
201 | |||
202 | $relations = $this->getRelationalData($eventId); |
||
203 | |||
204 | return compact('datetimes', 'tickets', 'prices', 'priceTypes', 'relations'); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param int $eventId |
||
209 | * @throws DomainException |
||
210 | * @throws EE_Error |
||
211 | * @throws InvalidArgumentException |
||
212 | * @throws InvalidDataTypeException |
||
213 | * @throws InvalidInterfaceException |
||
214 | * @throws ModelConfigurationException |
||
215 | * @throws ReflectionException |
||
216 | * @throws UnexpectedEntityException |
||
217 | * @since $VID:$ |
||
218 | */ |
||
219 | protected function addDefaultEntities($eventId) |
||
220 | { |
||
221 | $default_dates = $this->datetime_model->create_new_blank_datetime(); |
||
222 | if (is_array($default_dates) && isset($default_dates[0]) && $default_dates[0] instanceof EE_Datetime) { |
||
223 | $default_date = $default_dates[0]; |
||
224 | $default_date->save(); |
||
225 | $default_date->_add_relation_to($eventId, 'Event'); |
||
226 | $default_tickets = $this->ticket_model->get_all_default_tickets(); |
||
227 | $default_prices = $this->price_model->get_all_default_prices(); |
||
228 | foreach ($default_tickets as $default_ticket) { |
||
229 | $default_ticket->save(); |
||
230 | $default_ticket->_add_relation_to($default_date, 'Datetime'); |
||
231 | foreach ($default_prices as $default_price) { |
||
232 | $default_price->save(); |
||
233 | $default_price->_add_relation_to($default_ticket, 'Ticket'); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | |||
239 | |||
240 | /** |
||
241 | * @param int $eventId |
||
242 | * @return array|null |
||
243 | * @since $VID:$ |
||
244 | */ |
||
245 | View Code Duplication | protected function getGraphQLDatetimes($eventId) |
|
246 | { |
||
247 | $field_key = lcfirst($this->namespace) . 'Datetimes'; |
||
248 | $query = <<<QUERY |
||
249 | query GET_DATETIMES(\$where: {$this->namespace}RootQueryDatetimesConnectionWhereArgs, \$first: Int, \$last: Int ) { |
||
250 | {$field_key}(where: \$where, first: \$first, last: \$last) { |
||
251 | nodes { |
||
252 | id |
||
253 | dbId |
||
254 | cacheId |
||
255 | capacity |
||
256 | description |
||
257 | endDate |
||
258 | isActive |
||
259 | isExpired |
||
260 | isPrimary |
||
261 | isSoldOut |
||
262 | isTrashed |
||
263 | isUpcoming |
||
264 | length |
||
265 | name |
||
266 | order |
||
267 | reserved |
||
268 | sold |
||
269 | status |
||
270 | startDate |
||
271 | __typename |
||
272 | } |
||
273 | __typename |
||
274 | } |
||
275 | } |
||
276 | QUERY; |
||
277 | $data = [ |
||
278 | 'operation_name' => 'GET_DATETIMES', |
||
279 | 'variables' => [ |
||
280 | 'first' => 100, |
||
281 | 'where' => [ |
||
282 | 'eventId' => $eventId, |
||
283 | ], |
||
284 | ], |
||
285 | 'query' => $query, |
||
286 | ]; |
||
287 | |||
288 | $responseData = $this->makeGraphQLRequest($data); |
||
289 | return !empty($responseData[ $field_key ]) ? $responseData[ $field_key ] : null; |
||
290 | } |
||
291 | |||
292 | |||
293 | /** |
||
294 | * @param array $datetimeIn |
||
295 | * @return array|null |
||
296 | * @since $VID:$ |
||
297 | */ |
||
298 | View Code Duplication | protected function getGraphQLTickets(array $datetimeIn) |
|
299 | { |
||
300 | $field_key = lcfirst($this->namespace) . 'Tickets'; |
||
301 | $query = <<<QUERY |
||
302 | query GET_TICKETS(\$where: {$this->namespace}RootQueryTicketsConnectionWhereArgs, \$first: Int, \$last: Int ) { |
||
303 | {$field_key}(where: \$where, first: \$first, last: \$last) { |
||
304 | nodes { |
||
305 | id |
||
306 | cacheId |
||
307 | dbId |
||
308 | description |
||
309 | endDate |
||
310 | isDefault |
||
311 | isExpired |
||
312 | isFree |
||
313 | isOnSale |
||
314 | isPending |
||
315 | isRequired |
||
316 | isSoldOut |
||
317 | isTaxable |
||
318 | isTrashed |
||
319 | max |
||
320 | min |
||
321 | name |
||
322 | order |
||
323 | price |
||
324 | quantity |
||
325 | registrationCount |
||
326 | reserved |
||
327 | reverseCalculate |
||
328 | sold |
||
329 | startDate |
||
330 | uses |
||
331 | __typename |
||
332 | } |
||
333 | __typename |
||
334 | } |
||
335 | } |
||
336 | QUERY; |
||
337 | $data = [ |
||
338 | 'operation_name' => 'GET_TICKETS', |
||
339 | 'variables' => [ |
||
340 | 'first' => 100, |
||
341 | 'where' => [ |
||
342 | 'datetimeIn' => $datetimeIn, |
||
343 | ], |
||
344 | ], |
||
345 | 'query' => $query, |
||
346 | ]; |
||
347 | |||
348 | $responseData = $this->makeGraphQLRequest($data); |
||
349 | return !empty($responseData[ $field_key ]) ? $responseData[ $field_key ] : null; |
||
350 | } |
||
351 | |||
352 | |||
353 | /** |
||
354 | * @param array $ticketIn |
||
355 | * @return array|null |
||
356 | * @since $VID:$ |
||
357 | */ |
||
358 | View Code Duplication | protected function getGraphQLPrices(array $ticketIn) |
|
359 | { |
||
360 | $field_key = lcfirst($this->namespace) . 'Prices'; |
||
361 | $query = <<<QUERY |
||
362 | query GET_PRICES(\$where: {$this->namespace}RootQueryPricesConnectionWhereArgs, \$first: Int, \$last: Int ) { |
||
363 | {$field_key}(where: \$where, first: \$first, last: \$last) { |
||
364 | nodes { |
||
365 | id |
||
366 | dbId |
||
367 | amount |
||
368 | cacheId |
||
369 | desc |
||
370 | isBasePrice |
||
371 | isDefault |
||
372 | isDiscount |
||
373 | isPercent |
||
374 | isTax |
||
375 | isTrashed |
||
376 | name |
||
377 | order |
||
378 | overrides |
||
379 | __typename |
||
380 | } |
||
381 | __typename |
||
382 | } |
||
383 | } |
||
384 | QUERY; |
||
385 | $data = [ |
||
386 | 'operation_name' => 'GET_PRICES', |
||
387 | 'variables' => [ |
||
388 | 'first' => 100, |
||
389 | 'where' => [ |
||
390 | 'ticketIn' => $ticketIn, |
||
391 | ], |
||
392 | ], |
||
393 | 'query' => $query, |
||
394 | ]; |
||
395 | |||
396 | $responseData = $this->makeGraphQLRequest($data); |
||
397 | return !empty($responseData[ $field_key ]) ? $responseData[ $field_key ] : null; |
||
398 | } |
||
399 | |||
400 | |||
401 | /** |
||
402 | * @return array|null |
||
403 | * @since $VID:$ |
||
404 | */ |
||
405 | protected function getGraphQLPriceTypes() |
||
406 | { |
||
407 | $field_key = lcfirst($this->namespace) . 'PriceTypes'; |
||
408 | $query = <<<QUERY |
||
409 | query GET_PRICE_TYPES(\$first: Int, \$last: Int ) { |
||
410 | {$field_key}(first: \$first, last: \$last) { |
||
411 | nodes { |
||
412 | id |
||
413 | dbId |
||
414 | baseType |
||
415 | cacheId |
||
416 | isBasePrice |
||
417 | isDiscount |
||
418 | isPercent |
||
419 | isTax |
||
420 | isTrashed |
||
421 | name |
||
422 | order |
||
423 | __typename |
||
424 | } |
||
425 | __typename |
||
426 | } |
||
427 | } |
||
428 | QUERY; |
||
429 | $data = [ |
||
430 | 'operation_name' => 'GET_PRICE_TYPES', |
||
431 | 'variables' => [ |
||
432 | 'first' => 100, |
||
433 | ], |
||
434 | 'query' => $query, |
||
435 | ]; |
||
436 | |||
437 | $responseData = $this->makeGraphQLRequest($data); |
||
438 | return !empty($responseData[ $field_key ]) ? $responseData[ $field_key ] : null; |
||
439 | } |
||
440 | |||
441 | |||
442 | /** |
||
443 | * @return array|null |
||
444 | * @since $VID:$ |
||
445 | */ |
||
446 | View Code Duplication | protected function getGraphQLCurrentUser() |
|
475 | |||
476 | |||
477 | /** |
||
478 | * @return array|null |
||
479 | * @since $VID:$ |
||
480 | */ |
||
481 | View Code Duplication | protected function getGraphQLGeneralSettings() |
|
502 | |||
503 | |||
504 | /** |
||
505 | * @param array $data |
||
506 | * @return array |
||
507 | * @since $VID:$ |
||
508 | */ |
||
509 | protected function makeGraphQLRequest($data) |
||
522 | |||
523 | |||
524 | /** |
||
525 | * @param mixed $source The source that's passed down the GraphQL queries |
||
526 | * @param array $args The inputArgs on the field |
||
527 | * @param AppContext $context The AppContext passed down the GraphQL tree |
||
528 | * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
||
529 | * @return string |
||
530 | * @throws EE_Error |
||
531 | * @throws Exception |
||
532 | * @throws InvalidArgumentException |
||
533 | * @throws InvalidDataTypeException |
||
534 | * @throws InvalidInterfaceException |
||
535 | * @throws ReflectionException |
||
536 | * @throws UserError |
||
537 | * @throws UnexpectedEntityException |
||
538 | * @since $VID:$ |
||
539 | */ |
||
540 | public static function getRelationalData($eventId) |
||
629 | |||
630 | /** |
||
631 | * Convert the DB ID into GID |
||
632 | * |
||
633 | * @param string $type |
||
634 | * @param int|int[] $ID |
||
635 | * @return mixed |
||
636 | */ |
||
637 | public static function convertToGlobalId($type, $ID) |
||
646 | |||
647 | |||
648 | /** |
||
649 | * Returns Jed-formatted localization data. |
||
650 | * |
||
651 | * @param string $domain Translation domain. |
||
652 | * @return array |
||
653 | */ |
||
654 | View Code Duplication | public static function getJedLocaleData($domain) |
|
675 | } |
||
676 |