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 version. |
||
60 | */ |
||
61 | protected static $isLegacyDrush; |
||
62 | |||
63 | /** |
||
64 | * Set drush alias or root path. |
||
65 | * |
||
66 | * @param string $alias |
||
67 | * A drush alias. |
||
68 | * @param string $root_path |
||
69 | * The root path of the Drupal install. This is an alternative to using |
||
70 | * aliases. |
||
71 | * @param string $binary |
||
72 | * The path to the drush binary. |
||
73 | * @param \Drupal\Component\Utility\Random $random |
||
74 | * Random generator. |
||
75 | * |
||
76 | * @throws \Drupal\Driver\Exception\BootstrapException |
||
77 | * Thrown when a required parameter is missing. |
||
78 | */ |
||
79 | public function __construct($alias = NULL, $root_path = NULL, $binary = 'drush', Random $random = NULL) { |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function getRandom() { |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function bootstrap() { |
||
126 | |||
127 | /** |
||
128 | * Determine if drush is a legacy version. |
||
129 | * |
||
130 | * @return bool |
||
131 | * Returns TRUE if drush is older than drush 9. |
||
132 | */ |
||
133 | protected function isLegacyDrush() { |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function isBootstrapped() { |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function userCreate(\stdClass $user) { |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function userDelete(\stdClass $user) { |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function userAddRole(\stdClass $user, $role) { |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function clearCache($type = 'all') { |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function clearStaticCaches() { |
||
220 | |||
221 | /** |
||
222 | * Decodes JSON object returned by Drush. |
||
223 | * |
||
224 | * It will clean up any junk that may have appeared before or after the |
||
225 | * JSON object. This can happen with remote Drush aliases. |
||
226 | * |
||
227 | * @param string $output |
||
228 | * The output from Drush. |
||
229 | * |
||
230 | * @return object |
||
231 | * The decoded JSON object. |
||
232 | */ |
||
233 | protected function decodeJsonObject($output) { |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | public function createNode($node) { |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | public function nodeDelete($node) { |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function createTerm(\stdClass $term) { |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function termDelete(\stdClass $term) { |
||
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | public function isField($entity_type, $field_name) { |
||
290 | |||
291 | /** |
||
292 | * Sets common drush arguments or options. |
||
293 | * |
||
294 | * @param string $arguments |
||
295 | * Global arguments to add to every drush command. |
||
296 | */ |
||
297 | public function setArguments($arguments) { |
||
300 | |||
301 | /** |
||
302 | * Get common drush arguments. |
||
303 | */ |
||
304 | public function getArguments() { |
||
307 | |||
308 | /** |
||
309 | * Parse arguments into a string. |
||
310 | * |
||
311 | * @param array $arguments |
||
312 | * An array of argument/option names to values. |
||
313 | * |
||
314 | * @return string |
||
315 | * The parsed arguments. |
||
316 | */ |
||
317 | protected static function parseArguments(array $arguments) { |
||
329 | |||
330 | /** |
||
331 | * Execute a drush command. |
||
332 | */ |
||
333 | public function drush($command, array $arguments = array(), array $options = array()) { |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function processBatch() { |
||
377 | |||
378 | /** |
||
379 | * {@inheritdoc} |
||
380 | */ |
||
381 | public function runCron() { |
||
384 | |||
385 | /** |
||
386 | * Run Drush commands dynamically from a DrupalContext. |
||
387 | */ |
||
388 | public function __call($name, $arguments) { |
||
391 | |||
392 | } |
||
393 |