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  | 
            ||
| 12 | class DrupalDriver implements DriverInterface, SubDriverFinderInterface { | 
            ||
| 13 | |||
| 14 | /**  | 
            ||
| 15 | * Track whether Drupal has been bootstrapped.  | 
            ||
| 16 | *  | 
            ||
| 17 | * @var bool  | 
            ||
| 18 | */  | 
            ||
| 19 | private $bootstrapped = FALSE;  | 
            ||
| 20 | |||
| 21 | /**  | 
            ||
| 22 | * Drupal core object.  | 
            ||
| 23 | *  | 
            ||
| 24 | * @var \Drupal\Driver\Cores\CoreInterface  | 
            ||
| 25 | */  | 
            ||
| 26 | public $core;  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * System path to the Drupal installation.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var string  | 
            ||
| 32 | */  | 
            ||
| 33 | private $drupalRoot;  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * URI for the Drupal installation.  | 
            ||
| 37 | *  | 
            ||
| 38 | * @var string  | 
            ||
| 39 | */  | 
            ||
| 40 | private $uri;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * Drupal core version.  | 
            ||
| 44 | *  | 
            ||
| 45 | * @var int  | 
            ||
| 46 | */  | 
            ||
| 47 | public $version;  | 
            ||
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * Set Drupal root and URI.  | 
            ||
| 51 | *  | 
            ||
| 52 | * @param string $drupal_root  | 
            ||
| 53 | * The Drupal root path.  | 
            ||
| 54 | * @param string $uri  | 
            ||
| 55 | * The URI for the Drupal installation.  | 
            ||
| 56 | *  | 
            ||
| 57 | * @throws BootstrapException  | 
            ||
| 58 | * Thrown when the Drupal installation is not found in the given root path.  | 
            ||
| 59 | */  | 
            ||
| 60 |   public function __construct($drupal_root, $uri) { | 
            ||
| 61 | $this->drupalRoot = realpath($drupal_root);  | 
            ||
| 62 |     if (!$this->drupalRoot) { | 
            ||
| 63 |       throw new BootstrapException(sprintf('No Drupal installation found at %s', $drupal_root)); | 
            ||
| 64 | }  | 
            ||
| 65 | $this->uri = $uri;  | 
            ||
| 66 | $this->version = $this->getDrupalVersion();  | 
            ||
| 67 | }  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 |    * {@inheritdoc} | 
            ||
| 71 | */  | 
            ||
| 72 |   public function getRandom() { | 
            ||
| 75 | |||
| 76 | /**  | 
            ||
| 77 |    * {@inheritdoc} | 
            ||
| 78 | */  | 
            ||
| 79 |   public function bootstrap() { | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 |    * {@inheritdoc} | 
            ||
| 86 | */  | 
            ||
| 87 |   public function isBootstrapped() { | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 |    * {@inheritdoc} | 
            ||
| 94 | */  | 
            ||
| 95 |   public function userCreate(\stdClass $user) { | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 |    * {@inheritdoc} | 
            ||
| 101 | */  | 
            ||
| 102 |   public function userDelete(\stdClass $user) { | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 |    * {@inheritdoc} | 
            ||
| 108 | */  | 
            ||
| 109 |   public function processBatch() { | 
            ||
| 112 | |||
| 113 | /**  | 
            ||
| 114 |    * {@inheritdoc} | 
            ||
| 115 | */  | 
            ||
| 116 |   public function userAddRole(\stdClass $user, $role_name) { | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 |    * {@inheritdoc} | 
            ||
| 122 | */  | 
            ||
| 123 |   public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 |    * {@inheritdoc} | 
            ||
| 129 | */  | 
            ||
| 130 |   public function clearCache($type = NULL) { | 
            ||
| 133 | |||
| 134 | /**  | 
            ||
| 135 |    * {@inheritdoc} | 
            ||
| 136 | */  | 
            ||
| 137 |   public function getSubDriverPaths() { | 
            ||
| 138 | // Ensure system is bootstrapped.  | 
            ||
| 139 |     if (!$this->isBootstrapped()) { | 
            ||
| 140 | $this->bootstrap();  | 
            ||
| 141 | }  | 
            ||
| 142 | |||
| 143 | return $this->getCore()->getExtensionPathList();  | 
            ||
| 144 | }  | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * Determine major Drupal version.  | 
            ||
| 148 | *  | 
            ||
| 149 | * @return int  | 
            ||
| 150 | * The major Drupal version.  | 
            ||
| 151 | *  | 
            ||
| 152 | * @throws \Drupal\Driver\Exception\BootstrapException  | 
            ||
| 153 | * Thrown when the Drupal version could not be determined.  | 
            ||
| 154 | *  | 
            ||
| 155 | * @see drush_drupal_version()  | 
            ||
| 156 | */  | 
            ||
| 157 |   public function getDrupalVersion() { | 
            ||
| 158 |     if (!isset($this->version)) { | 
            ||
| 159 | // Support 6, 7 and 8.  | 
            ||
| 160 | $version_constant_paths = array(  | 
            ||
| 161 | // Drupal 6.  | 
            ||
| 162 | '/modules/system/system.module',  | 
            ||
| 163 | // Drupal 7.  | 
            ||
| 164 | '/includes/bootstrap.inc',  | 
            ||
| 165 | // Drupal 8.  | 
            ||
| 166 | '/autoload.php',  | 
            ||
| 167 | '/core/includes/bootstrap.inc',  | 
            ||
| 168 | );  | 
            ||
| 169 | |||
| 170 |       if ($this->drupalRoot === FALSE) { | 
            ||
| 171 |         throw new BootstrapException('`drupal_root` parameter must be defined.'); | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 |       foreach ($version_constant_paths as $path) { | 
            ||
| 175 |         if (file_exists($this->drupalRoot . $path)) { | 
            ||
| 176 | require_once $this->drupalRoot . $path;  | 
            ||
| 177 | }  | 
            ||
| 178 | }  | 
            ||
| 179 |       if (defined('VERSION')) { | 
            ||
| 180 | $version = VERSION;  | 
            ||
| 181 | }  | 
            ||
| 182 |       elseif (defined('\Drupal::VERSION')) { | 
            ||
| 183 | $version = \Drupal::VERSION;  | 
            ||
| 184 | }  | 
            ||
| 185 |       else { | 
            ||
| 186 |         throw new BootstrapException('Unable to determine Drupal core version. Supported versions are 6, 7, and 8.'); | 
            ||
| 187 | }  | 
            ||
| 188 | |||
| 189 | // Extract the major version from VERSION.  | 
            ||
| 190 |       $version_parts = explode('.', $version); | 
            ||
| 191 |       if (is_numeric($version_parts[0])) { | 
            ||
| 192 | $this->version = (integer) $version_parts[0];  | 
            ||
| 193 | }  | 
            ||
| 194 |       else { | 
            ||
| 195 |         throw new BootstrapException(sprintf('Unable to extract major Drupal core version from version string %s.', $version)); | 
            ||
| 196 | }  | 
            ||
| 197 | }  | 
            ||
| 198 | return $this->version;  | 
            ||
| 199 | }  | 
            ||
| 200 | |||
| 201 | /**  | 
            ||
| 202 | * Instantiate and set Drupal core class.  | 
            ||
| 203 | *  | 
            ||
| 204 | * @param array $available_cores  | 
            ||
| 205 | * A major-version-keyed array of available core controllers.  | 
            ||
| 206 | */  | 
            ||
| 207 |   public function setCore(array $available_cores) { | 
            ||
| 208 |     if (!isset($available_cores[$this->version])) { | 
            ||
| 209 |       throw new BootstrapException(sprintf('There is no available Drupal core controller for Drupal version %s.', $this->version)); | 
            ||
| 210 | }  | 
            ||
| 211 | $this->core = $available_cores[$this->version];  | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 | /**  | 
            ||
| 215 | * Automatically set the core from the current version.  | 
            ||
| 216 | */  | 
            ||
| 217 |   public function setCoreFromVersion() { | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * Return current core.  | 
            ||
| 224 | */  | 
            ||
| 225 |   public function getCore() { | 
            ||
| 228 | |||
| 229 | /**  | 
            ||
| 230 |    * {@inheritdoc} | 
            ||
| 231 | */  | 
            ||
| 232 |   public function createNode($node) { | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 |    * {@inheritdoc} | 
            ||
| 238 | */  | 
            ||
| 239 |   public function nodeDelete($node) { | 
            ||
| 242 | |||
| 243 | /**  | 
            ||
| 244 |    * {@inheritdoc} | 
            ||
| 245 | */  | 
            ||
| 246 |   public function runCron() { | 
            ||
| 251 | |||
| 252 | /**  | 
            ||
| 253 |    * {@inheritdoc} | 
            ||
| 254 | */  | 
            ||
| 255 |   public function createTerm(\stdClass $term) { | 
            ||
| 258 | |||
| 259 | /**  | 
            ||
| 260 |    * {@inheritdoc} | 
            ||
| 261 | */  | 
            ||
| 262 |   public function termDelete(\stdClass $term) { | 
            ||
| 265 | |||
| 266 | /**  | 
            ||
| 267 |    * {@inheritdoc} | 
            ||
| 268 | */  | 
            ||
| 269 |   public function roleCreate(array $permissions) { | 
            ||
| 272 | |||
| 273 | /**  | 
            ||
| 274 |    * {@inheritdoc} | 
            ||
| 275 | */  | 
            ||
| 276 |   public function roleDelete($rid) { | 
            ||
| 279 | |||
| 280 | /**  | 
            ||
| 281 |    * {@inheritdoc} | 
            ||
| 282 | */  | 
            ||
| 283 |   public function isField($entity_type, $field_name) { | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 |    * {@inheritdoc} | 
            ||
| 289 | */  | 
            ||
| 290 |   public function languageCreate($language) { | 
            ||
| 293 | |||
| 294 | /**  | 
            ||
| 295 |    * {@inheritdoc} | 
            ||
| 296 | */  | 
            ||
| 297 |   public function languageDelete($language) { | 
            ||
| 300 | |||
| 301 | /**  | 
            ||
| 302 |    * {@inheritdoc} | 
            ||
| 303 | */  | 
            ||
| 304 |   public function configGet($name, $key) { | 
            ||
| 307 | |||
| 308 | /**  | 
            ||
| 309 |    * {@inheritdoc} | 
            ||
| 310 | */  | 
            ||
| 311 |   public function configSet($name, $key, $value) { | 
            ||
| 314 | |||
| 315 | /**  | 
            ||
| 316 |    * {@inheritdoc} | 
            ||
| 317 | */  | 
            ||
| 318 |   public function clearStaticCaches() { | 
            ||
| 321 | |||
| 322 | /**  | 
            ||
| 323 | * Place block.  | 
            ||
| 324 | */  | 
            ||
| 325 |   public function placeBlock($delta, $module, $region) { | 
            ||
| 328 | |||
| 329 | }  | 
            ||
| 330 | 
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: