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() { | ||
| 145 | |||
| 146 | /** | ||
| 147 |    * {@inheritdoc} | ||
| 148 | */ | ||
| 149 |   public function isBootstrapped() { | ||
| 152 | |||
| 153 | /** | ||
| 154 |    * {@inheritdoc} | ||
| 155 | */ | ||
| 156 |   public function userCreate(\stdClass $user) { | ||
| 171 | |||
| 172 | /** | ||
| 173 |    * {@inheritdoc} | ||
| 174 | */ | ||
| 175 |   public function userDelete(\stdClass $user) { | ||
| 183 | |||
| 184 | /** | ||
| 185 |    * {@inheritdoc} | ||
| 186 | */ | ||
| 187 |   public function userAddRole(\stdClass $user, $role) { | ||
| 194 | |||
| 195 | /** | ||
| 196 |    * {@inheritdoc} | ||
| 197 | */ | ||
| 198 |   public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { | ||
| 206 | |||
| 207 | /** | ||
| 208 |    * {@inheritdoc} | ||
| 209 | */ | ||
| 210 |   public function clearCache($type = 'all') { | ||
| 214 | |||
| 215 | /** | ||
| 216 |    * {@inheritdoc} | ||
| 217 | */ | ||
| 218 |   public function clearStaticCaches() { | ||
| 222 | |||
| 223 | /** | ||
| 224 | * Decodes JSON object returned by Drush. | ||
| 225 | * | ||
| 226 | * It will clean up any junk that may have appeared before or after the | ||
| 227 | * JSON object. This can happen with remote Drush aliases. | ||
| 228 | * | ||
| 229 | * @param string $output | ||
| 230 | * The output from Drush. | ||
| 231 | * | ||
| 232 | * @return object | ||
| 233 | * The decoded JSON object. | ||
| 234 | */ | ||
| 235 |   protected function decodeJsonObject($output) { | ||
| 242 | |||
| 243 | /** | ||
| 244 |    * {@inheritdoc} | ||
| 245 | */ | ||
| 246 |   public function createNode($node) { | ||
| 250 | |||
| 251 | /** | ||
| 252 |    * {@inheritdoc} | ||
| 253 | */ | ||
| 254 |   public function nodeDelete($node) { | ||
| 257 | |||
| 258 | /** | ||
| 259 |    * {@inheritdoc} | ||
| 260 | */ | ||
| 261 |   public function createTerm(\stdClass $term) { | ||
| 265 | |||
| 266 | /** | ||
| 267 |    * {@inheritdoc} | ||
| 268 | */ | ||
| 269 |   public function termDelete(\stdClass $term) { | ||
| 272 | |||
| 273 | /** | ||
| 274 |    * {@inheritdoc} | ||
| 275 | */ | ||
| 276 |   public function isField($entity_type, $field_name) { | ||
| 292 | |||
| 293 | /** | ||
| 294 | * Sets common drush arguments or options. | ||
| 295 | * | ||
| 296 | * @param string $arguments | ||
| 297 | * Global arguments to add to every drush command. | ||
| 298 | */ | ||
| 299 |   public function setArguments($arguments) { | ||
| 302 | |||
| 303 | /** | ||
| 304 | * Get common drush arguments. | ||
| 305 | */ | ||
| 306 |   public function getArguments() { | ||
| 309 | |||
| 310 | /** | ||
| 311 | * Parse arguments into a string. | ||
| 312 | * | ||
| 313 | * @param array $arguments | ||
| 314 | * An array of argument/option names to values. | ||
| 315 | * | ||
| 316 | * @return string | ||
| 317 | * The parsed arguments. | ||
| 318 | */ | ||
| 319 |   protected static function parseArguments(array $arguments) { | ||
| 331 | |||
| 332 | /** | ||
| 333 | * Execute a drush command. | ||
| 334 | */ | ||
| 335 |   public function drush($command, array $arguments = array(), array $options = array()) { | ||
| 371 | |||
| 372 | /** | ||
| 373 |    * {@inheritdoc} | ||
| 374 | */ | ||
| 375 |   public function processBatch() { | ||
| 379 | |||
| 380 | /** | ||
| 381 |    * {@inheritdoc} | ||
| 382 | */ | ||
| 383 |   public function runCron() { | ||
| 386 | |||
| 387 | /** | ||
| 388 | * Run Drush commands dynamically from a DrupalContext. | ||
| 389 | */ | ||
| 390 |   public function __call($name, $arguments) { | ||
| 393 | |||
| 394 | } | ||
| 395 |