Complex classes like DrupalDriver 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 DrupalDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class DrupalDriver implements DriverInterface, SubDriverFinderInterface, AuthenticationDriverInterface { |
||
14 | |||
15 | /** |
||
16 | * Track whether Drupal has been bootstrapped. |
||
17 | * |
||
18 | * @var bool |
||
19 | */ |
||
20 | private $bootstrapped = FALSE; |
||
21 | |||
22 | /** |
||
23 | * Drupal core object. |
||
24 | * |
||
25 | * @var \Drupal\Driver\Cores\CoreInterface |
||
26 | */ |
||
27 | public $core; |
||
28 | |||
29 | /** |
||
30 | * System path to the Drupal installation. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $drupalRoot; |
||
35 | |||
36 | /** |
||
37 | * URI for the Drupal installation. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private $uri; |
||
42 | |||
43 | /** |
||
44 | * Drupal core version. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | public $version; |
||
49 | |||
50 | /** |
||
51 | * Set Drupal root and URI. |
||
52 | * |
||
53 | * @param string $drupal_root |
||
54 | * The Drupal root path. |
||
55 | * @param string $uri |
||
56 | * The URI for the Drupal installation. |
||
57 | * |
||
58 | * @throws \Drupal\Driver\Exception\BootstrapException |
||
59 | * Thrown when the Drupal installation is not found in the given root path. |
||
60 | */ |
||
61 | public function __construct($drupal_root, $uri) { |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function getRandom() { |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function bootstrap() { |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function isBootstrapped() { |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function userCreate(\stdClass $user) { |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function userDelete(\stdClass $user) { |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function processBatch() { |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function userAddRole(\stdClass $user, $role_name) { |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function clearCache($type = NULL) { |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function getSubDriverPaths() { |
||
146 | |||
147 | /** |
||
148 | * Determine major Drupal version. |
||
149 | * |
||
150 | * @return int |
||
151 | * The major Drupal version. |
||
152 | * |
||
153 | * @throws \Drupal\Driver\Exception\BootstrapException |
||
154 | * Thrown when the Drupal version could not be determined. |
||
155 | * |
||
156 | * @see drush_drupal_version() |
||
157 | */ |
||
158 | public function getDrupalVersion() { |
||
159 | if (!isset($this->version)) { |
||
160 | // Support 6, 7 and 8. |
||
161 | $version_constant_paths = [ |
||
162 | // Drupal 6. |
||
163 | '/modules/system/system.module', |
||
164 | // Drupal 7. |
||
165 | '/includes/bootstrap.inc', |
||
166 | // Drupal 8. |
||
167 | '/autoload.php', |
||
168 | '/core/includes/bootstrap.inc', |
||
169 | ]; |
||
170 | |||
171 | if ($this->drupalRoot === FALSE) { |
||
172 | throw new BootstrapException('`drupal_root` parameter must be defined.'); |
||
173 | } |
||
174 | |||
175 | foreach ($version_constant_paths as $path) { |
||
176 | if (file_exists($this->drupalRoot . $path)) { |
||
177 | require_once $this->drupalRoot . $path; |
||
178 | } |
||
179 | } |
||
180 | if (defined('VERSION')) { |
||
181 | $version = VERSION; |
||
182 | } |
||
183 | elseif (defined('\Drupal::VERSION')) { |
||
184 | $version = \Drupal::VERSION; |
||
185 | } |
||
186 | else { |
||
187 | throw new BootstrapException('Unable to determine Drupal core version. Supported versions are 6, 7, and 8.'); |
||
188 | } |
||
189 | |||
190 | // Extract the major version from VERSION. |
||
191 | $version_parts = explode('.', $version); |
||
192 | if (is_numeric($version_parts[0])) { |
||
193 | $this->version = (integer) $version_parts[0] < 8 ? $version_parts[0] : 8; |
||
194 | } |
||
195 | else { |
||
196 | throw new BootstrapException(sprintf('Unable to extract major Drupal core version from version string %s.', $version)); |
||
197 | } |
||
198 | } |
||
199 | return $this->version; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Instantiate and set Drupal core class. |
||
204 | * |
||
205 | * @param array $available_cores |
||
206 | * A major-version-keyed array of available core controllers. |
||
207 | */ |
||
208 | public function setCore(array $available_cores) { |
||
214 | |||
215 | /** |
||
216 | * Automatically set the core from the current version. |
||
217 | */ |
||
218 | public function setCoreFromVersion() { |
||
222 | |||
223 | /** |
||
224 | * Return current core. |
||
225 | */ |
||
226 | public function getCore() { |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function createNode($node) { |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function nodeDelete($node) { |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function runCron() { |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function createTerm(\stdClass $term) { |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function termDelete(\stdClass $term) { |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function roleCreate(array $permissions) { |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function roleDelete($rid) { |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function isField($entity_type, $field_name) { |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function languageCreate($language) { |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | public function languageDelete($language) { |
||
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | public function configGet($name, $key) { |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function configSet($name, $key, $value) { |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function clearStaticCaches() { |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function createEntity($entity_type, \stdClass $entity) { |
||
329 | |||
330 | /** |
||
331 | * {@inheritdoc} |
||
332 | */ |
||
333 | public function entityDelete($entity_type, \stdClass $entity) { |
||
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | public function startCollectingMail() { |
||
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | public function stopCollectingMail() { |
||
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | public function getMail() { |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | public function clearMail() { |
||
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | public function sendMail($body, $subject, $to, $langcode) { |
||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | public function login(\stdClass $user) { |
||
380 | |||
381 | /** |
||
382 | * {@inheritdoc} |
||
383 | */ |
||
384 | public function logout() { |
||
389 | |||
390 | } |
||
391 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.