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 |
||
12 | class DrupalDriver implements DriverInterface, SubDriverFinderInterface { |
||
13 | |||
14 | /** |
||
15 | * Track whether Drupal has been bootstrapped. |
||
16 | * |
||
17 | * @var bool |
||
18 | */ |
||
19 | private $bootstrapped = FALSE; |
||
20 | |||
21 | /** |
||
22 | * Drupal core object. |
||
23 | * |
||
24 | * @var \Drupal\Driver\Cores\CoreInterface |
||
25 | */ |
||
26 | public $core; |
||
27 | |||
28 | /** |
||
29 | * System path to the Drupal installation. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $drupalRoot; |
||
34 | |||
35 | /** |
||
36 | * URI for the Drupal installation. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $uri; |
||
41 | |||
42 | /** |
||
43 | * Drupal core version. |
||
44 | * |
||
45 | * @var integer |
||
46 | */ |
||
47 | public $version; |
||
48 | |||
49 | /** |
||
50 | * Set Drupal root and URI. |
||
51 | * |
||
52 | * @param string $drupal_root |
||
53 | * The Drupal root path. |
||
54 | * @param string $uri |
||
55 | * The URI for the Drupal installation. |
||
56 | * |
||
57 | * @throws BootstrapException |
||
58 | * Thrown when the Drupal installation is not found in the given root path. |
||
59 | */ |
||
60 | public function __construct($drupal_root, $uri) { |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function getRandom() { |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function bootstrap() { |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function isBootstrapped() { |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function userCreate(\stdClass $user) { |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function userAlter($user, $values) { |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function userDelete(\stdClass $user) { |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function processBatch() { |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function userAddRole(\stdClass $user, $role_name) { |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function clearCache($type = NULL) { |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function getSubDriverPaths() { |
||
152 | |||
153 | /** |
||
154 | * Determine major Drupal version. |
||
155 | * |
||
156 | * @return int |
||
157 | * The major Drupal version. |
||
158 | * |
||
159 | * @throws \Drupal\Driver\Exception\BootstrapException |
||
160 | * Thrown when the Drupal version could not be determined. |
||
161 | * |
||
162 | * @see drush_drupal_version() |
||
163 | */ |
||
164 | public function getDrupalVersion() { |
||
207 | |||
208 | /** |
||
209 | * Instantiate and set Drupal core class. |
||
210 | * |
||
211 | * @param array $available_cores |
||
212 | * A major-version-keyed array of available core controllers. |
||
213 | */ |
||
214 | public function setCore(array $available_cores) { |
||
220 | |||
221 | /** |
||
222 | * Automatically set the core from the current version. |
||
223 | */ |
||
224 | public function setCoreFromVersion() { |
||
228 | |||
229 | /** |
||
230 | * Return current core. |
||
231 | */ |
||
232 | public function getCore() { |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function createNode($node) { |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function nodeAlter($node, $values) { |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | public function nodeDelete($node) { |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function runCron() { |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function createTerm(\stdClass $term) { |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function termDelete(\stdClass $term) { |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function roleCreate(array $permissions) { |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | public function roleDelete($rid) { |
||
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | */ |
||
297 | public function isField($entity_type, $field_name) { |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function languageCreate($language) { |
||
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function languageDelete($language) { |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function configGet($name, $key) { |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function configSet($name, $key, $value) { |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function clearStaticCaches() { |
||
335 | |||
336 | /** |
||
337 | * {@inheritdoc} |
||
338 | */ |
||
339 | public function nodeDeleteMultiple(array $nids) { |
||
342 | |||
343 | /** |
||
344 | * {@inheritdoc} |
||
345 | */ |
||
346 | public function userDeleteMultiple(array $uids) { |
||
349 | |||
350 | } |
||
351 |