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 |
||
| 14 | abstract class DriverEntityBase implements DriverEntityWrapperInterface { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Entity type's machine name. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $type; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Entity bundle's machine name. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $bundle; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * A driver entity plugin manager object. |
||
| 32 | * |
||
| 33 | * @var \Drupal\Driver\Plugin\DriverPluginManagerInterface |
||
| 34 | */ |
||
| 35 | protected $entityPluginManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * A driver field plugin manager object. |
||
| 39 | * |
||
| 40 | * @var \Drupal\Driver\Plugin\DriverPluginManagerInterface |
||
| 41 | */ |
||
| 42 | protected $fieldPluginManager; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The directory to search for additional project-specific driver plugins. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $projectPluginRoot; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The preliminary bundle-agnostic matched driver entity plugin. |
||
| 53 | * |
||
| 54 | * @var \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 55 | */ |
||
| 56 | protected $provisionalPlugin; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The final bundle-specific matched driver entity plugin. |
||
| 60 | * |
||
| 61 | * @var \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 62 | */ |
||
| 63 | protected $finalPlugin; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Constructs a driver entity wrapper object. |
||
| 67 | * |
||
| 68 | * @param string $type |
||
| 69 | * Machine name of the entity type. |
||
| 70 | * @param string $bundle |
||
| 71 | * (optional) Machine name of the entity bundle. |
||
| 72 | * @param \Drupal\Driver\Plugin\DriverPluginManagerInterface $entityPluginManager |
||
| 73 | * (optional) An driver entity plugin manager. |
||
| 74 | * @param \Drupal\Driver\Plugin\DriverPluginManagerInterface $fieldPluginManager |
||
| 75 | * (optional) An driver entity plugin manager. |
||
| 76 | * @param string $projectPluginRoot |
||
| 77 | * The directory to search for additional project-specific driver plugins . |
||
| 78 | */ |
||
| 79 | public function __construct( |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function __call($name, $arguments) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function __get($name) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function bundle() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function delete() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function getEntity() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get an entity plugin. |
||
| 158 | * |
||
| 159 | * This may or may not be bundle-specific, depending on whether the bundle is |
||
| 160 | * known at this point. |
||
| 161 | * |
||
| 162 | * @return \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 163 | * An instantiated driver entity plugin object. |
||
| 164 | */ |
||
| 165 | protected function getPlugin() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | public function getFinalPlugin() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | public function getEntityTypeId() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function id() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function isNew() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function label() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function load($entityId) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * {@inheritdoc} |
||
| 265 | */ |
||
| 266 | public function reload() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | public function save() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function set($identifier, $field) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | public function setBundle($identifier) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | public function setFinalPlugin(DriverEntityPluginInterface $plugin) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function setFields(array $fields) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | */ |
||
| 327 | public function url($rel = 'canonical', array $options = []) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | */ |
||
| 334 | public function tearDown() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Extract the bundle field from a set of fields, and store the bundle. |
||
| 340 | * |
||
| 341 | * @param array $fields |
||
| 342 | * An array of inputs that represent fields. |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | * An array of inputs that represent fields, without the bundle field. |
||
| 346 | */ |
||
| 347 | protected function extractBundleField(array $fields) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get a new driver field with values. |
||
| 389 | * |
||
| 390 | * @param string $fieldName |
||
| 391 | * A string identifying an entity field. |
||
| 392 | * @param string|array $values |
||
| 393 | * An input that can be transformed into Driver field values. |
||
| 394 | */ |
||
| 395 | protected function getNewDriverField($fieldName, $values) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Gets the provisional entity plugin. |
||
| 410 | * |
||
| 411 | * @return \Drupal\Driver\Plugin\DriverEntityPluginInterface |
||
| 412 | * The provisional (bundle-unaware) entity plugin. |
||
| 413 | */ |
||
| 414 | protected function getProvisionalPlugin() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Whether a matched plugin has yet been discovered and stored. |
||
| 423 | * |
||
| 424 | * @return bool |
||
| 425 | * Whether a matched plugin has yet been discovered and stored. |
||
| 426 | */ |
||
| 427 | protected function hasFinalPlugin() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Whether a bundle has been set yet. |
||
| 437 | * |
||
| 438 | * @return bool |
||
| 439 | * Whether a bundle has been set yet. |
||
| 440 | */ |
||
| 441 | protected function isBundleMissing() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Set the driver entity plugin manager. |
||
| 448 | * |
||
| 449 | * @param mixed $manager |
||
| 450 | * The driver entity plugin manager. |
||
| 451 | * @param string $projectPluginRoot |
||
| 452 | * The directory to search for additional project-specific driver plugins. |
||
| 453 | */ |
||
| 454 | protected function setEntityPluginManager($manager, $projectPluginRoot) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Sets the provisional entity plugin. |
||
| 463 | * |
||
| 464 | * @param \Drupal\Driver\Plugin\DriverEntityPluginInterface $plugin |
||
| 465 | * The provisional entity plugin. |
||
| 466 | */ |
||
| 467 | protected function setProvisionalPlugin(DriverEntityPluginInterface $plugin) { |
||
| 470 | |||
| 471 | } |
||
| 472 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: