Complex classes like DrupalDriver 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 DrupalDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class DrupalDriver implements DriverInterface, SubDriverFinderInterface { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Track whether Drupal has been bootstrapped. |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | private $bootstrapped = FALSE; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Drupal core object. |
||
| 29 | * |
||
| 30 | * @var \Drupal\Driver\Cores\CoreInterface |
||
| 31 | */ |
||
| 32 | public $core; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * System path to the Drupal installation. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $drupalRoot; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * URI for the Drupal installation. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $uri; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Drupal core version. |
||
| 50 | * |
||
| 51 | * @var integer |
||
| 52 | */ |
||
| 53 | public $version; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Set Drupal root and URI. |
||
| 57 | * |
||
| 58 | * @param string $drupal_root |
||
| 59 | * The Drupal root path. |
||
| 60 | * @param string $uri |
||
| 61 | * The URI for the Drupal installation. |
||
| 62 | * |
||
| 63 | * @throws BootstrapException |
||
| 64 | * Thrown when the Drupal installation is not found in the given root path. |
||
| 65 | */ |
||
| 66 | public function __construct($drupal_root, $uri) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function getRandom() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function bootstrap() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function isBootstrapped() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function userCreate(\stdClass $user) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | public function userDelete(\stdClass $user) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | public function processBatch() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function userAddRole(\stdClass $user, $role_name) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function clearCache($type = NULL) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function getSubDriverPaths() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Determine major Drupal version. |
||
| 154 | * |
||
| 155 | * @return int |
||
| 156 | * The major Drupal version. |
||
| 157 | * |
||
| 158 | * @throws \Drupal\Driver\Exception\BootstrapException |
||
| 159 | * Thrown when the Drupal version could not be determined. |
||
| 160 | * |
||
| 161 | * @see drush_drupal_version() |
||
| 162 | */ |
||
| 163 | public function getDrupalVersion() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Instantiate and set Drupal core class. |
||
| 209 | * |
||
| 210 | * @param array $available_cores |
||
| 211 | * A major-version-keyed array of available core controllers. |
||
| 212 | */ |
||
| 213 | public function setCore(array $available_cores) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Automatically set the core from the current version. |
||
| 222 | */ |
||
| 223 | public function setCoreFromVersion() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Return current core. |
||
| 230 | */ |
||
| 231 | public function getCore() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | public function createNode($node) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | public function nodeDelete($node) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function runCron() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function createTerm(\stdClass $term) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function termDelete(\stdClass $term) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function roleCreate(array $permissions) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function roleDelete($rid) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function isField($entity_type, $field_name) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function languageCreate($language) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | public function languageDelete($language) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritdoc} |
||
| 309 | */ |
||
| 310 | public function installModules(array $modules) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritdoc} |
||
| 316 | */ |
||
| 317 | public function uninstallModules(array $modules) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritdoc} |
||
| 323 | */ |
||
| 324 | public function getModuleList() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | public function clearStaticCaches() { |
||
| 334 | |||
| 335 | } |
||
| 336 |