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 EntityMetadata 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 EntityMetadata, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class EntityMetadata implements Interfaces\AttributeInterface, Interfaces\EmbedInterface, Interfaces\MergeableInterface, Interfaces\MixinInterface, Interfaces\RelationshipInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Uses attributes. |
||
| 18 | */ |
||
| 19 | use Traits\AttributesTrait; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Uses embeds. |
||
| 23 | */ |
||
| 24 | use Traits\EmbedsTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Uses mixins. |
||
| 28 | */ |
||
| 29 | use Traits\MixinsTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Uses merged properties. |
||
| 33 | */ |
||
| 34 | use Traits\PropertiesTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Uses relationships. |
||
| 38 | */ |
||
| 39 | use Traits\RelationshipsTrait; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The id key name and type. |
||
| 43 | */ |
||
| 44 | const ID_KEY = 'id'; |
||
| 45 | const ID_TYPE = 'string'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The model type key. |
||
| 49 | */ |
||
| 50 | const TYPE_KEY = 'type'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Whether this class is abstract. |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | public $abstract = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * An array of attribute default values for this model. |
||
| 61 | * Keyed by field name. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | public $defaultValues = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The entity type this entity extends. |
||
| 69 | * |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | public $extends; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Child entity types this entity owns. |
||
| 76 | * Only used for polymorphic entities. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | public $ownedTypes = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The persistence metadata for this entity. |
||
| 84 | * |
||
| 85 | * @var StorageLayerInterface |
||
| 86 | */ |
||
| 87 | public $persistence; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Whether this class is considered polymorphic. |
||
| 91 | * |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | public $polymorphic = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The search metadata for this entity. |
||
| 98 | * |
||
| 99 | * @var StorageLayerInterface |
||
| 100 | */ |
||
| 101 | public $search; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Uniquely defines the type of entity. |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | public $type; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Constructor. |
||
| 112 | * |
||
| 113 | * @param string $type The resource identifier type. |
||
| 114 | */ |
||
| 115 | public function __construct($type) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Gets the parent entity type. |
||
| 122 | * For entities that are extended. |
||
| 123 | * |
||
| 124 | * @return string|null |
||
| 125 | */ |
||
| 126 | public function getParentEntityType() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function getProperties() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Whether this metadata represents an abstract class. |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function isAbstract() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Whether this metadata represents a polymorphic class. |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function isPolymorphic() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Deteremines whether search is enabled for this model. |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function isSearchEnabled() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Determines if this is a child entity of another entity. |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function isChildEntity() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritDoc} |
||
| 181 | */ |
||
| 182 | public function merge(Interfaces\MergeableInterface $metadata) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Sets this metadata as representing an abstract class. |
||
| 208 | * |
||
| 209 | * @param bool $bit |
||
| 210 | * @return self |
||
| 211 | */ |
||
| 212 | public function setAbstract($bit = true) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the persistence metadata for this entity. |
||
| 220 | * |
||
| 221 | * @param Interfaces\StorageLayerInterface $persistence |
||
| 222 | * @return self |
||
| 223 | */ |
||
| 224 | public function setPersistence(Interfaces\StorageLayerInterface $persistence) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Sets this metadata as representing a polymorphic class. |
||
| 232 | * |
||
| 233 | * @param bool $bit |
||
| 234 | * @return self |
||
| 235 | */ |
||
| 236 | public function setPolymorphic($bit = true) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Sets the search metadata for this entity. |
||
| 244 | * |
||
| 245 | * @param Interfaces\StorageLayerInterface $search |
||
| 246 | * @return self |
||
| 247 | */ |
||
| 248 | public function setSearch(Interfaces\StorageLayerInterface $search) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Sets the entity type. |
||
| 256 | * |
||
| 257 | * @param string $type |
||
| 258 | * @return self |
||
| 259 | * @throws MetadataException If the type is not a string or is empty. |
||
| 260 | */ |
||
| 261 | public function setType($type) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * {@inheritdoc} |
||
| 272 | */ |
||
| 273 | View Code Duplication | protected function applyMixinProperties(MixinMetadata $mixin) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * {@inheritdoc} |
||
| 297 | */ |
||
| 298 | View Code Duplication | protected function validateAttribute(AttributeMetadata $attribute) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | View Code Duplication | protected function validateEmbed(EmbeddedPropMetadata $embed) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritdoc} |
||
| 323 | */ |
||
| 324 | View Code Duplication | protected function validateRelationship(RelationshipMetadata $relationship) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Merges attributes with this instance's attributes. |
||
| 336 | * |
||
| 337 | * @param array $toAdd |
||
| 338 | * @return self |
||
| 339 | */ |
||
| 340 | private function mergeAttributes(array $toAdd) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Merges embeds with this instance's embeds. |
||
| 350 | * |
||
| 351 | * @param array $toAdd |
||
| 352 | * @return self |
||
| 353 | */ |
||
| 354 | private function mergeEmbeds(array $toAdd) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Merges mixins with this instance's mixins. |
||
| 364 | * |
||
| 365 | * @param array $toAdd |
||
| 366 | * @return self |
||
| 367 | */ |
||
| 368 | private function mergeMixins(array $toAdd) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Merges relationships with this instance's relationships. |
||
| 380 | * |
||
| 381 | * @param array $toAdd |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | private function mergeRelationships(array $toAdd) |
||
| 391 | } |
||
| 392 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..