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 |
||
28 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface { |
||
29 | |||
30 | use DrupalParametersTrait; |
||
31 | |||
32 | /** |
||
33 | * Drupal driver manager. |
||
34 | * |
||
35 | * @var \Drupal\DrupalDriverManager |
||
36 | */ |
||
37 | private $drupal; |
||
38 | |||
39 | /** |
||
40 | * Event dispatcher object. |
||
41 | * |
||
42 | * @var \Behat\Testwork\Hook\HookDispatcher |
||
43 | */ |
||
44 | protected $dispatcher; |
||
45 | |||
46 | /** |
||
47 | * Drupal authentication manager. |
||
48 | * |
||
49 | * @var \Drupal\DrupalExtension\Manager\DrupalAuthenticationManagerInterface |
||
50 | */ |
||
51 | protected $authenticationManager; |
||
52 | |||
53 | /** |
||
54 | * Drupal user manager. |
||
55 | * |
||
56 | * @var \Drupal\DrupalExtension\Manager\DrupalUserManagerInterface |
||
57 | */ |
||
58 | protected $userManager; |
||
59 | |||
60 | /** |
||
61 | * Keep track of nodes so they can be cleaned up. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $nodes = array(); |
||
66 | |||
67 | /** |
||
68 | * Keep track of all terms that are created so they can easily be removed. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $terms = array(); |
||
73 | |||
74 | /** |
||
75 | * Keep track of any roles that are created so they can easily be removed. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $roles = array(); |
||
80 | |||
81 | /** |
||
82 | * Keep track of any languages that are created so they can easily be removed. |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $languages = array(); |
||
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | public function setDrupal(DrupalDriverManager $drupal) { |
||
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | public function getDrupal() { |
||
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | */ |
||
105 | public function setUserManager(DrupalUserManagerInterface $userManager) { |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function getUserManager() { |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function setAuthenticationManager(DrupalAuthenticationManagerInterface $authenticationManager) { |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function getAuthenticationManager() { |
||
129 | |||
130 | /** |
||
131 | * Magic setter. |
||
132 | */ |
||
133 | public function __set($name, $value) { |
||
157 | |||
158 | /** |
||
159 | * Magic getter. |
||
160 | */ |
||
161 | public function __get($name) { |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function setDispatcher(HookDispatcher $dispatcher) { |
||
183 | |||
184 | /** |
||
185 | * Get active Drupal Driver. |
||
186 | * |
||
187 | * @return \Drupal\Driver\DrupalDriver |
||
188 | */ |
||
189 | public function getDriver($name = NULL) { |
||
192 | |||
193 | /** |
||
194 | * Get driver's random generator. |
||
195 | */ |
||
196 | public function getRandom() { |
||
199 | |||
200 | /** |
||
201 | * Massage node values to match the expectations on different Drupal versions. |
||
202 | * |
||
203 | * @beforeNodeCreate |
||
204 | */ |
||
205 | public function alterNodeParameters(BeforeNodeCreateScope $scope) { |
||
227 | |||
228 | /** |
||
229 | * Remove any created nodes. |
||
230 | * |
||
231 | * @AfterScenario |
||
232 | */ |
||
233 | public function cleanNodes() { |
||
240 | |||
241 | /** |
||
242 | * Remove any created users. |
||
243 | * |
||
244 | * @AfterScenario |
||
245 | */ |
||
246 | public function cleanUsers() { |
||
259 | |||
260 | /** |
||
261 | * Remove any created terms. |
||
262 | * |
||
263 | * @AfterScenario |
||
264 | */ |
||
265 | public function cleanTerms() { |
||
272 | |||
273 | /** |
||
274 | * Remove any created roles. |
||
275 | * |
||
276 | * @AfterScenario |
||
277 | */ |
||
278 | public function cleanRoles() { |
||
285 | |||
286 | /** |
||
287 | * Remove any created languages. |
||
288 | * |
||
289 | * @AfterScenario |
||
290 | */ |
||
291 | public function cleanLanguages() { |
||
298 | |||
299 | /** |
||
300 | * Clear static caches. |
||
301 | * |
||
302 | * @AfterScenario @api |
||
303 | */ |
||
304 | public function clearStaticCaches() { |
||
307 | |||
308 | /** |
||
309 | * Dispatch scope hooks. |
||
310 | * |
||
311 | * @param string $scope |
||
312 | * The entity scope to dispatch. |
||
313 | * @param \stdClass $entity |
||
314 | * The entity. |
||
315 | */ |
||
316 | protected function dispatchHooks($scopeType, \stdClass $entity) { |
||
329 | |||
330 | /** |
||
331 | * Create a node. |
||
332 | * |
||
333 | * @return object |
||
334 | * The created node. |
||
335 | */ |
||
336 | View Code Duplication | public function nodeCreate($node) { |
|
344 | |||
345 | /** |
||
346 | * Parse multi-value fields. Possible formats: |
||
347 | * A, B, C |
||
348 | * A - B, C - D, E - F |
||
349 | * |
||
350 | * @param string $entity_type |
||
351 | * The entity type. |
||
352 | * @param \stdClass $entity |
||
353 | * An object containing the entity properties and fields as properties. |
||
354 | */ |
||
355 | public function parseEntityFields($entity_type, \stdClass $entity) { |
||
422 | |||
423 | /** |
||
424 | * Create a user. |
||
425 | * |
||
426 | * @return object |
||
427 | * The created user. |
||
428 | */ |
||
429 | public function userCreate($user) { |
||
437 | |||
438 | /** |
||
439 | * Create a term. |
||
440 | * |
||
441 | * @return object |
||
442 | * The created term. |
||
443 | */ |
||
444 | View Code Duplication | public function termCreate($term) { |
|
452 | |||
453 | /** |
||
454 | * Creates a language. |
||
455 | * |
||
456 | * @param \stdClass $language |
||
457 | * An object with the following properties: |
||
458 | * - langcode: the langcode of the language to create. |
||
459 | * |
||
460 | * @return object|FALSE |
||
461 | * The created language, or FALSE if the language was already created. |
||
462 | */ |
||
463 | public function languageCreate(\stdClass $language) { |
||
472 | |||
473 | /** |
||
474 | * Log-in the given user. |
||
475 | * |
||
476 | * @param \stdClass $user |
||
477 | * The user to log in. |
||
478 | */ |
||
479 | public function login(\stdClass $user) { |
||
482 | |||
483 | /** |
||
484 | * Logs the current user out. |
||
485 | */ |
||
486 | public function logout() { |
||
489 | |||
490 | /** |
||
491 | * Determine if the a user is already logged in. |
||
492 | * |
||
493 | * @return boolean |
||
494 | * Returns TRUE if a user is logged in for this session. |
||
495 | */ |
||
496 | public function loggedIn() { |
||
499 | |||
500 | /** |
||
501 | * User with a given role is already logged in. |
||
502 | * |
||
503 | * @param string $role |
||
504 | * A single role, or multiple comma-separated roles in a single string. |
||
505 | * |
||
506 | * @return boolean |
||
507 | * Returns TRUE if the current logged in user has this role (or roles). |
||
508 | */ |
||
509 | public function loggedInWithRole($role) { |
||
512 | |||
513 | } |
||
514 |
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: