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 |
||
25 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface { |
||
26 | |||
27 | /** |
||
28 | * Drupal driver manager. |
||
29 | * |
||
30 | * @var \Drupal\DrupalDriverManager |
||
31 | */ |
||
32 | private $drupal; |
||
33 | |||
34 | /** |
||
35 | * Test parameters. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | private $drupalParameters; |
||
40 | |||
41 | /** |
||
42 | * Event dispatcher object. |
||
43 | * |
||
44 | * @var \Behat\Testwork\Hook\HookDispatcher |
||
45 | */ |
||
46 | protected $dispatcher; |
||
47 | |||
48 | /** |
||
49 | * Keep track of nodes so they can be cleaned up. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $nodes = array(); |
||
54 | |||
55 | /** |
||
56 | * Current authenticated user. |
||
57 | * |
||
58 | * A value of FALSE denotes an anonymous user. |
||
59 | * |
||
60 | * @var \stdClass|bool |
||
61 | */ |
||
62 | public $user = FALSE; |
||
63 | |||
64 | /** |
||
65 | * Keep track of all users that are created so they can easily be removed. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $users = 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 | * {@inheritDoc} |
||
94 | */ |
||
95 | public function setDrupal(DrupalDriverManager $drupal) { |
||
98 | |||
99 | /** |
||
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | public function getDrupal() { |
||
103 | return $this->drupal; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | public function setDispatcher(HookDispatcher $dispatcher) { |
||
110 | $this->dispatcher = $dispatcher; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Set parameters provided for Drupal. |
||
115 | */ |
||
116 | public function setDrupalParameters(array $parameters) { |
||
117 | $this->drupalParameters = $parameters; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Returns a specific Drupal parameter. |
||
122 | * |
||
123 | * @param string $name |
||
124 | * Parameter name. |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function getDrupalParameter($name) { |
||
129 | return isset($this->drupalParameters[$name]) ? $this->drupalParameters[$name] : NULL; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Returns a specific Drupal text value. |
||
134 | * |
||
135 | * @param string $name |
||
136 | * Text value name, such as 'log_out', which corresponds to the default 'Log |
||
137 | * out' link text. |
||
138 | * @throws \Exception |
||
139 | * @return |
||
140 | */ |
||
141 | public function getDrupalText($name) { |
||
142 | $text = $this->getDrupalParameter('text'); |
||
143 | if (!isset($text[$name])) { |
||
144 | throw new \Exception(sprintf('No such Drupal string: %s', $name)); |
||
145 | } |
||
146 | return $text[$name]; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns a specific css selector. |
||
151 | * |
||
152 | * @param $name |
||
153 | * string CSS selector name |
||
154 | */ |
||
155 | public function getDrupalSelector($name) { |
||
162 | |||
163 | /** |
||
164 | * Get active Drupal Driver. |
||
165 | * |
||
166 | * @return \Drupal\Driver\DrupalDriver |
||
167 | */ |
||
168 | public function getDriver($name = NULL) { |
||
169 | return $this->getDrupal()->getDriver($name); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Get driver's random generator. |
||
174 | */ |
||
175 | public function getRandom() { |
||
176 | return $this->getDriver()->getRandom(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Massage node values to match the expectations on different Drupal versions. |
||
181 | * |
||
182 | * @beforeNodeCreate |
||
183 | */ |
||
184 | public function alterNodeParameters(BeforeNodeCreateScope $scope) { |
||
206 | |||
207 | /** |
||
208 | * Remove any created nodes. |
||
209 | * |
||
210 | * @AfterScenario |
||
211 | */ |
||
212 | public function cleanNodes() { |
||
219 | |||
220 | /** |
||
221 | * Remove any created users. |
||
222 | * |
||
223 | * @AfterScenario |
||
224 | */ |
||
225 | public function cleanUsers() { |
||
239 | |||
240 | /** |
||
241 | * Remove any created terms. |
||
242 | * |
||
243 | * @AfterScenario |
||
244 | */ |
||
245 | public function cleanTerms() { |
||
252 | |||
253 | /** |
||
254 | * Remove any created roles. |
||
255 | * |
||
256 | * @AfterScenario |
||
257 | */ |
||
258 | public function cleanRoles() { |
||
265 | |||
266 | /** |
||
267 | * Remove any created languages. |
||
268 | * |
||
269 | * @AfterScenario |
||
270 | */ |
||
271 | public function cleanLanguages() { |
||
278 | |||
279 | /** |
||
280 | * Clear static caches. |
||
281 | * |
||
282 | * @AfterScenario @api |
||
283 | */ |
||
284 | public function clearStaticCaches() { |
||
287 | |||
288 | /** |
||
289 | * Dispatch scope hooks. |
||
290 | * |
||
291 | * @param string $scope |
||
292 | * The entity scope to dispatch. |
||
293 | * @param \stdClass $entity |
||
294 | * The entity. |
||
295 | */ |
||
296 | protected function dispatchHooks($scopeType, \stdClass $entity) { |
||
309 | |||
310 | /** |
||
311 | * Create a node. |
||
312 | * |
||
313 | * @return object |
||
314 | * The created node. |
||
315 | */ |
||
316 | View Code Duplication | public function nodeCreate($node) { |
|
324 | |||
325 | /** |
||
326 | * Parse multi-value fields. Possible formats: |
||
327 | * A, B, C |
||
328 | * A - B, C - D, E - F |
||
329 | * |
||
330 | * @param string $entity_type |
||
331 | * The entity type. |
||
332 | * @param \stdClass $entity |
||
333 | * An object containing the entity properties and fields as properties. |
||
334 | */ |
||
335 | public function parseEntityFields($entity_type, \stdClass $entity) { |
||
402 | |||
403 | /** |
||
404 | * Create a user. |
||
405 | * |
||
406 | * @return object |
||
407 | * The created user. |
||
408 | */ |
||
409 | View Code Duplication | public function userCreate($user) { |
|
417 | |||
418 | /** |
||
419 | * Create a term. |
||
420 | * |
||
421 | * @return object |
||
422 | * The created term. |
||
423 | */ |
||
424 | View Code Duplication | public function termCreate($term) { |
|
432 | |||
433 | /** |
||
434 | * Creates a language. |
||
435 | * |
||
436 | * @param \stdClass $language |
||
437 | * An object with the following properties: |
||
438 | * - langcode: the langcode of the language to create. |
||
439 | * |
||
440 | * @return object|FALSE |
||
441 | * The created language, or FALSE if the language was already created. |
||
442 | */ |
||
443 | public function languageCreate(\stdClass $language) { |
||
452 | |||
453 | /** |
||
454 | * Log-in the current user. |
||
455 | */ |
||
456 | public function login() { |
||
487 | |||
488 | /** |
||
489 | * Logs the current user out. |
||
490 | */ |
||
491 | public function logout() { |
||
494 | |||
495 | /** |
||
496 | * Determine if the a user is already logged in. |
||
497 | * |
||
498 | * @return boolean |
||
499 | * Returns TRUE if a user is logged in for this session. |
||
500 | */ |
||
501 | 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 |
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: