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 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() { |
||
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 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.