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 DriverEntityBase 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 DriverEntityBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class DriverEntityBase implements DriverEntityWrapperInterface |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Entity type's machine name. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $type; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Entity bundle's machine name. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $bundle; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * A driver entity plugin manager object. |
||
| 36 | * |
||
| 37 | * @var \Drupal\Driver\Plugin\DriverPluginManagerInterface |
||
| 38 | */ |
||
| 39 | protected $entityPluginManager; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A driver field plugin manager object. |
||
| 43 | * |
||
| 44 | * @var \Drupal\Driver\Plugin\DriverPluginManagerInterface |
||
| 45 | */ |
||
| 46 | protected $fieldPluginManager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The directory to search for additional project-specific driver plugins. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $projectPluginRoot; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The preliminary bundle-agnostic matched driver entity plugin. |
||
| 57 | * |
||
| 58 | * @var \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 59 | */ |
||
| 60 | protected $provisionalPlugin; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The final bundle-specific matched driver entity plugin. |
||
| 64 | * |
||
| 65 | * @var \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 66 | */ |
||
| 67 | protected $finalPlugin; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructs a driver entity wrapper object. |
||
| 71 | * |
||
| 72 | * @param string $type |
||
| 73 | * Machine name of the entity type. |
||
| 74 | * @param string $bundle |
||
| 75 | * (optional) Machine name of the entity bundle. |
||
| 76 | * @param \Drupal\Driver\Plugin\DriverPluginManagerInterface $entityPluginManager |
||
| 77 | * (optional) An driver entity plugin manager. |
||
| 78 | * @param \Drupal\Driver\Plugin\DriverPluginManagerInterface $entityPluginManager |
||
| 79 | * (optional) An driver entity plugin manager. |
||
| 80 | * @param string $projectPluginRoot |
||
| 81 | * The directory to search for additional project-specific driver plugins . |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function __call($name, $arguments) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function __get($name) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function bundle() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function delete() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function getEntity() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get an entity plugin. |
||
| 167 | * |
||
| 168 | * This may or may not be bundle-specific, depending on whether the bundle is |
||
| 169 | * known at this point. |
||
| 170 | * |
||
| 171 | * @return \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 172 | * An instantiated driver entity plugin object. |
||
| 173 | */ |
||
| 174 | protected function getPlugin() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | public function getFinalPlugin() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function getEntityTypeId() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function id() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function isNew() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function label() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function load($entityId) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritdoc} |
||
| 280 | */ |
||
| 281 | public function reload() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | public function save() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function set($identifier, $field) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * {@inheritdoc} |
||
| 307 | */ |
||
| 308 | public function setBundle($identifier) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | */ |
||
| 320 | public function setFinalPlugin($plugin) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function setFields($fields) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function url($rel = 'canonical', $options = []) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | public function tearDown() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Extract the bundle field from a set of fields, and store the bundle. |
||
| 363 | * |
||
| 364 | * @param array $fields |
||
| 365 | * An array of inputs that represent fields. |
||
| 366 | * |
||
| 367 | * @return array |
||
| 368 | * An array of inputs that represent fields, without the bundle field. |
||
| 369 | */ |
||
| 370 | protected function extractBundleField($fields) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the driver entity plugin manager. |
||
| 411 | * |
||
| 412 | * @return \Drupal\Driver\Plugin\DriverPluginManagerInterface |
||
| 413 | */ |
||
| 414 | protected function getFinalPluginManager() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get a new driver field with values. |
||
| 421 | * |
||
| 422 | * @param string $fieldName |
||
| 423 | * A string identifying an entity field. |
||
| 424 | * @param string|array $values |
||
| 425 | * An input that can be transformed into Driver field values. |
||
| 426 | */ |
||
| 427 | protected function getNewDriverField($fieldName, $values) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Gets the provisional entity plugin. |
||
| 443 | * |
||
| 444 | * @return \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 445 | */ |
||
| 446 | protected function getProvisionalPlugin() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Whether a matched plugin has yet been discovered and stored. |
||
| 456 | * |
||
| 457 | * @return boolean |
||
| 458 | */ |
||
| 459 | protected function hasFinalPlugin() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Whether a bundle has been set yet. |
||
| 470 | * |
||
| 471 | * @return boolean |
||
| 472 | */ |
||
| 473 | protected function isBundleMissing() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set the driver entity plugin manager. |
||
| 481 | * |
||
| 482 | * @param \Drupal\Driver\Plugin\DriverPluginManagerInterface $manager |
||
| 483 | * The driver entity plugin manager. |
||
| 484 | * @param string $projectPluginRoot |
||
| 485 | * The directory to search for additional project-specific driver plugins. |
||
| 486 | */ |
||
| 487 | View Code Duplication | protected function setEntityPluginManager($manager, $projectPluginRoot) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Sets the provisional entity plugin. |
||
| 497 | * |
||
| 498 | * @param \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 499 | */ |
||
| 500 | protected function setProvisionalPlugin($plugin) |
||
| 504 | } |
||
| 505 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):