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 |
||
27 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface { |
||
28 | |||
29 | /** |
||
30 | * Drupal driver manager. |
||
31 | * |
||
32 | * @var \Drupal\DrupalDriverManager |
||
33 | */ |
||
34 | private $drupal; |
||
35 | |||
36 | /** |
||
37 | * Test parameters. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | private $drupalParameters; |
||
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\DrupalAuthenticationManagerInterface |
||
54 | */ |
||
55 | protected $authenticationManager; |
||
56 | |||
57 | /** |
||
58 | * Drupal user manager. |
||
59 | * |
||
60 | * @var \Drupal\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 | * {@inheritDoc} |
||
94 | */ |
||
95 | public function setDrupal(DrupalDriverManager $drupal) { |
||
96 | $this->drupal = $drupal; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | public function getDrupal() { |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | public function setUserManager(DrupalUserManagerInterface $userManager) { |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function getUserManager() { |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function setAuthenticationManager(DrupalAuthenticationManagerInterface $authenticationManager) { |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function getAuthenticationManager() { |
||
133 | |||
134 | /** |
||
135 | * Magic setter. |
||
136 | */ |
||
137 | public function __set($name, $value) { |
||
161 | |||
162 | /** |
||
163 | * Magic getter. |
||
164 | */ |
||
165 | public function __get($name) { |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function setDispatcher(HookDispatcher $dispatcher) { |
||
187 | |||
188 | /** |
||
189 | * Set parameters provided for Drupal. |
||
190 | */ |
||
191 | public function setDrupalParameters(array $parameters) { |
||
194 | |||
195 | /** |
||
196 | * Returns a specific Drupal parameter. |
||
197 | * |
||
198 | * @param string $name |
||
199 | * Parameter name. |
||
200 | * |
||
201 | * @return mixed |
||
202 | */ |
||
203 | public function getDrupalParameter($name) { |
||
206 | |||
207 | /** |
||
208 | * Returns a specific Drupal text value. |
||
209 | * |
||
210 | * @param string $name |
||
211 | * Text value name, such as 'log_out', which corresponds to the default 'Log |
||
212 | * out' link text. |
||
213 | * @throws \Exception |
||
214 | * @return |
||
215 | */ |
||
216 | View Code Duplication | public function getDrupalText($name) { |
|
223 | |||
224 | /** |
||
225 | * Returns a specific css selector. |
||
226 | * |
||
227 | * @param $name |
||
228 | * string CSS selector name |
||
229 | */ |
||
230 | View Code Duplication | public function getDrupalSelector($name) { |
|
237 | |||
238 | /** |
||
239 | * Get active Drupal Driver. |
||
240 | * |
||
241 | * @return \Drupal\Driver\DrupalDriver |
||
242 | */ |
||
243 | public function getDriver($name = NULL) { |
||
246 | |||
247 | /** |
||
248 | * Get driver's random generator. |
||
249 | */ |
||
250 | public function getRandom() { |
||
253 | |||
254 | /** |
||
255 | * Massage node values to match the expectations on different Drupal versions. |
||
256 | * |
||
257 | * @beforeNodeCreate |
||
258 | */ |
||
259 | public function alterNodeParameters(BeforeNodeCreateScope $scope) { |
||
281 | |||
282 | /** |
||
283 | * Remove any created nodes. |
||
284 | * |
||
285 | * @AfterScenario |
||
286 | */ |
||
287 | public function cleanNodes() { |
||
294 | |||
295 | /** |
||
296 | * Remove any created users. |
||
297 | * |
||
298 | * @AfterScenario |
||
299 | */ |
||
300 | public function cleanUsers() { |
||
313 | |||
314 | /** |
||
315 | * Remove any created terms. |
||
316 | * |
||
317 | * @AfterScenario |
||
318 | */ |
||
319 | public function cleanTerms() { |
||
326 | |||
327 | /** |
||
328 | * Remove any created roles. |
||
329 | * |
||
330 | * @AfterScenario |
||
331 | */ |
||
332 | public function cleanRoles() { |
||
339 | |||
340 | /** |
||
341 | * Remove any created languages. |
||
342 | * |
||
343 | * @AfterScenario |
||
344 | */ |
||
345 | public function cleanLanguages() { |
||
352 | |||
353 | /** |
||
354 | * Clear static caches. |
||
355 | * |
||
356 | * @AfterScenario @api |
||
357 | */ |
||
358 | public function clearStaticCaches() { |
||
361 | |||
362 | /** |
||
363 | * Dispatch scope hooks. |
||
364 | * |
||
365 | * @param string $scope |
||
366 | * The entity scope to dispatch. |
||
367 | * @param \stdClass $entity |
||
368 | * The entity. |
||
369 | */ |
||
370 | protected function dispatchHooks($scopeType, \stdClass $entity) { |
||
383 | |||
384 | /** |
||
385 | * Create a node. |
||
386 | * |
||
387 | * @return object |
||
388 | * The created node. |
||
389 | */ |
||
390 | View Code Duplication | public function nodeCreate($node) { |
|
398 | |||
399 | /** |
||
400 | * Parse multi-value fields. Possible formats: |
||
401 | * A, B, C |
||
402 | * A - B, C - D, E - F |
||
403 | * |
||
404 | * @param string $entity_type |
||
405 | * The entity type. |
||
406 | * @param \stdClass $entity |
||
407 | * An object containing the entity properties and fields as properties. |
||
408 | */ |
||
409 | public function parseEntityFields($entity_type, \stdClass $entity) { |
||
476 | |||
477 | /** |
||
478 | * Create a user. |
||
479 | * |
||
480 | * @return object |
||
481 | * The created user. |
||
482 | */ |
||
483 | public function userCreate($user) { |
||
491 | |||
492 | /** |
||
493 | * Create a term. |
||
494 | * |
||
495 | * @return object |
||
496 | * The created term. |
||
497 | */ |
||
498 | View Code Duplication | public function termCreate($term) { |
|
506 | |||
507 | /** |
||
508 | * Creates a language. |
||
509 | * |
||
510 | * @param \stdClass $language |
||
511 | * An object with the following properties: |
||
512 | * - langcode: the langcode of the language to create. |
||
513 | * |
||
514 | * @return object|FALSE |
||
515 | * The created language, or FALSE if the language was already created. |
||
516 | */ |
||
517 | public function languageCreate(\stdClass $language) { |
||
526 | |||
527 | /** |
||
528 | * Log-in the given user. |
||
529 | * |
||
530 | * @param \stdClass $user |
||
531 | * The user to log in. |
||
532 | */ |
||
533 | public function login(\stdClass $user) { |
||
564 | |||
565 | /** |
||
566 | * Logs the current user out. |
||
567 | */ |
||
568 | public function logout() { |
||
572 | |||
573 | /** |
||
574 | * Determine if the a user is already logged in. |
||
575 | * |
||
576 | * @return boolean |
||
577 | * Returns TRUE if a user is logged in for this session. |
||
578 | */ |
||
579 | public function loggedIn() { |
||
614 | |||
615 | /** |
||
616 | * User with a given role is already logged in. |
||
617 | * |
||
618 | * @param string $role |
||
619 | * A single role, or multiple comma-separated roles in a single string. |
||
620 | * |
||
621 | * @return boolean |
||
622 | * Returns TRUE if the current logged in user has this role (or roles). |
||
623 | */ |
||
624 | public function loggedInWithRole($role) { |
||
627 | |||
628 | } |
||
629 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..