Complex classes like DefaultContext 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 DefaultContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | abstract class DefaultContext extends RawMinkContext implements Context, KernelAwareContext |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $applicationName = 'sylius'; |
||
39 | |||
40 | /** |
||
41 | * @var Generator |
||
42 | */ |
||
43 | protected $faker; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $actions = [ |
||
49 | 'viewing' => 'show', |
||
50 | 'creation' => 'create', |
||
51 | 'editing' => 'update', |
||
52 | 'building' => 'build', |
||
53 | 'customization' => 'customize', |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * @var KernelInterface |
||
58 | */ |
||
59 | private $kernel; |
||
60 | |||
61 | /** |
||
62 | * @var KernelInterface |
||
63 | */ |
||
64 | private static $sharedKernel; |
||
65 | |||
66 | public function __construct($applicationName = null) |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function setKernel(KernelInterface $kernel) |
||
89 | |||
90 | /** |
||
91 | * @param string $type |
||
92 | * @param string $name |
||
93 | * |
||
94 | * @return object |
||
95 | */ |
||
96 | protected function findOneByName($type, $name) |
||
97 | { |
||
98 | $resource = $this->getRepository($type)->findOneByName(trim($name)); |
||
99 | |||
100 | if (null === $resource) { |
||
101 | throw new \InvalidArgumentException( |
||
102 | sprintf('%s with name "%s" was not found.', str_replace('_', ' ', ucfirst($type)), $name) |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | return $resource; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param string $type |
||
111 | * @param array $criteria |
||
112 | * |
||
113 | * @return object |
||
114 | * |
||
115 | * @throws \InvalidArgumentException |
||
116 | */ |
||
117 | protected function findOneBy($type, array $criteria) |
||
132 | |||
133 | /** |
||
134 | * @param string $resourceName |
||
135 | * |
||
136 | * @return RepositoryInterface |
||
137 | */ |
||
138 | protected function getRepository($resourceName) |
||
142 | |||
143 | /** |
||
144 | * @param string $resourceName |
||
145 | * |
||
146 | * @return FactoryInterface |
||
147 | */ |
||
148 | protected function getFactory($resourceName) |
||
152 | |||
153 | /** |
||
154 | * @return ObjectManager |
||
155 | */ |
||
156 | protected function getEntityManager() |
||
160 | |||
161 | /** |
||
162 | * @return ContainerInterface |
||
163 | */ |
||
164 | protected function getContainer() |
||
168 | |||
169 | /** |
||
170 | * @param string $id |
||
171 | * |
||
172 | * @return object |
||
173 | */ |
||
174 | protected function getService($id) |
||
178 | |||
179 | /** |
||
180 | * Configuration converter. |
||
181 | * |
||
182 | * @param string $configurationString |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | protected function getConfiguration($configurationString) |
||
226 | |||
227 | /** |
||
228 | * Generate page url. |
||
229 | * This method uses simple convention where page argument is prefixed |
||
230 | * with the application name and used as route name passed to router generate method. |
||
231 | * |
||
232 | * @param object|string $page |
||
233 | * @param array $parameters |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | protected function generatePageUrl($page, array $parameters = []) |
||
259 | |||
260 | /** |
||
261 | * Get current user instance. |
||
262 | * |
||
263 | * @return UserInterface|null |
||
264 | * |
||
265 | * @throws \Exception |
||
266 | */ |
||
267 | protected function getUser() |
||
277 | |||
278 | /** |
||
279 | * @return TokenStorageInterface |
||
280 | */ |
||
281 | protected function getTokenStorage() |
||
285 | |||
286 | /** |
||
287 | * @return AuthorizationCheckerInterface |
||
288 | */ |
||
289 | protected function getAuthorizationChecker() |
||
293 | |||
294 | /** |
||
295 | * @param string $route |
||
296 | * @param array $parameters |
||
297 | * @param bool $absolute |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | protected function generateUrl($route, array $parameters = [], $absolute = false) |
||
305 | |||
306 | /** |
||
307 | * Presses button with specified id|name|title|alt|value. |
||
308 | * |
||
309 | * @param string $button |
||
310 | * |
||
311 | * @throws ElementNotFoundException |
||
312 | */ |
||
313 | protected function pressButton($button) |
||
317 | |||
318 | /** |
||
319 | * Clicks link with specified id|title|alt|text. |
||
320 | * |
||
321 | * @param string $link |
||
322 | * |
||
323 | * @throws ElementNotFoundException |
||
324 | */ |
||
325 | protected function clickLink($link) |
||
329 | |||
330 | /** |
||
331 | * Fills in form field with specified id|name|label|value. |
||
332 | * |
||
333 | * @param string $field |
||
334 | * @param string $value |
||
335 | * |
||
336 | * @throws ElementNotFoundException |
||
337 | */ |
||
338 | protected function fillField($field, $value) |
||
345 | |||
346 | /** |
||
347 | * Selects option in select field with specified id|name|label|value. |
||
348 | * |
||
349 | * @param string $select |
||
350 | * @param string $option |
||
351 | * |
||
352 | * @throws ElementNotFoundException |
||
353 | */ |
||
354 | protected function selectOption($select, $option) |
||
361 | |||
362 | /** |
||
363 | * Returns fixed step argument (with \\" replaced back to "). |
||
364 | * |
||
365 | * @param string $argument |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | protected function fixStepArgument($argument) |
||
373 | |||
374 | /** |
||
375 | * @param NodeElement $table |
||
376 | * @param string $columnName |
||
377 | * |
||
378 | * @return int |
||
379 | * |
||
380 | * @throws \Exception If column was not found |
||
381 | */ |
||
382 | protected function getColumnIndex(NodeElement $table, $columnName) |
||
402 | |||
403 | /** |
||
404 | * @param NodeElement $table |
||
405 | * @param array $fields |
||
406 | * |
||
407 | * @return NodeElement|null |
||
408 | * |
||
409 | * @throws \Exception If column was not found |
||
410 | */ |
||
411 | protected function getRowWithFields(NodeElement $table, array $fields) |
||
421 | |||
422 | /** |
||
423 | * @param NodeElement $table |
||
424 | * @param array $fields |
||
425 | * @param bool $onlyFirstOccurence |
||
426 | * |
||
427 | * @return NodeElement[] |
||
428 | * |
||
429 | * @throws \Exception If columns or rows were not found |
||
430 | */ |
||
431 | protected function getRowsWithFields(NodeElement $table, array $fields, $onlyFirstOccurence = false) |
||
481 | |||
482 | /** |
||
483 | * @param NodeElement $table |
||
484 | * @param string[] $fields |
||
485 | * |
||
486 | * @return string[] |
||
487 | * |
||
488 | * @throws \Exception |
||
489 | */ |
||
490 | protected function replaceColumnNamesWithColumnIds(NodeElement $table, array $fields) |
||
501 | |||
502 | /** |
||
503 | * @param callable $callback |
||
504 | * @param int $limit |
||
505 | * @param int $delay In miliseconds |
||
506 | * |
||
507 | * @return mixed |
||
508 | * |
||
509 | * @throws \RuntimeException If timeout was reached |
||
510 | */ |
||
511 | protected function waitFor(callable $callback, $limit = 30, $delay = 100) |
||
525 | |||
526 | /** |
||
527 | * @param string $name |
||
528 | * |
||
529 | * @return string |
||
530 | * |
||
531 | * @throws \InvalidArgumentException If name is not found in country code registry. |
||
532 | */ |
||
533 | protected function getCountryCodeByEnglishCountryName($name) |
||
546 | |||
547 | /** |
||
548 | * @param string $name |
||
549 | * |
||
550 | * @return string |
||
551 | * |
||
552 | * @throws \InvalidArgumentException If name is not found in locale code registry. |
||
553 | */ |
||
554 | protected function getLocaleCodeByEnglishLocaleName($name) |
||
560 | |||
561 | /** |
||
562 | * @return RouterInterface |
||
563 | */ |
||
564 | protected function getRouter() |
||
568 | |||
569 | /** |
||
570 | * @return KernelInterface |
||
571 | */ |
||
572 | protected function getKernel() |
||
576 | |||
577 | /** |
||
578 | * @return KernelInterface |
||
579 | */ |
||
580 | protected function getSharedKernel() |
||
584 | |||
585 | /** |
||
586 | * @param string $id |
||
587 | * |
||
588 | * @return object |
||
589 | */ |
||
590 | protected function getSharedService($id) |
||
594 | } |
||
595 |