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 |
||
31 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface |
||
32 | { |
||
33 | |||
34 | use DrupalParametersTrait; |
||
35 | |||
36 | /** |
||
37 | * Drupal driver manager. |
||
38 | * |
||
39 | * @var \Drupal\DrupalDriverManager |
||
40 | */ |
||
41 | private $drupal; |
||
42 | |||
43 | /** |
||
44 | * Event dispatcher object. |
||
45 | * |
||
46 | * @var \Behat\Testwork\Hook\HookDispatcher |
||
47 | */ |
||
48 | protected $dispatcher; |
||
49 | |||
50 | /** |
||
51 | * Drupal authentication manager. |
||
52 | * |
||
53 | * @var \Drupal\DrupalExtension\Manager\DrupalAuthenticationManagerInterface |
||
54 | */ |
||
55 | protected $authenticationManager; |
||
56 | |||
57 | /** |
||
58 | * Drupal user manager. |
||
59 | * |
||
60 | * @var \Drupal\DrupalExtension\Manager\DrupalUserManagerInterface |
||
61 | */ |
||
62 | protected $userManager; |
||
63 | |||
64 | /** |
||
65 | * Keep track of nodes so they can be cleaned up. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $nodes = array(); |
||
70 | |||
71 | /** |
||
72 | * Keep track of all terms that are created so they can easily be removed. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $terms = array(); |
||
77 | |||
78 | /** |
||
79 | * Keep track of any roles that are created so they can easily be removed. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $roles = array(); |
||
84 | |||
85 | /** |
||
86 | * Keep track of any languages that are created so they can easily be removed. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $languages = array(); |
||
91 | |||
92 | /** |
||
93 | * Keep track of any entities that are created so they can easily be removed. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $entities = array(); |
||
98 | |||
99 | /** |
||
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | public function setDrupal(DrupalDriverManager $drupal) |
||
106 | |||
107 | /** |
||
108 | * {@inheritDoc} |
||
109 | */ |
||
110 | public function getDrupal() |
||
114 | |||
115 | /** |
||
116 | * {@inheritDoc} |
||
117 | */ |
||
118 | public function setUserManager(DrupalUserManagerInterface $userManager) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function getUserManager() |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function setAuthenticationManager(DrupalAuthenticationManagerInterface $authenticationManager) |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function getAuthenticationManager() |
||
146 | |||
147 | /** |
||
148 | * Magic setter. |
||
149 | */ |
||
150 | public function __set($name, $value) |
||
174 | |||
175 | /** |
||
176 | * Magic getter. |
||
177 | */ |
||
178 | public function __get($name) |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function setDispatcher(HookDispatcher $dispatcher) |
||
202 | |||
203 | /** |
||
204 | * Get active Drupal Driver. |
||
205 | * |
||
206 | * @return \Drupal\Driver\DrupalDriver |
||
207 | */ |
||
208 | public function getDriver($name = null) |
||
212 | |||
213 | /** |
||
214 | * Get driver's random generator. |
||
215 | */ |
||
216 | public function getRandom() |
||
220 | |||
221 | /** |
||
222 | * Massage node values to match the expectations on different Drupal versions. |
||
223 | * |
||
224 | * @beforeNodeCreate |
||
225 | */ |
||
226 | public static function alterNodeParameters(BeforeNodeCreateScope $scope) |
||
249 | |||
250 | /** |
||
251 | * Remove any created nodes. |
||
252 | * |
||
253 | * @AfterScenario |
||
254 | */ |
||
255 | public function cleanNodes() |
||
263 | |||
264 | /** |
||
265 | * Remove any created users. |
||
266 | * |
||
267 | * @AfterScenario |
||
268 | */ |
||
269 | public function cleanUsers() |
||
286 | |||
287 | /** |
||
288 | * Remove any created terms. |
||
289 | * |
||
290 | * @AfterScenario |
||
291 | */ |
||
292 | public function cleanTerms() |
||
300 | |||
301 | /** |
||
302 | * Remove any created roles. |
||
303 | * |
||
304 | * @AfterScenario |
||
305 | */ |
||
306 | public function cleanRoles() |
||
314 | |||
315 | /** |
||
316 | * Remove any created languages. |
||
317 | * |
||
318 | * @AfterScenario |
||
319 | */ |
||
320 | public function cleanLanguages() |
||
328 | |||
329 | /** |
||
330 | * Clear static caches. |
||
331 | * |
||
332 | * @AfterScenario @api |
||
333 | */ |
||
334 | public function clearStaticCaches() |
||
338 | |||
339 | /** |
||
340 | * Dispatch scope hooks. |
||
341 | * |
||
342 | * @param string $scope |
||
343 | * The entity scope to dispatch. |
||
344 | * @param \stdClass $entity |
||
345 | * The entity. |
||
346 | */ |
||
347 | protected function dispatchHooks($scopeType, \stdClass $entity) |
||
361 | |||
362 | /** |
||
363 | * Create a node. |
||
364 | * |
||
365 | * @return object |
||
366 | * The created node. |
||
367 | */ |
||
368 | View Code Duplication | public function nodeCreate($node) |
|
377 | |||
378 | /** |
||
379 | * Parses the field values and turns them into the format expected by Drupal. |
||
380 | * |
||
381 | * Multiple values in a single field must be separated by commas. Wrap the |
||
382 | * field value in double quotes in case it should contain a comma. |
||
383 | * |
||
384 | * Compound field properties are identified using a ':' operator, either in |
||
385 | * the column heading or in the cell. If multiple properties are present in a |
||
386 | * single cell, they must be separated using ' - ', and values should not |
||
387 | * contain ':' or ' - '. |
||
388 | * |
||
389 | * Possible formats for the values: |
||
390 | * A |
||
391 | * A, B, "a value, containing a comma" |
||
392 | * A - B |
||
393 | * x: A - y: B |
||
394 | * A - B, C - D, "E - F" |
||
395 | * x: A - y: B, x: C - y: D, "x: E - y: F" |
||
396 | * |
||
397 | * See field_handlers.feature for examples of usage. |
||
398 | * |
||
399 | * @param string $entity_type |
||
400 | * The entity type. |
||
401 | * @param \stdClass $entity |
||
402 | * An object containing the entity properties and fields as properties. |
||
403 | * |
||
404 | * @throws \Exception |
||
405 | * Thrown when a field name is invalid. |
||
406 | */ |
||
407 | public function parseEntityFields($entity_type, \stdClass $entity) |
||
480 | |||
481 | /** |
||
482 | * Create a user. |
||
483 | * |
||
484 | * @return object |
||
485 | * The created user. |
||
486 | */ |
||
487 | public function userCreate($user) |
||
496 | |||
497 | /** |
||
498 | * Create a term. |
||
499 | * |
||
500 | * @return object |
||
501 | * The created term. |
||
502 | */ |
||
503 | View Code Duplication | public function termCreate($term) |
|
512 | |||
513 | /** |
||
514 | * Creates a language. |
||
515 | * |
||
516 | * @param \stdClass $language |
||
517 | * An object with the following properties: |
||
518 | * - langcode: the langcode of the language to create. |
||
519 | * |
||
520 | * @return object|FALSE |
||
521 | * The created language, or FALSE if the language was already created. |
||
522 | */ |
||
523 | public function languageCreate(\stdClass $language) |
||
533 | |||
534 | /** |
||
535 | * Log-in the given user. |
||
536 | * |
||
537 | * @param \stdClass $user |
||
538 | * The user to log in. |
||
539 | */ |
||
540 | public function login(\stdClass $user) |
||
544 | |||
545 | /** |
||
546 | * Logs the current user out. |
||
547 | * |
||
548 | * @param bool $fast |
||
549 | * Utilize direct logout by session if available. |
||
550 | */ |
||
551 | public function logout($fast = false) |
||
559 | |||
560 | /** |
||
561 | * Determine if the a user is already logged in. |
||
562 | * |
||
563 | * @return boolean |
||
564 | * Returns TRUE if a user is logged in for this session. |
||
565 | */ |
||
566 | public function loggedIn() |
||
570 | |||
571 | /** |
||
572 | * User with a given role is already logged in. |
||
573 | * |
||
574 | * @param string $role |
||
575 | * A single role, or multiple comma-separated roles in a single string. |
||
576 | * |
||
577 | * @return boolean |
||
578 | * Returns TRUE if the current logged in user has this role (or roles). |
||
579 | */ |
||
580 | public function loggedInWithRole($role) |
||
584 | |||
585 | /** |
||
586 | * Create an entity. |
||
587 | * |
||
588 | * @return object |
||
589 | * The created entity. |
||
590 | */ |
||
591 | public function entityCreate($entity_type, $entity) { |
||
606 | |||
607 | /** |
||
608 | * Remove any content entities created by entityCreate(), |
||
609 | * but not those created by nodeCreate(), termCreate() or userCreate(). |
||
610 | * |
||
611 | * @AfterScenario |
||
612 | */ |
||
613 | public function cleanEntities() { |
||
619 | |||
620 | /** |
||
621 | * Returns the Behat context that corresponds with the given class name. |
||
622 | * |
||
623 | * This is inspired by InitializedContextEnvironment::getContext() but also |
||
624 | * returns subclasses of the given class name. This allows us to retrieve for |
||
625 | * example DrupalContext even if it is overridden in a project. |
||
626 | * |
||
627 | * @param string $class |
||
628 | * A fully namespaced class name. |
||
629 | * |
||
630 | * @return \Behat\Behat\Context\Context|false |
||
631 | * The requested context, or FALSE if the context is not registered. |
||
632 | * |
||
633 | * @throws \Exception |
||
634 | * Thrown when the environment is not yet initialized, meaning that contexts |
||
635 | * cannot yet be retrieved. |
||
636 | */ |
||
637 | protected function getContext($class) |
||
657 | } |
||
658 |
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: