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 |
||
24 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface { |
||
25 | |||
26 | /** |
||
27 | * Drupal driver manager. |
||
28 | * |
||
29 | * @var \Drupal\DrupalDriverManager |
||
30 | */ |
||
31 | private $drupal; |
||
32 | |||
33 | /** |
||
34 | * Test parameters. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $drupalParameters; |
||
39 | |||
40 | /** |
||
41 | * Event dispatcher object. |
||
42 | * |
||
43 | * @var \Behat\Testwork\Hook\HookDispatcher |
||
44 | */ |
||
45 | protected $dispatcher; |
||
46 | |||
47 | /** |
||
48 | * Keep track of nodes so they can be cleaned up. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $nodes = array(); |
||
53 | |||
54 | /** |
||
55 | * Current authenticated user. |
||
56 | * |
||
57 | * A value of FALSE denotes an anonymous user. |
||
58 | * |
||
59 | * @var stdClass|bool |
||
60 | */ |
||
61 | public $user = FALSE; |
||
62 | |||
63 | /** |
||
64 | * Keep track of all users that are created so they can easily be removed. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $users = array(); |
||
69 | |||
70 | /** |
||
71 | * Keep track of all terms that are created so they can easily be removed. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $terms = array(); |
||
76 | |||
77 | /** |
||
78 | * Keep track of any roles that are created so they can easily be removed. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $roles = array(); |
||
83 | |||
84 | /** |
||
85 | * Keep track of any languages that are created so they can easily be removed. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $languages = array(); |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | public function setDrupal(DrupalDriverManager $drupal) { |
||
95 | $this->drupal = $drupal; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function getDrupal() { |
||
102 | return $this->drupal; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | public function setDispatcher(HookDispatcher $dispatcher) { |
||
109 | $this->dispatcher = $dispatcher; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Set parameters provided for Drupal. |
||
114 | */ |
||
115 | public function setDrupalParameters(array $parameters) { |
||
116 | $this->drupalParameters = $parameters; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns a specific Drupal parameter. |
||
121 | * |
||
122 | * @param string $name |
||
123 | * Parameter name. |
||
124 | * |
||
125 | * @return mixed |
||
126 | */ |
||
127 | public function getDrupalParameter($name) { |
||
128 | return isset($this->drupalParameters[$name]) ? $this->drupalParameters[$name] : NULL; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns a specific Drupal text value. |
||
133 | * |
||
134 | * @param string $name |
||
135 | * Text value name, such as 'log_out', which corresponds to the default 'Log |
||
136 | * out' link text. |
||
137 | * @throws \Exception |
||
138 | * @return |
||
139 | */ |
||
140 | View Code Duplication | public function getDrupalText($name) { |
|
|
|||
141 | $text = $this->getDrupalParameter('text'); |
||
142 | if (!isset($text[$name])) { |
||
143 | throw new \Exception(sprintf('No such Drupal string: %s', $name)); |
||
144 | } |
||
145 | return $text[$name]; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Get active Drupal Driver. |
||
150 | */ |
||
151 | public function getDriver($name = NULL) { |
||
152 | return $this->getDrupal()->getDriver($name); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get driver's random generator. |
||
157 | */ |
||
158 | public function getRandom() { |
||
159 | return $this->getDriver()->getRandom(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Remove any created nodes. |
||
164 | * |
||
165 | * @AfterScenario |
||
166 | */ |
||
167 | public function cleanNodes() { |
||
168 | // Remove any nodes that were created. |
||
169 | foreach ($this->nodes as $node) { |
||
170 | $this->getDriver()->nodeDelete($node); |
||
171 | } |
||
172 | $this->nodes = array(); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Remove any created users. |
||
177 | * |
||
178 | * @AfterScenario |
||
179 | */ |
||
180 | public function cleanUsers() { |
||
181 | // Remove any users that were created. |
||
182 | if (!empty($this->users)) { |
||
183 | foreach ($this->users as $user) { |
||
184 | $this->getDriver()->userDelete($user); |
||
185 | } |
||
186 | $this->getDriver()->processBatch(); |
||
187 | $this->users = array(); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Remove any created terms. |
||
193 | * |
||
194 | * @AfterScenario |
||
195 | */ |
||
196 | public function cleanTerms() { |
||
197 | // Remove any terms that were created. |
||
198 | foreach ($this->terms as $term) { |
||
199 | $this->getDriver()->termDelete($term); |
||
200 | } |
||
201 | $this->terms = array(); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Remove any created roles. |
||
206 | * |
||
207 | * @AfterScenario |
||
208 | */ |
||
209 | public function cleanRoles() { |
||
210 | // Remove any roles that were created. |
||
211 | foreach ($this->roles as $rid) { |
||
212 | $this->getDriver()->roleDelete($rid); |
||
213 | } |
||
214 | $this->roles = array(); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Remove any created languages. |
||
219 | * |
||
220 | * @AfterScenario |
||
221 | */ |
||
222 | public function cleanLanguages() { |
||
223 | // Delete any languages that were created. |
||
224 | foreach ($this->languages as $language) { |
||
225 | $this->getDriver()->languageDelete($language); |
||
226 | unset($this->languages[$language->langcode]); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Clear static caches. |
||
232 | * |
||
233 | * @AfterScenario @api |
||
234 | */ |
||
235 | public function clearStaticCaches() { |
||
238 | |||
239 | /** |
||
240 | * Dispatch scope hooks. |
||
241 | * |
||
242 | * @param string $scope |
||
243 | * The entity scope to dispatch. |
||
244 | * @param stdClass $entity |
||
245 | * The entity. |
||
246 | */ |
||
247 | protected function dispatchHooks($scopeType, \stdClass $entity) { |
||
248 | $fullScopeClass = 'Drupal\\DrupalExtension\\Hook\\Scope\\' . $scopeType; |
||
249 | $scope = new $fullScopeClass($this->getDrupal()->getEnvironment(), $this, $entity); |
||
250 | $callResults = $this->dispatcher->dispatchScopeHooks($scope); |
||
251 | |||
252 | // The dispatcher suppresses exceptions, throw them here if there are any. |
||
253 | foreach ($callResults as $result) { |
||
254 | if ($result->hasException()) { |
||
255 | $exception = $result->getException(); |
||
256 | throw $exception; |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Create a node. |
||
263 | * |
||
264 | * @return object |
||
265 | * The created node. |
||
266 | */ |
||
267 | View Code Duplication | public function nodeCreate($node) { |
|
275 | |||
276 | /** |
||
277 | * Parse multi-value fields. Possible formats: |
||
278 | * A, B, C |
||
279 | * A - B, C - D, E - F |
||
280 | * |
||
281 | * @param string $entity_type |
||
282 | * The entity type. |
||
283 | * @param \stdClass $entity |
||
284 | * An object containing the entity properties and fields as properties. |
||
285 | */ |
||
286 | public function parseEntityFields($entity_type, \stdClass $entity) { |
||
353 | |||
354 | /** |
||
355 | * Create a user. |
||
356 | * |
||
357 | * @return object |
||
358 | * The created user. |
||
359 | */ |
||
360 | View Code Duplication | public function userCreate($user) { |
|
368 | |||
369 | /** |
||
370 | * Create a term. |
||
371 | * |
||
372 | * @return object |
||
373 | * The created term. |
||
374 | */ |
||
375 | View Code Duplication | public function termCreate($term) { |
|
383 | |||
384 | /** |
||
385 | * Creates a language. |
||
386 | * |
||
387 | * @param \stdClass $language |
||
388 | * An object with the following properties: |
||
389 | * - langcode: the langcode of the language to create. |
||
390 | * |
||
391 | * @return object|FALSE |
||
392 | * The created language, or FALSE if the language was already created. |
||
393 | */ |
||
394 | public function languageCreate(\stdClass $language) { |
||
403 | |||
404 | /** |
||
405 | * Log-in the current user. |
||
406 | */ |
||
407 | public function login() { |
||
408 | // Check if logged in. |
||
409 | if ($this->loggedIn()) { |
||
410 | $this->logout(); |
||
411 | } |
||
412 | |||
433 | |||
434 | /** |
||
435 | * Logs the current user out. |
||
436 | */ |
||
437 | public function logout() { |
||
440 | |||
441 | /** |
||
442 | * Determine if the a user is already logged in. |
||
443 | * |
||
444 | * @return boolean |
||
445 | * Returns TRUE if a user is logged in for this session. |
||
446 | */ |
||
447 | public function loggedIn() { |
||
456 | |||
457 | /** |
||
458 | * User with a given role is already logged in. |
||
459 | * |
||
460 | * @param string $role |
||
461 | * A single role, or multiple comma-separated roles in a single string. |
||
462 | * |
||
463 | * @return boolean |
||
464 | * Returns TRUE if the current logged in user has this role (or roles). |
||
465 | */ |
||
466 | public function loggedInWithRole($role) { |
||
469 | |||
470 | } |
||
471 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.