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) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public function userDelete(\stdClass $user) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function userAddRole(\stdClass $user, $role) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function clearCache($type = 'all') { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function clearStaticCaches() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Decodes JSON object returned by Drush. |
||
| 234 | * |
||
| 235 | * It will clean up any junk that may have appeared before or after the |
||
| 236 | * JSON object. This can happen with remote Drush aliases. |
||
| 237 | * |
||
| 238 | * @param string $output |
||
| 239 | * The output from Drush. |
||
| 240 | * |
||
| 241 | * @return object |
||
| 242 | * The decoded JSON object. |
||
| 243 | */ |
||
| 244 | protected function decodeJsonObject($output) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function createNode($node) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | public function nodeDelete($node) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function createTerm(\stdClass $term) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function termDelete(\stdClass $term) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | public function isField($entity_type, $field_name) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Sets common drush arguments or options. |
||
| 304 | * |
||
| 305 | * @param string $arguments |
||
| 306 | * Global arguments to add to every drush command. |
||
| 307 | */ |
||
| 308 | public function setArguments($arguments) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get common drush arguments. |
||
| 314 | */ |
||
| 315 | public function getArguments() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Parse arguments into a string. |
||
| 321 | * |
||
| 322 | * @param array $arguments |
||
| 323 | * An array of argument/option names to values. |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | * The parsed arguments. |
||
| 327 | */ |
||
| 328 | protected static function parseArguments(array $arguments) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Execute a drush command. |
||
| 343 | */ |
||
| 344 | public function drush($command, array $arguments = array(), array $options = array()) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function processBatch() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | public function runCron() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Run Drush commands dynamically from a DrupalContext. |
||
| 398 | */ |
||
| 399 | public function __call($name, $arguments) { |
||
| 402 | |||
| 403 | } |
||
| 404 |