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 = array( |
||
| 49 | 'viewing' => 'show', |
||
| 50 | 'creation' => 'create', |
||
| 51 | 'editing' => 'update', |
||
| 52 | 'building' => 'build', |
||
| 53 | ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var KernelInterface |
||
| 57 | */ |
||
| 58 | private $kernel; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var KernelInterface |
||
| 62 | */ |
||
| 63 | private static $sharedKernel; |
||
| 64 | |||
| 65 | public function __construct($applicationName = null) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function setKernel(KernelInterface $kernel) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $type |
||
| 91 | * @param string $name |
||
| 92 | * |
||
| 93 | * @return object |
||
| 94 | */ |
||
| 95 | protected function findOneByName($type, $name) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $type |
||
| 102 | * @param array $criteria |
||
| 103 | * |
||
| 104 | * @return object |
||
| 105 | * |
||
| 106 | * @throws \InvalidArgumentException |
||
| 107 | */ |
||
| 108 | protected function findOneBy($type, array $criteria) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $resourceName |
||
| 126 | * |
||
| 127 | * @return RepositoryInterface |
||
| 128 | */ |
||
| 129 | protected function getRepository($resourceName) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $resourceName |
||
| 136 | * |
||
| 137 | * @return FactoryInterface |
||
| 138 | */ |
||
| 139 | protected function getFactory($resourceName) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return ObjectManager |
||
| 146 | */ |
||
| 147 | protected function getEntityManager() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return ContainerInterface |
||
| 154 | */ |
||
| 155 | protected function getContainer() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $id |
||
| 162 | * |
||
| 163 | * @return object |
||
| 164 | */ |
||
| 165 | protected function getService($id) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Configuration converter. |
||
| 172 | * |
||
| 173 | * @param string $configurationString |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | protected function getConfiguration($configurationString) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Generate page url. |
||
| 216 | * This method uses simple convention where page argument is prefixed |
||
| 217 | * with the application name and used as route name passed to router generate method. |
||
| 218 | * |
||
| 219 | * @param object|string $page |
||
| 220 | * @param array $parameters |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | protected function generatePageUrl($page, array $parameters = array()) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get current user instance. |
||
| 249 | * |
||
| 250 | * @return UserInterface|null |
||
| 251 | * |
||
| 252 | * @throws \Exception |
||
| 253 | */ |
||
| 254 | protected function getUser() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return TokenStorageInterface |
||
| 267 | */ |
||
| 268 | protected function getTokenStorage() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return AuthorizationCheckerInterface |
||
| 275 | */ |
||
| 276 | protected function getAuthorizationChecker() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $route |
||
| 283 | * @param array $parameters |
||
| 284 | * @param Boolean $absolute |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | protected function generateUrl($route, array $parameters = array(), $absolute = false) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Presses button with specified id|name|title|alt|value. |
||
| 295 | * |
||
| 296 | * @param string $button |
||
| 297 | * |
||
| 298 | * @throws ElementNotFoundException |
||
| 299 | */ |
||
| 300 | protected function pressButton($button) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Clicks link with specified id|title|alt|text. |
||
| 307 | * |
||
| 308 | * @param string $link |
||
| 309 | * |
||
| 310 | * @throws ElementNotFoundException |
||
| 311 | */ |
||
| 312 | protected function clickLink($link) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Fills in form field with specified id|name|label|value. |
||
| 319 | * |
||
| 320 | * @param string $field |
||
| 321 | * @param string $value |
||
| 322 | * |
||
| 323 | * @throws ElementNotFoundException |
||
| 324 | */ |
||
| 325 | protected function fillField($field, $value) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Selects option in select field with specified id|name|label|value. |
||
| 335 | * |
||
| 336 | * @param string $select |
||
| 337 | * @param string $option |
||
| 338 | * |
||
| 339 | * @throws ElementNotFoundException |
||
| 340 | */ |
||
| 341 | protected function selectOption($select, $option) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Returns fixed step argument (with \\" replaced back to "). |
||
| 351 | * |
||
| 352 | * @param string $argument |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | protected function fixStepArgument($argument) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param NodeElement $table |
||
| 363 | * @param string $columnName |
||
| 364 | * |
||
| 365 | * @return integer |
||
| 366 | * |
||
| 367 | * @throws \Exception If column was not found |
||
| 368 | */ |
||
| 369 | protected function getColumnIndex(NodeElement $table, $columnName) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param NodeElement $table |
||
| 392 | * @param array $fields |
||
| 393 | * |
||
| 394 | * @return NodeElement|null |
||
| 395 | * |
||
| 396 | * @throws \Exception If column was not found |
||
| 397 | */ |
||
| 398 | protected function getRowWithFields(NodeElement $table, array $fields) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param NodeElement $table |
||
| 411 | * @param array $fields |
||
| 412 | * @param boolean $onlyFirstOccurence |
||
| 413 | * |
||
| 414 | * @return NodeElement[] |
||
| 415 | * |
||
| 416 | * @throws \Exception If columns or rows were not found |
||
| 417 | */ |
||
| 418 | protected function getRowsWithFields(NodeElement $table, array $fields, $onlyFirstOccurence = false) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param NodeElement $table |
||
| 471 | * @param string[] $fields |
||
| 472 | * |
||
| 473 | * @return string[] |
||
| 474 | * |
||
| 475 | * @throws \Exception |
||
| 476 | */ |
||
| 477 | protected function replaceColumnNamesWithColumnIds(NodeElement $table, array $fields) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param callable $callback |
||
| 491 | * @param int $limit |
||
| 492 | * @param int $delay In miliseconds |
||
| 493 | * |
||
| 494 | * @return mixed |
||
| 495 | * |
||
| 496 | * @throws \RuntimeException If timeout was reached |
||
| 497 | */ |
||
| 498 | protected function waitFor(callable $callback, $limit = 30, $delay = 100) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $name |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | * |
||
| 518 | * @throws \InvalidArgumentException If name is not found in country code registry. |
||
| 519 | */ |
||
| 520 | protected function getCountryCodeByEnglishCountryName($name) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $name |
||
| 536 | * |
||
| 537 | * @return string |
||
| 538 | * |
||
| 539 | * @throws \InvalidArgumentException If name is not found in locale code registry. |
||
| 540 | */ |
||
| 541 | protected function getLocaleCodeByEnglishLocaleName($name) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return RouterInterface |
||
| 557 | */ |
||
| 558 | protected function getRouter() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return KernelInterface |
||
| 565 | */ |
||
| 566 | protected function getKernel() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return KernelInterface |
||
| 573 | */ |
||
| 574 | protected function getSharedKernel() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $id |
||
| 581 | * |
||
| 582 | * @return object |
||
| 583 | */ |
||
| 584 | protected function getSharedService($id) |
||
| 588 | } |
||
| 589 |