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() |
||
277 | |||
278 | /** |
||
279 | * Remove any created terms. |
||
280 | * |
||
281 | * @AfterScenario |
||
282 | */ |
||
283 | public function cleanTerms() |
||
291 | |||
292 | /** |
||
293 | * Remove any created roles. |
||
294 | * |
||
295 | * @AfterScenario |
||
296 | */ |
||
297 | public function cleanRoles() |
||
305 | |||
306 | /** |
||
307 | * Remove any created languages. |
||
308 | * |
||
309 | * @AfterScenario |
||
310 | */ |
||
311 | public function cleanLanguages() |
||
319 | |||
320 | /** |
||
321 | * Clear static caches. |
||
322 | * |
||
323 | * @AfterScenario @api |
||
324 | */ |
||
325 | public function clearStaticCaches() |
||
329 | |||
330 | /** |
||
331 | * Dispatch scope hooks. |
||
332 | * |
||
333 | * @param string $scope |
||
334 | * The entity scope to dispatch. |
||
335 | * @param \stdClass $entity |
||
336 | * The entity. |
||
337 | */ |
||
338 | protected function dispatchHooks($scopeType, \stdClass $entity) |
||
352 | |||
353 | /** |
||
354 | * Create a node. |
||
355 | * |
||
356 | * @return object |
||
357 | * The created node. |
||
358 | */ |
||
359 | View Code Duplication | public function nodeCreate($node) |
|
368 | |||
369 | /** |
||
370 | * Parses the field values and turns them into the format expected by Drupal. |
||
371 | * |
||
372 | * Multiple values in a single field must be separated by commas. Wrap the |
||
373 | * field value in double quotes in case it should contain a comma. |
||
374 | * |
||
375 | * Compound field properties are identified using a ':' operator, either in |
||
376 | * the column heading or in the cell. If multiple properties are present in a |
||
377 | * single cell, they must be separated using ' - ', and values should not |
||
378 | * contain ':' or ' - '. |
||
379 | * |
||
380 | * Possible formats for the values: |
||
381 | * A |
||
382 | * A, B, "a value, containing a comma" |
||
383 | * A - B |
||
384 | * x: A - y: B |
||
385 | * A - B, C - D, "E - F" |
||
386 | * x: A - y: B, x: C - y: D, "x: E - y: F" |
||
387 | * |
||
388 | * See field_handlers.feature for examples of usage. |
||
389 | * |
||
390 | * @param string $entity_type |
||
391 | * The entity type. |
||
392 | * @param \stdClass $entity |
||
393 | * An object containing the entity properties and fields as properties. |
||
394 | * |
||
395 | * @throws \Exception |
||
396 | * Thrown when a field name is invalid. |
||
397 | */ |
||
398 | public function parseEntityFields($entity_type, \stdClass $entity) |
||
471 | |||
472 | /** |
||
473 | * Create a user. |
||
474 | * |
||
475 | * @return object |
||
476 | * The created user. |
||
477 | */ |
||
478 | public function userCreate($user) |
||
487 | |||
488 | /** |
||
489 | * Create a term. |
||
490 | * |
||
491 | * @return object |
||
492 | * The created term. |
||
493 | */ |
||
494 | View Code Duplication | public function termCreate($term) |
|
503 | |||
504 | /** |
||
505 | * Creates a language. |
||
506 | * |
||
507 | * @param \stdClass $language |
||
508 | * An object with the following properties: |
||
509 | * - langcode: the langcode of the language to create. |
||
510 | * |
||
511 | * @return object|FALSE |
||
512 | * The created language, or FALSE if the language was already created. |
||
513 | */ |
||
514 | public function languageCreate(\stdClass $language) |
||
524 | |||
525 | /** |
||
526 | * Log-in the given user. |
||
527 | * |
||
528 | * @param \stdClass $user |
||
529 | * The user to log in. |
||
530 | */ |
||
531 | public function login(\stdClass $user) |
||
535 | |||
536 | /** |
||
537 | * Logs the current user out. |
||
538 | * |
||
539 | * @param bool $fast |
||
540 | * Utilize direct logout by session if available. |
||
541 | */ |
||
542 | public function logout($fast = false) |
||
550 | |||
551 | /** |
||
552 | * Determine if the a user is already logged in. |
||
553 | * |
||
554 | * @return boolean |
||
555 | * Returns TRUE if a user is logged in for this session. |
||
556 | */ |
||
557 | public function loggedIn() |
||
561 | |||
562 | /** |
||
563 | * User with a given role is already logged in. |
||
564 | * |
||
565 | * @param string $role |
||
566 | * A single role, or multiple comma-separated roles in a single string. |
||
567 | * |
||
568 | * @return boolean |
||
569 | * Returns TRUE if the current logged in user has this role (or roles). |
||
570 | */ |
||
571 | public function loggedInWithRole($role) |
||
575 | |||
576 | /** |
||
577 | * Returns the Behat context that corresponds with the given class name. |
||
578 | * |
||
579 | * This is inspired by InitializedContextEnvironment::getContext() but also |
||
580 | * returns subclasses of the given class name. This allows us to retrieve for |
||
581 | * example DrupalContext even if it is overridden in a project. |
||
582 | * |
||
583 | * @param string $class |
||
584 | * A fully namespaced class name. |
||
585 | * |
||
586 | * @return \Behat\Behat\Context\Context|false |
||
587 | * The requested context, or FALSE if the context is not registered. |
||
588 | * |
||
589 | * @throws \Exception |
||
590 | * Thrown when the environment is not yet initialized, meaning that contexts |
||
591 | * cannot yet be retrieved. |
||
592 | */ |
||
593 | protected function getContext($class) |
||
613 | } |
||
614 |
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: