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) { | ||
| 173 | |||
| 174 | /** | ||
| 175 |    * {@inheritdoc} | ||
| 176 | */ | ||
| 177 |   public function userDelete(\stdClass $user) { | ||
| 185 | |||
| 186 | /** | ||
| 187 |    * {@inheritdoc} | ||
| 188 | */ | ||
| 189 |   public function userAddRole(\stdClass $user, $role) { | ||
| 196 | |||
| 197 | /** | ||
| 198 |    * {@inheritdoc} | ||
| 199 | */ | ||
| 200 |   public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { | ||
| 208 | |||
| 209 | /** | ||
| 210 |    * {@inheritdoc} | ||
| 211 | */ | ||
| 212 |   public function clearCache($type = 'all') { | ||
| 216 | |||
| 217 | /** | ||
| 218 |    * {@inheritdoc} | ||
| 219 | */ | ||
| 220 |   public function clearStaticCaches() { | ||
| 224 | |||
| 225 | /** | ||
| 226 | * Decodes JSON object returned by Drush. | ||
| 227 | * | ||
| 228 | * It will clean up any junk that may have appeared before or after the | ||
| 229 | * JSON object. This can happen with remote Drush aliases. | ||
| 230 | * | ||
| 231 | * @param string $output | ||
| 232 | * The output from Drush. | ||
| 233 | * | ||
| 234 | * @return object | ||
| 235 | * The decoded JSON object. | ||
| 236 | */ | ||
| 237 |   protected function decodeJsonObject($output) { | ||
| 244 | |||
| 245 | /** | ||
| 246 |    * {@inheritdoc} | ||
| 247 | */ | ||
| 248 |   public function createNode($node) { | ||
| 252 | |||
| 253 | /** | ||
| 254 |    * {@inheritdoc} | ||
| 255 | */ | ||
| 256 |   public function nodeDelete($node) { | ||
| 259 | |||
| 260 | /** | ||
| 261 |    * {@inheritdoc} | ||
| 262 | */ | ||
| 263 |   public function createTerm(\stdClass $term) { | ||
| 267 | |||
| 268 | /** | ||
| 269 |    * {@inheritdoc} | ||
| 270 | */ | ||
| 271 |   public function termDelete(\stdClass $term) { | ||
| 274 | |||
| 275 | /** | ||
| 276 |    * {@inheritdoc} | ||
| 277 | */ | ||
| 278 |   public function isField($entity_type, $field_name) { | ||
| 294 | |||
| 295 | /** | ||
| 296 | * Sets common drush arguments or options. | ||
| 297 | * | ||
| 298 | * @param string $arguments | ||
| 299 | * Global arguments to add to every drush command. | ||
| 300 | */ | ||
| 301 |   public function setArguments($arguments) { | ||
| 304 | |||
| 305 | /** | ||
| 306 | * Get common drush arguments. | ||
| 307 | */ | ||
| 308 |   public function getArguments() { | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Parse arguments into a string. | ||
| 314 | * | ||
| 315 | * @param array $arguments | ||
| 316 | * An array of argument/option names to values. | ||
| 317 | * | ||
| 318 | * @return string | ||
| 319 | * The parsed arguments. | ||
| 320 | */ | ||
| 321 |   protected static function parseArguments(array $arguments) { | ||
| 333 | |||
| 334 | /** | ||
| 335 | * Execute a drush command. | ||
| 336 | */ | ||
| 337 |   public function drush($command, array $arguments = array(), array $options = array()) { | ||
| 373 | |||
| 374 | /** | ||
| 375 |    * {@inheritdoc} | ||
| 376 | */ | ||
| 377 |   public function processBatch() { | ||
| 381 | |||
| 382 | /** | ||
| 383 |    * {@inheritdoc} | ||
| 384 | */ | ||
| 385 |   public function runCron() { | ||
| 388 | |||
| 389 | /** | ||
| 390 | * Run Drush commands dynamically from a DrupalContext. | ||
| 391 | */ | ||
| 392 |   public function __call($name, $arguments) { | ||
| 395 | |||
| 396 | } | ||
| 397 |