Complex classes like EntityGubbins 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 EntityGubbins, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class EntityGubbins |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $name; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $className; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var EntityField[] |
||
| 26 | */ |
||
| 27 | private $keyFields; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var EntityField[] |
||
| 31 | */ |
||
| 32 | private $fields; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var AssociationStubBase[] |
||
| 36 | */ |
||
| 37 | private $stubs; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Association[] |
||
| 41 | */ |
||
| 42 | private $associations; |
||
| 43 | /** |
||
| 44 | * @var ResourceEntityType |
||
| 45 | */ |
||
| 46 | private $odataResourceType; |
||
| 47 | /** |
||
| 48 | * @var bool|null |
||
| 49 | */ |
||
| 50 | private $isPolymorphicAffected = null; |
||
| 51 | |||
| 52 | //TODO: move the checking part of this to the set stubs method. |
||
|
|
|||
| 53 | public function isPolymorphicAffected() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return ResourceEntityType |
||
| 73 | */ |
||
| 74 | public function getOdataResourceType() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param ResourceEntityType $odataType |
||
| 81 | */ |
||
| 82 | public function setOdataResourceType(ResourceEntityType $odataType) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function getName() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $name |
||
| 101 | */ |
||
| 102 | public function setName($name) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getClassName() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $className |
||
| 117 | */ |
||
| 118 | public function setClassName($className) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return EntityField[] |
||
| 125 | */ |
||
| 126 | public function getKeyFields() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return EntityField[] |
||
| 133 | */ |
||
| 134 | public function getFields() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param EntityField[] $fields |
||
| 141 | */ |
||
| 142 | public function setFields(array $fields) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return AssociationStubBase[] |
||
| 168 | */ |
||
| 169 | public function getStubs() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param AssociationStubBase[] $stubs |
||
| 176 | */ |
||
| 177 | public function setStubs(array $stubs) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param Association $association |
||
| 190 | * @param bool $isFirst |
||
| 191 | */ |
||
| 192 | public function addAssociation(Association $association, $isFirst = true) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return String[] |
||
| 207 | */ |
||
| 208 | protected function getFieldNames() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return String[] |
||
| 219 | */ |
||
| 220 | protected function getAssociationNames() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return Associations\Association[] |
||
| 234 | */ |
||
| 235 | public function getAssociations() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param $relName |
||
| 242 | * @return Association|null |
||
| 243 | */ |
||
| 244 | public function resolveAssociation($relName) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function isOK() |
||
| 260 | } |
||
| 261 |