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 | use DrupalParametersTrait; |
||
32 | |||
33 | /** |
||
34 | * Drupal driver manager. |
||
35 | * |
||
36 | * @var \Drupal\DrupalDriverManager |
||
37 | */ |
||
38 | private $drupal; |
||
39 | |||
40 | /** |
||
41 | * Event dispatcher object. |
||
42 | * |
||
43 | * @var \Behat\Testwork\Hook\HookDispatcher |
||
44 | */ |
||
45 | protected $dispatcher; |
||
46 | |||
47 | /** |
||
48 | * Drupal authentication manager. |
||
49 | * |
||
50 | * @var \Drupal\DrupalExtension\Manager\DrupalAuthenticationManagerInterface |
||
51 | */ |
||
52 | protected $authenticationManager; |
||
53 | |||
54 | /** |
||
55 | * Drupal user manager. |
||
56 | * |
||
57 | * @var \Drupal\DrupalExtension\Manager\DrupalUserManagerInterface |
||
58 | */ |
||
59 | protected $userManager; |
||
60 | |||
61 | /** |
||
62 | * Keep track of nodes so they can be cleaned up. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $nodes = array(); |
||
67 | |||
68 | /** |
||
69 | * Keep track of all terms that are created so they can easily be removed. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $terms = array(); |
||
74 | |||
75 | /** |
||
76 | * Keep track of any roles that are created so they can easily be removed. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $roles = array(); |
||
81 | |||
82 | /** |
||
83 | * Keep track of any languages that are created so they can easily be removed. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $languages = array(); |
||
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | public function setDrupal(DrupalDriverManager $drupal) { |
||
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | public function getDrupal() { |
||
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | public function setUserManager(DrupalUserManagerInterface $userManager) { |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function getUserManager() { |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function setAuthenticationManager(DrupalAuthenticationManagerInterface $authenticationManager) { |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function getAuthenticationManager() { |
||
130 | |||
131 | /** |
||
132 | * Magic setter. |
||
133 | */ |
||
134 | public function __set($name, $value) { |
||
158 | |||
159 | /** |
||
160 | * Magic getter. |
||
161 | */ |
||
162 | public function __get($name) { |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | public function setDispatcher(HookDispatcher $dispatcher) { |
||
184 | |||
185 | /** |
||
186 | * Get active Drupal Driver. |
||
187 | * |
||
188 | * @return \Drupal\Driver\DrupalDriver |
||
189 | */ |
||
190 | public function getDriver($name = NULL) { |
||
193 | |||
194 | /** |
||
195 | * Get driver's random generator. |
||
196 | */ |
||
197 | public function getRandom() { |
||
200 | |||
201 | /** |
||
202 | * Massage node values to match the expectations on different Drupal versions. |
||
203 | * |
||
204 | * @beforeNodeCreate |
||
205 | */ |
||
206 | public static function alterNodeParameters(BeforeNodeCreateScope $scope) { |
||
228 | |||
229 | /** |
||
230 | * Remove any created nodes. |
||
231 | * |
||
232 | * @AfterScenario |
||
233 | */ |
||
234 | public function cleanNodes() { |
||
241 | |||
242 | /** |
||
243 | * Remove any created users. |
||
244 | * |
||
245 | * @AfterScenario |
||
246 | */ |
||
247 | public function cleanUsers() { |
||
260 | |||
261 | /** |
||
262 | * Remove any created terms. |
||
263 | * |
||
264 | * @AfterScenario |
||
265 | */ |
||
266 | public function cleanTerms() { |
||
273 | |||
274 | /** |
||
275 | * Remove any created roles. |
||
276 | * |
||
277 | * @AfterScenario |
||
278 | */ |
||
279 | public function cleanRoles() { |
||
286 | |||
287 | /** |
||
288 | * Remove any created languages. |
||
289 | * |
||
290 | * @AfterScenario |
||
291 | */ |
||
292 | public function cleanLanguages() { |
||
299 | |||
300 | /** |
||
301 | * Clear static caches. |
||
302 | * |
||
303 | * @AfterScenario @api |
||
304 | */ |
||
305 | public function clearStaticCaches() { |
||
308 | |||
309 | /** |
||
310 | * Dispatch scope hooks. |
||
311 | * |
||
312 | * @param string $scope |
||
313 | * The entity scope to dispatch. |
||
314 | * @param \stdClass $entity |
||
315 | * The entity. |
||
316 | */ |
||
317 | protected function dispatchHooks($scopeType, \stdClass $entity) { |
||
330 | |||
331 | /** |
||
332 | * Create a node. |
||
333 | * |
||
334 | * @return object |
||
335 | * The created node. |
||
336 | */ |
||
337 | View Code Duplication | public function nodeCreate($node) { |
|
345 | |||
346 | /** |
||
347 | * Parses the field values and turns them into the format expected by Drupal. |
||
348 | * |
||
349 | * Multiple values in a single field must be separated by commas. Wrap the |
||
350 | * field value in double quotes in case it should contain a comma. |
||
351 | * |
||
352 | * Compound field properties are identified using a ':' operator, either in |
||
353 | * the column heading or in the cell. If multiple properties are present in a |
||
354 | * single cell, they must be separated using ' - ', and values should not |
||
355 | * contain ':' or ' - '. |
||
356 | * |
||
357 | * Possible formats for the values: |
||
358 | * A |
||
359 | * A, B, "a value, containing a comma" |
||
360 | * A - B |
||
361 | * x: A - y: B |
||
362 | * A - B, C - D, "E - F" |
||
363 | * x: A - y: B, x: C - y: D, "x: E - y: F" |
||
364 | * |
||
365 | * See field_handlers.feature for examples of usage. |
||
366 | * |
||
367 | * @param string $entity_type |
||
368 | * The entity type. |
||
369 | * @param \stdClass $entity |
||
370 | * An object containing the entity properties and fields as properties. |
||
371 | * |
||
372 | * @throws \Exception |
||
373 | * Thrown when a field name is invalid. |
||
374 | */ |
||
375 | public function parseEntityFields($entity_type, \stdClass $entity) { |
||
452 | |||
453 | /** |
||
454 | * Create a user. |
||
455 | * |
||
456 | * @return object |
||
457 | * The created user. |
||
458 | */ |
||
459 | public function userCreate($user) { |
||
467 | |||
468 | /** |
||
469 | * Create a term. |
||
470 | * |
||
471 | * @return object |
||
472 | * The created term. |
||
473 | */ |
||
474 | View Code Duplication | public function termCreate($term) { |
|
482 | |||
483 | /** |
||
484 | * Creates a language. |
||
485 | * |
||
486 | * @param \stdClass $language |
||
487 | * An object with the following properties: |
||
488 | * - langcode: the langcode of the language to create. |
||
489 | * |
||
490 | * @return object|FALSE |
||
491 | * The created language, or FALSE if the language was already created. |
||
492 | */ |
||
493 | public function languageCreate(\stdClass $language) { |
||
502 | |||
503 | /** |
||
504 | * Log-in the given user. |
||
505 | * |
||
506 | * @param \stdClass $user |
||
507 | * The user to log in. |
||
508 | */ |
||
509 | public function login(\stdClass $user) { |
||
512 | |||
513 | /** |
||
514 | * Logs the current user out. |
||
515 | */ |
||
516 | public function logout() { |
||
519 | |||
520 | /** |
||
521 | * Determine if the a user is already logged in. |
||
522 | * |
||
523 | * @return boolean |
||
524 | * Returns TRUE if a user is logged in for this session. |
||
525 | */ |
||
526 | public function loggedIn() { |
||
529 | |||
530 | /** |
||
531 | * User with a given role is already logged in. |
||
532 | * |
||
533 | * @param string $role |
||
534 | * A single role, or multiple comma-separated roles in a single string. |
||
535 | * |
||
536 | * @return boolean |
||
537 | * Returns TRUE if the current logged in user has this role (or roles). |
||
538 | */ |
||
539 | public function loggedInWithRole($role) { |
||
542 | |||
543 | /** |
||
544 | * Returns the Behat context that corresponds with the given class name. |
||
545 | * |
||
546 | * This is inspired by InitializedContextEnvironment::getContext() but also |
||
547 | * returns subclasses of the given class name. This allows us to retrieve for |
||
548 | * example DrupalContext even if it is overridden in a project. |
||
549 | * |
||
550 | * @param string $class |
||
551 | * A fully namespaced class name. |
||
552 | * |
||
553 | * @return \Behat\Behat\Context\Context|false |
||
554 | * The requested context, or FALSE if the context is not registered. |
||
555 | * |
||
556 | * @throws \Exception |
||
557 | * Thrown when the environment is not yet initialized, meaning that contexts |
||
558 | * cannot yet be retrieved. |
||
559 | */ |
||
560 | protected function getContext($class) { |
||
579 | |||
580 | } |
||
581 |
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: