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 RawDrupalContext 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 RawDrupalContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface |
||
| 30 | { |
||
| 31 | |||
| 32 | use DrupalParametersTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Drupal driver manager. |
||
| 36 | * |
||
| 37 | * @var \Drupal\DrupalDriverManager |
||
| 38 | */ |
||
| 39 | private $drupal; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Event dispatcher object. |
||
| 43 | * |
||
| 44 | * @var \Behat\Testwork\Hook\HookDispatcher |
||
| 45 | */ |
||
| 46 | protected $dispatcher; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Drupal authentication manager. |
||
| 50 | * |
||
| 51 | * @var \Drupal\DrupalExtension\Manager\DrupalAuthenticationManagerInterface |
||
| 52 | */ |
||
| 53 | protected $authenticationManager; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Drupal user manager. |
||
| 57 | * |
||
| 58 | * @var \Drupal\DrupalExtension\Manager\DrupalUserManagerInterface |
||
| 59 | */ |
||
| 60 | protected $userManager; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Keep track of nodes so they can be cleaned up. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $nodes = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Keep track of all terms that are created so they can easily be removed. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $terms = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Keep track of any roles that are created so they can easily be removed. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $roles = array(); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Keep track of any languages that are created so they can easily be removed. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $languages = array(); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritDoc} |
||
| 92 | */ |
||
| 93 | public function setDrupal(DrupalDriverManager $drupal) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritDoc} |
||
| 100 | */ |
||
| 101 | public function getDrupal() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritDoc} |
||
| 108 | */ |
||
| 109 | public function setUserManager(DrupalUserManagerInterface $userManager) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function getUserManager() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function setAuthenticationManager(DrupalAuthenticationManagerInterface $authenticationManager) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function getAuthenticationManager() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Magic setter. |
||
| 140 | */ |
||
| 141 | public function __set($name, $value) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Magic getter. |
||
| 168 | */ |
||
| 169 | public function __get($name) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function setDispatcher(HookDispatcher $dispatcher) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get active Drupal Driver. |
||
| 196 | * |
||
| 197 | * @return \Drupal\Driver\DrupalDriver |
||
| 198 | */ |
||
| 199 | public function getDriver($name = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get driver's random generator. |
||
| 206 | */ |
||
| 207 | public function getRandom() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Massage node values to match the expectations on different Drupal versions. |
||
| 214 | * |
||
| 215 | * @beforeNodeCreate |
||
| 216 | */ |
||
| 217 | public static function alterNodeParameters(BeforeNodeCreateScope $scope) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Remove any created nodes. |
||
| 243 | * |
||
| 244 | * @AfterScenario |
||
| 245 | */ |
||
| 246 | public function cleanNodes() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Remove any created users. |
||
| 257 | * |
||
| 258 | * @AfterScenario |
||
| 259 | */ |
||
| 260 | public function cleanUsers() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Remove any created terms. |
||
| 277 | * |
||
| 278 | * @AfterScenario |
||
| 279 | */ |
||
| 280 | public function cleanTerms() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Remove any created roles. |
||
| 291 | * |
||
| 292 | * @AfterScenario |
||
| 293 | */ |
||
| 294 | public function cleanRoles() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Remove any created languages. |
||
| 305 | * |
||
| 306 | * @AfterScenario |
||
| 307 | */ |
||
| 308 | public function cleanLanguages() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Clear static caches. |
||
| 319 | * |
||
| 320 | * @AfterScenario @api |
||
| 321 | */ |
||
| 322 | public function clearStaticCaches() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Dispatch scope hooks. |
||
| 329 | * |
||
| 330 | * @param string $scope |
||
| 331 | * The entity scope to dispatch. |
||
| 332 | * @param \stdClass $entity |
||
| 333 | * The entity. |
||
| 334 | */ |
||
| 335 | protected function dispatchHooks($scopeType, \stdClass $entity) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Create a node. |
||
| 352 | * |
||
| 353 | * @return object |
||
| 354 | * The created node. |
||
| 355 | */ |
||
| 356 | View Code Duplication | public function nodeCreate($node) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Parses the field values and turns them into the format expected by Drupal. |
||
| 368 | * |
||
| 369 | * Multiple values in a single field must be separated by commas. Wrap the |
||
| 370 | * field value in double quotes in case it should contain a comma. |
||
| 371 | * |
||
| 372 | * Compound field properties are identified using a ':' operator, either in |
||
| 373 | * the column heading or in the cell. If multiple properties are present in a |
||
| 374 | * single cell, they must be separated using ' - ', and values should not |
||
| 375 | * contain ':' or ' - '. |
||
| 376 | * |
||
| 377 | * Possible formats for the values: |
||
| 378 | * A |
||
| 379 | * A, B, "a value, containing a comma" |
||
| 380 | * A - B |
||
| 381 | * x: A - y: B |
||
| 382 | * A - B, C - D, "E - F" |
||
| 383 | * x: A - y: B, x: C - y: D, "x: E - y: F" |
||
| 384 | * |
||
| 385 | * See field_handlers.feature for examples of usage. |
||
| 386 | * |
||
| 387 | * @param string $entity_type |
||
| 388 | * The entity type. |
||
| 389 | * @param \stdClass $entity |
||
| 390 | * An object containing the entity properties and fields as properties. |
||
| 391 | * |
||
| 392 | * @throws \Exception |
||
| 393 | * Thrown when a field name is invalid. |
||
| 394 | */ |
||
| 395 | public function parseEntityFields($entity_type, \stdClass $entity) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Create a user. |
||
| 471 | * |
||
| 472 | * @return object |
||
| 473 | * The created user. |
||
| 474 | */ |
||
| 475 | public function userCreate($user) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Create a term. |
||
| 487 | * |
||
| 488 | * @return object |
||
| 489 | * The created term. |
||
| 490 | */ |
||
| 491 | View Code Duplication | public function termCreate($term) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Creates a language. |
||
| 503 | * |
||
| 504 | * @param \stdClass $language |
||
| 505 | * An object with the following properties: |
||
| 506 | * - langcode: the langcode of the language to create. |
||
| 507 | * |
||
| 508 | * @return object|FALSE |
||
| 509 | * The created language, or FALSE if the language was already created. |
||
| 510 | */ |
||
| 511 | public function languageCreate(\stdClass $language) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Log-in the given user. |
||
| 524 | * |
||
| 525 | * @param \stdClass $user |
||
| 526 | * The user to log in. |
||
| 527 | */ |
||
| 528 | public function login(\stdClass $user) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Logs the current user out. |
||
| 535 | * |
||
| 536 | * @param bool $fast |
||
| 537 | * Utilize direct logout by session if available. |
||
| 538 | */ |
||
| 539 | public function logout($fast = false) |
||
| 548 | /** |
||
| 549 | * Determine if the a user is already logged in. |
||
| 550 | * |
||
| 551 | * @return boolean |
||
| 552 | * Returns TRUE if a user is logged in for this session. |
||
| 553 | */ |
||
| 554 | public function loggedIn() |
||
| 555 | { |
||
| 559 | /** |
||
| 560 | * User with a given role is already logged in. |
||
| 561 | * |
||
| 562 | * @param string $role |
||
| 563 | * A single role, or multiple comma-separated roles in a single string. |
||
| 564 | * |
||
| 565 | * @return boolean |
||
| 566 | * Returns TRUE if the current logged in user has this role (or roles). |
||
| 567 | */ |
||
| 568 | public function loggedInWithRole($role) |
||
| 569 | { |
||
| 573 | /** |
||
| 574 | * Returns the Behat context that corresponds with the given class name. |
||
| 575 | * |
||
| 576 | * This is inspired by InitializedContextEnvironment::getContext() but also |
||
| 577 | * returns subclasses of the given class name. This allows us to retrieve for |
||
| 578 | * example DrupalContext even if it is overridden in a project. |
||
| 579 | * |
||
| 580 | * @param string $class |
||
| 581 | * A fully namespaced class name. |
||
| 582 | * |
||
| 583 | * @return \Behat\Behat\Context\Context|false |
||
| 584 | * The requested context, or FALSE if the context is not registered. |
||
| 585 | * |
||
| 586 | * @throws \Exception |
||
| 587 | * Thrown when the environment is not yet initialized, meaning that contexts |
||
| 588 | * cannot yet be retrieved. |
||
| 589 | */ |
||
| 590 | protected function getContext($class) |
||
| 591 | { |
||
| 611 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: