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 |
||
| 26 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Drupal driver manager. |
||
| 30 | * |
||
| 31 | * @var \Drupal\DrupalDriverManager |
||
| 32 | */ |
||
| 33 | private $drupal; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Test parameters. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $drupalParameters; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Event dispatcher object. |
||
| 44 | * |
||
| 45 | * @var \Behat\Testwork\Hook\HookDispatcher |
||
| 46 | */ |
||
| 47 | protected $dispatcher; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Keep track of nodes so they can be cleaned up. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $nodes = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Current authenticated user. |
||
| 58 | * |
||
| 59 | * A value of FALSE denotes an anonymous user. |
||
| 60 | * |
||
| 61 | * @var \stdClass|bool |
||
| 62 | */ |
||
| 63 | public $user = FALSE; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Keep track of all users that are created so they can easily be removed. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $users = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Keep track of all terms that are created so they can easily be removed. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $terms = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Keep track of any roles that are created so they can easily be removed. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $roles = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Keep track of any languages that are created so they can easily be removed. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $languages = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Keep track of any entities that are created so they can easily be removed. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $entities = array(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritDoc} |
||
| 102 | */ |
||
| 103 | public function setDrupal(DrupalDriverManager $drupal) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritDoc} |
||
| 109 | */ |
||
| 110 | public function getDrupal() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritDoc} |
||
| 116 | */ |
||
| 117 | public function setDispatcher(HookDispatcher $dispatcher) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set parameters provided for Drupal. |
||
| 123 | */ |
||
| 124 | public function setDrupalParameters(array $parameters) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns a specific Drupal parameter. |
||
| 130 | * |
||
| 131 | * @param string $name |
||
| 132 | * Parameter name. |
||
| 133 | * |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | public function getDrupalParameter($name) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns a specific Drupal text value. |
||
| 142 | * |
||
| 143 | * @param string $name |
||
| 144 | * Text value name, such as 'log_out', which corresponds to the default 'Log |
||
| 145 | * out' link text. |
||
| 146 | * @throws \Exception |
||
| 147 | * @return |
||
| 148 | */ |
||
| 149 | public function getDrupalText($name) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns a specific css selector. |
||
| 159 | * |
||
| 160 | * @param $name |
||
| 161 | * string CSS selector name |
||
| 162 | */ |
||
| 163 | public function getDrupalSelector($name) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get active Drupal Driver. |
||
| 173 | * |
||
| 174 | * @return \Drupal\Driver\DrupalDriver |
||
| 175 | */ |
||
| 176 | public function getDriver($name = NULL) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get driver's random generator. |
||
| 182 | */ |
||
| 183 | public function getRandom() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Massage node values to match the expectations on different Drupal versions. |
||
| 189 | * |
||
| 190 | * @beforeNodeCreate |
||
| 191 | */ |
||
| 192 | public function alterNodeParameters(BeforeNodeCreateScope $scope) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Remove any created nodes. |
||
| 217 | * |
||
| 218 | * @AfterScenario |
||
| 219 | */ |
||
| 220 | public function cleanNodes() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Remove any created users. |
||
| 230 | * |
||
| 231 | * @AfterScenario |
||
| 232 | */ |
||
| 233 | public function cleanUsers() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Remove any created terms. |
||
| 250 | * |
||
| 251 | * @AfterScenario |
||
| 252 | */ |
||
| 253 | public function cleanTerms() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Remove any created roles. |
||
| 263 | * |
||
| 264 | * @AfterScenario |
||
| 265 | */ |
||
| 266 | public function cleanRoles() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Remove any created languages. |
||
| 276 | * |
||
| 277 | * @AfterScenario |
||
| 278 | */ |
||
| 279 | public function cleanLanguages() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Clear static caches. |
||
| 289 | * |
||
| 290 | * @AfterScenario @api |
||
| 291 | */ |
||
| 292 | public function clearStaticCaches() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Dispatch scope hooks. |
||
| 298 | * |
||
| 299 | * @param string $scope |
||
| 300 | * The entity scope to dispatch. |
||
| 301 | * @param \stdClass $entity |
||
| 302 | * The entity. |
||
| 303 | */ |
||
| 304 | protected function dispatchHooks($scopeType, \stdClass $entity) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Create a node. |
||
| 320 | * |
||
| 321 | * @return object |
||
| 322 | * The created node. |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function nodeCreate($node) { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Parse multi-value fields. Possible formats: |
||
| 335 | * A, B, C |
||
| 336 | * A - B, C - D, E - F |
||
| 337 | * |
||
| 338 | * @param string $entity_type |
||
| 339 | * The entity type. |
||
| 340 | * @param \stdClass $entity |
||
| 341 | * An object containing the entity properties and fields as properties. |
||
| 342 | */ |
||
| 343 | public function parseEntityFields($entity_type, \stdClass $entity) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Create a user. |
||
| 413 | * |
||
| 414 | * @return object |
||
| 415 | * The created user. |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function userCreate($user) { |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Create a term. |
||
| 428 | * |
||
| 429 | * @return object |
||
| 430 | * The created term. |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function termCreate($term) { |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Creates a language. |
||
| 443 | * |
||
| 444 | * @param \stdClass $language |
||
| 445 | * An object with the following properties: |
||
| 446 | * - langcode: the langcode of the language to create. |
||
| 447 | * |
||
| 448 | * @return object|FALSE |
||
| 449 | * The created language, or FALSE if the language was already created. |
||
| 450 | */ |
||
| 451 | public function languageCreate(\stdClass $language) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Log-in the current user. |
||
| 463 | */ |
||
| 464 | public function login() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Logs the current user out. |
||
| 498 | */ |
||
| 499 | public function logout() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Determine if the a user is already logged in. |
||
| 505 | * |
||
| 506 | * @return boolean |
||
| 507 | * Returns TRUE if a user is logged in for this session. |
||
| 508 | */ |
||
| 509 | public function loggedIn() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * User with a given role is already logged in. |
||
| 540 | * |
||
| 541 | * @param string $role |
||
| 542 | * A single role, or multiple comma-separated roles in a single string. |
||
| 543 | * |
||
| 544 | * @return boolean |
||
| 545 | * Returns TRUE if the current logged in user has this role (or roles). |
||
| 546 | */ |
||
| 547 | public function loggedInWithRole($role) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Create an entity. |
||
| 553 | * |
||
| 554 | * @return object |
||
| 555 | * The created entity. |
||
| 556 | */ |
||
| 557 | public function entityCreate($entity_type, $entity) { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Remove any content entities created by entityCreate(), |
||
| 573 | * but not those created by nodeCreate(), termCreate() or userCreate(). |
||
| 574 | * |
||
| 575 | * @AfterScenario |
||
| 576 | */ |
||
| 577 | public function cleanEntities() { |
||
| 583 | |||
| 584 | } |
||
| 585 |
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: