Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RawDrupalContext 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 RawDrupalContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 28 | class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface { | 
            ||
| 29 | |||
| 30 | use DrupalParametersTrait;  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * Drupal driver manager.  | 
            ||
| 34 | *  | 
            ||
| 35 | * @var \Drupal\DrupalDriverManager  | 
            ||
| 36 | */  | 
            ||
| 37 | private $drupal;  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * Event dispatcher object.  | 
            ||
| 41 | *  | 
            ||
| 42 | * @var \Behat\Testwork\Hook\HookDispatcher  | 
            ||
| 43 | */  | 
            ||
| 44 | protected $dispatcher;  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * Drupal authentication manager.  | 
            ||
| 48 | *  | 
            ||
| 49 | * @var \Drupal\DrupalExtension\Manager\DrupalAuthenticationManagerInterface  | 
            ||
| 50 | */  | 
            ||
| 51 | protected $authenticationManager;  | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * Drupal user manager.  | 
            ||
| 55 | *  | 
            ||
| 56 | * @var \Drupal\DrupalExtension\Manager\DrupalUserManagerInterface  | 
            ||
| 57 | */  | 
            ||
| 58 | protected $userManager;  | 
            ||
| 59 | |||
| 60 | /**  | 
            ||
| 61 | * Keep track of nodes so they can be cleaned up.  | 
            ||
| 62 | *  | 
            ||
| 63 | * @var array  | 
            ||
| 64 | */  | 
            ||
| 65 | protected $nodes = array();  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * Keep track of all terms that are created so they can easily be removed.  | 
            ||
| 69 | *  | 
            ||
| 70 | * @var array  | 
            ||
| 71 | */  | 
            ||
| 72 | protected $terms = array();  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * Keep track of any roles that are created so they can easily be removed.  | 
            ||
| 76 | *  | 
            ||
| 77 | * @var array  | 
            ||
| 78 | */  | 
            ||
| 79 | protected $roles = array();  | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * Keep track of any languages that are created so they can easily be removed.  | 
            ||
| 83 | *  | 
            ||
| 84 | * @var array  | 
            ||
| 85 | */  | 
            ||
| 86 | protected $languages = array();  | 
            ||
| 87 | |||
| 88 | /**  | 
            ||
| 89 |    * {@inheritDoc} | 
            ||
| 90 | */  | 
            ||
| 91 |   public function setDrupal(DrupalDriverManager $drupal) { | 
            ||
| 94 | |||
| 95 | /**  | 
            ||
| 96 |    * {@inheritDoc} | 
            ||
| 97 | */  | 
            ||
| 98 |   public function getDrupal() { | 
            ||
| 101 | |||
| 102 | /**  | 
            ||
| 103 |    * {@inheritDoc} | 
            ||
| 104 | */  | 
            ||
| 105 |   public function setUserManager(DrupalUserManagerInterface $userManager) { | 
            ||
| 108 | |||
| 109 | /**  | 
            ||
| 110 |    * {@inheritdoc} | 
            ||
| 111 | */  | 
            ||
| 112 |   public function getUserManager() { | 
            ||
| 115 | |||
| 116 | /**  | 
            ||
| 117 |    * {@inheritdoc} | 
            ||
| 118 | */  | 
            ||
| 119 |   public function setAuthenticationManager(DrupalAuthenticationManagerInterface $authenticationManager) { | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 |    * {@inheritdoc} | 
            ||
| 125 | */  | 
            ||
| 126 |   public function getAuthenticationManager() { | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * Magic setter.  | 
            ||
| 132 | */  | 
            ||
| 133 |   public function __set($name, $value) { | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * Magic getter.  | 
            ||
| 160 | */  | 
            ||
| 161 |   public function __get($name) { | 
            ||
| 176 | |||
| 177 | /**  | 
            ||
| 178 |    * {@inheritdoc} | 
            ||
| 179 | */  | 
            ||
| 180 |   public function setDispatcher(HookDispatcher $dispatcher) { | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * Get active Drupal Driver.  | 
            ||
| 186 | *  | 
            ||
| 187 | * @return \Drupal\Driver\DrupalDriver  | 
            ||
| 188 | */  | 
            ||
| 189 |   public function getDriver($name = NULL) { | 
            ||
| 192 | |||
| 193 | /**  | 
            ||
| 194 | * Get driver's random generator.  | 
            ||
| 195 | */  | 
            ||
| 196 |   public function getRandom() { | 
            ||
| 199 | |||
| 200 | /**  | 
            ||
| 201 | * Massage node values to match the expectations on different Drupal versions.  | 
            ||
| 202 | *  | 
            ||
| 203 | * @beforeNodeCreate  | 
            ||
| 204 | */  | 
            ||
| 205 |   public static function alterNodeParameters(BeforeNodeCreateScope $scope) { | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * Remove any created nodes.  | 
            ||
| 230 | *  | 
            ||
| 231 | * @AfterScenario  | 
            ||
| 232 | */  | 
            ||
| 233 |   public function cleanNodes() { | 
            ||
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * Remove any created users.  | 
            ||
| 243 | *  | 
            ||
| 244 | * @AfterScenario  | 
            ||
| 245 | */  | 
            ||
| 246 |   public function cleanUsers() { | 
            ||
| 259 | |||
| 260 | /**  | 
            ||
| 261 | * Remove any created terms.  | 
            ||
| 262 | *  | 
            ||
| 263 | * @AfterScenario  | 
            ||
| 264 | */  | 
            ||
| 265 |   public function cleanTerms() { | 
            ||
| 272 | |||
| 273 | /**  | 
            ||
| 274 | * Remove any created roles.  | 
            ||
| 275 | *  | 
            ||
| 276 | * @AfterScenario  | 
            ||
| 277 | */  | 
            ||
| 278 |   public function cleanRoles() { | 
            ||
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * Remove any created languages.  | 
            ||
| 288 | *  | 
            ||
| 289 | * @AfterScenario  | 
            ||
| 290 | */  | 
            ||
| 291 |   public function cleanLanguages() { | 
            ||
| 298 | |||
| 299 | /**  | 
            ||
| 300 | * Clear static caches.  | 
            ||
| 301 | *  | 
            ||
| 302 | * @AfterScenario @api  | 
            ||
| 303 | */  | 
            ||
| 304 |   public function clearStaticCaches() { | 
            ||
| 307 | |||
| 308 | /**  | 
            ||
| 309 | * Dispatch scope hooks.  | 
            ||
| 310 | *  | 
            ||
| 311 | * @param string $scope  | 
            ||
| 312 | * The entity scope to dispatch.  | 
            ||
| 313 | * @param \stdClass $entity  | 
            ||
| 314 | * The entity.  | 
            ||
| 315 | */  | 
            ||
| 316 |   protected function dispatchHooks($scopeType, \stdClass $entity) { | 
            ||
| 329 | |||
| 330 | /**  | 
            ||
| 331 | * Create a node.  | 
            ||
| 332 | *  | 
            ||
| 333 | * @return object  | 
            ||
| 334 | * The created node.  | 
            ||
| 335 | */  | 
            ||
| 336 | View Code Duplication |   public function nodeCreate($node) { | 
            |
| 344 | |||
| 345 | /**  | 
            ||
| 346 | * Parses the field values and turns them into the format expected by Drupal.  | 
            ||
| 347 | *  | 
            ||
| 348 | * Multiple values in a single field must be separated by commas. Wrap the  | 
            ||
| 349 | * field value in double quotes in case it should contain a comma.  | 
            ||
| 350 | *  | 
            ||
| 351 | * Compound field properties are identified using a ':' operator, either in  | 
            ||
| 352 | * the column heading or in the cell. If multiple properties are present in a  | 
            ||
| 353 | * single cell, they must be separated using ' - ', and values should not  | 
            ||
| 354 | * contain ':' or ' - '.  | 
            ||
| 355 | *  | 
            ||
| 356 | * Possible formats for the values:  | 
            ||
| 357 | * A  | 
            ||
| 358 | * A, B, "a value, containing a comma"  | 
            ||
| 359 | * A - B  | 
            ||
| 360 | * x: A - y: B  | 
            ||
| 361 | * A - B, C - D, "E - F"  | 
            ||
| 362 | * x: A - y: B, x: C - y: D, "x: E - y: F"  | 
            ||
| 363 | *  | 
            ||
| 364 | * See field_handlers.feature for examples of usage.  | 
            ||
| 365 | *  | 
            ||
| 366 | * @param string $entity_type  | 
            ||
| 367 | * The entity type.  | 
            ||
| 368 | * @param \stdClass $entity  | 
            ||
| 369 | * An object containing the entity properties and fields as properties.  | 
            ||
| 370 | *  | 
            ||
| 371 | * @throws \Exception  | 
            ||
| 372 | * Thrown when a field name is invalid.  | 
            ||
| 373 | */  | 
            ||
| 374 |   public function parseEntityFields($entity_type, \stdClass $entity) { | 
            ||
| 451 | |||
| 452 | /**  | 
            ||
| 453 | * Create a user.  | 
            ||
| 454 | *  | 
            ||
| 455 | * @return object  | 
            ||
| 456 | * The created user.  | 
            ||
| 457 | */  | 
            ||
| 458 |   public function userCreate($user) { | 
            ||
| 466 | |||
| 467 | /**  | 
            ||
| 468 | * Create a term.  | 
            ||
| 469 | *  | 
            ||
| 470 | * @return object  | 
            ||
| 471 | * The created term.  | 
            ||
| 472 | */  | 
            ||
| 473 | View Code Duplication |   public function termCreate($term) { | 
            |
| 481 | |||
| 482 | /**  | 
            ||
| 483 | * Creates a language.  | 
            ||
| 484 | *  | 
            ||
| 485 | * @param \stdClass $language  | 
            ||
| 486 | * An object with the following properties:  | 
            ||
| 487 | * - langcode: the langcode of the language to create.  | 
            ||
| 488 | *  | 
            ||
| 489 | * @return object|FALSE  | 
            ||
| 490 | * The created language, or FALSE if the language was already created.  | 
            ||
| 491 | */  | 
            ||
| 492 |   public function languageCreate(\stdClass $language) { | 
            ||
| 501 | |||
| 502 | /**  | 
            ||
| 503 | * Log-in the given user.  | 
            ||
| 504 | *  | 
            ||
| 505 | * @param \stdClass $user  | 
            ||
| 506 | * The user to log in.  | 
            ||
| 507 | */  | 
            ||
| 508 |   public function login(\stdClass $user) { | 
            ||
| 511 | |||
| 512 | /**  | 
            ||
| 513 | * Logs the current user out.  | 
            ||
| 514 | */  | 
            ||
| 515 |   public function logout() { | 
            ||
| 518 | |||
| 519 | /**  | 
            ||
| 520 | * Determine if the a user is already logged in.  | 
            ||
| 521 | *  | 
            ||
| 522 | * @return boolean  | 
            ||
| 523 | * Returns TRUE if a user is logged in for this session.  | 
            ||
| 524 | */  | 
            ||
| 525 |   public function loggedIn() { | 
            ||
| 528 | |||
| 529 | /**  | 
            ||
| 530 | * User with a given role is already logged in.  | 
            ||
| 531 | *  | 
            ||
| 532 | * @param string $role  | 
            ||
| 533 | * A single role, or multiple comma-separated roles in a single string.  | 
            ||
| 534 | *  | 
            ||
| 535 | * @return boolean  | 
            ||
| 536 | * Returns TRUE if the current logged in user has this role (or roles).  | 
            ||
| 537 | */  | 
            ||
| 538 |   public function loggedInWithRole($role) { | 
            ||
| 541 | |||
| 542 | }  | 
            ||
| 543 | 
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: