Complex classes like DrushDriver 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 DrushDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class DrushDriver extends BaseDriver { |
||
14 | /** |
||
15 | * Store a drush alias for tests requiring shell access. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $alias; |
||
20 | |||
21 | /** |
||
22 | * Stores the root path to a Drupal installation. |
||
23 | * |
||
24 | * This is an alternative to using drush aliases. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | public $root; |
||
29 | |||
30 | /** |
||
31 | * Store the path to drush binary. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | public $binary; |
||
36 | |||
37 | /** |
||
38 | * Track bootstrapping. |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $bootstrapped = FALSE; |
||
43 | |||
44 | /** |
||
45 | * Random generator. |
||
46 | * |
||
47 | * @var \Drupal\Component\Utility\Random |
||
48 | */ |
||
49 | private $random; |
||
50 | |||
51 | /** |
||
52 | * Global arguments or options for drush commands. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $arguments = ''; |
||
57 | |||
58 | /** |
||
59 | * Tracks legacy drush. |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected static $isLegacyDrush; |
||
64 | |||
65 | /** |
||
66 | * Set drush alias or root path. |
||
67 | * |
||
68 | * @param string $alias |
||
69 | * A drush alias. |
||
70 | * @param string $root_path |
||
71 | * The root path of the Drupal install. This is an alternative to using |
||
72 | * aliases. |
||
73 | * @param string $binary |
||
74 | * The path to the drush binary. |
||
75 | * @param \Drupal\Component\Utility\Random $random |
||
76 | * Random generator. |
||
77 | * |
||
78 | * @throws \Drupal\Driver\Exception\BootstrapException |
||
79 | * Thrown when a required parameter is missing. |
||
80 | */ |
||
81 | public function __construct($alias = NULL, $root_path = NULL, $binary = 'drush', Random $random = NULL) { |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function getRandom() { |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function bootstrap() { |
||
128 | |||
129 | /** |
||
130 | * Determine if drush is a legacy version. |
||
131 | * |
||
132 | * @return bool |
||
133 | * Returns TRUE if drush is older than drush 9. |
||
134 | */ |
||
135 | protected function isLegacyDrush() { |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function isBootstrapped() { |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function userCreate(\stdClass $user) { |
||
176 | |||
177 | /** |
||
178 | * Parse user id from drush user-information output. |
||
179 | */ |
||
180 | protected function parseUserId($info) { |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function userDelete(\stdClass $user) { |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function userAddRole(\stdClass $user, $role) { |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function clearCache($type = 'all') { |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function clearStaticCaches() { |
||
240 | |||
241 | /** |
||
242 | * Decodes JSON object returned by Drush. |
||
243 | * |
||
244 | * It will clean up any junk that may have appeared before or after the |
||
245 | * JSON object. This can happen with remote Drush aliases. |
||
246 | * |
||
247 | * @param string $output |
||
248 | * The output from Drush. |
||
249 | * |
||
250 | * @return object |
||
251 | * The decoded JSON object. |
||
252 | */ |
||
253 | protected function decodeJsonObject($output) { |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function createNode($node) { |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | public function nodeDelete($node) { |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function createTerm(\stdClass $term) { |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function termDelete(\stdClass $term) { |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function isField($entity_type, $field_name) { |
||
317 | |||
318 | /** |
||
319 | * Sets common drush arguments or options. |
||
320 | * |
||
321 | * @param string $arguments |
||
322 | * Global arguments to add to every drush command. |
||
323 | */ |
||
324 | public function setArguments($arguments) { |
||
327 | |||
328 | /** |
||
329 | * Get common drush arguments. |
||
330 | */ |
||
331 | public function getArguments() { |
||
334 | |||
335 | /** |
||
336 | * Parse arguments into a string. |
||
337 | * |
||
338 | * @param array $arguments |
||
339 | * An array of argument/option names to values. |
||
340 | * |
||
341 | * @return string |
||
342 | * The parsed arguments. |
||
343 | */ |
||
344 | protected static function parseArguments(array $arguments) { |
||
356 | |||
357 | /** |
||
358 | * Execute a drush command. |
||
359 | */ |
||
360 | public function drush($command, array $arguments = array(), array $options = array()) { |
||
396 | |||
397 | /** |
||
398 | * {@inheritdoc} |
||
399 | */ |
||
400 | public function processBatch() { |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function runCron() { |
||
411 | |||
412 | /** |
||
413 | * Run Drush commands dynamically from a DrupalContext. |
||
414 | */ |
||
415 | public function __call($name, $arguments) { |
||
418 | |||
419 | } |
||
420 |