Complex classes like Item 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 Item, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Item implements EntityDocument, FingerprintHolder, StatementListHolder, |
||
| 30 | LabelsProvider, DescriptionsProvider, AliasesProvider { |
||
| 31 | |||
| 32 | const ENTITY_TYPE = 'item'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ItemId|null |
||
| 36 | */ |
||
| 37 | private $id; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Fingerprint |
||
| 41 | */ |
||
| 42 | private $fingerprint; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var SiteLinkList |
||
| 46 | */ |
||
| 47 | private $siteLinks; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var StatementList |
||
| 51 | */ |
||
| 52 | private $statements; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @since 1.0 |
||
| 56 | 81 | * |
|
| 57 | * @param ItemId|null $id |
||
| 58 | * @param Fingerprint|null $fingerprint |
||
| 59 | * @param SiteLinkList|null $siteLinks |
||
| 60 | * @param StatementList|null $statements |
||
| 61 | */ |
||
| 62 | 81 | public function __construct( |
|
| 73 | |||
| 74 | /** |
||
| 75 | 12 | * Returns the id of the entity or null if it does not have one. |
|
| 76 | 12 | * |
|
| 77 | * @since 0.1 return type changed in 0.3 |
||
| 78 | * |
||
| 79 | * @return ItemId|null |
||
| 80 | */ |
||
| 81 | public function getId() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Can be integer since 0.1. |
||
| 87 | * Can be ItemId since 0.5. |
||
| 88 | 2 | * Can be null since 1.0. |
|
| 89 | 2 | * |
|
| 90 | 1 | * @param ItemId|int|null $id |
|
| 91 | 2 | * |
|
| 92 | 1 | * @throws InvalidArgumentException |
|
| 93 | 1 | */ |
|
| 94 | public function setId( $id ) { |
||
| 104 | 58 | ||
| 105 | 58 | /** |
|
| 106 | * @since 0.7.3 |
||
| 107 | * |
||
| 108 | * @return Fingerprint |
||
| 109 | */ |
||
| 110 | public function getFingerprint() { |
||
| 113 | 3 | ||
| 114 | 3 | /** |
|
| 115 | 3 | * @since 0.7.3 |
|
| 116 | * |
||
| 117 | * @param Fingerprint $fingerprint |
||
| 118 | */ |
||
| 119 | public function setFingerprint( Fingerprint $fingerprint ) { |
||
| 122 | |||
| 123 | 12 | /** |
|
| 124 | 12 | * @see LabelsProvider::getLabels |
|
| 125 | 12 | * |
|
| 126 | * @return TermList |
||
| 127 | */ |
||
| 128 | public function getLabels() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | 11 | * @see DescriptionsProvider::getDescriptions |
|
| 134 | 11 | * |
|
| 135 | 11 | * @return TermList |
|
| 136 | */ |
||
| 137 | public function getDescriptions() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @see AliasesProvider::getAliasGroups |
||
| 143 | 31 | * |
|
| 144 | 31 | * @return AliasGroupList |
|
| 145 | 31 | */ |
|
| 146 | public function getAliasGroups() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $languageCode |
||
| 152 | 17 | * @param string $value |
|
| 153 | 17 | * |
|
| 154 | * @throws InvalidArgumentException |
||
| 155 | */ |
||
| 156 | public function setLabel( $languageCode, $value ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $languageCode |
||
| 162 | * @param string $value |
||
| 163 | * |
||
| 164 | * @throws InvalidArgumentException |
||
| 165 | */ |
||
| 166 | public function setDescription( $languageCode, $value ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $languageCode |
||
| 172 | * @param string[] $aliases |
||
| 173 | * |
||
| 174 | * @throws InvalidArgumentException |
||
| 175 | 1 | */ |
|
| 176 | 1 | public function setAliases( $languageCode, array $aliases ) { |
|
| 179 | |||
| 180 | 1 | /** |
|
| 181 | 1 | * @since 0.8 |
|
| 182 | * |
||
| 183 | * @return SiteLinkList |
||
| 184 | */ |
||
| 185 | public function getSiteLinkList() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @since 0.8 |
||
| 191 | * |
||
| 192 | * @param SiteLinkList $siteLinks |
||
| 193 | */ |
||
| 194 | public function setSiteLinkList( SiteLinkList $siteLinks ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds a site link to the list of site links. |
||
| 200 | * If there already is a site link with the site id of the provided site link, |
||
| 201 | 4 | * then that one will be overridden by the provided one. |
|
| 202 | 4 | * |
|
| 203 | * @deprecated since 0.8, use getSiteLinkList()->addSiteLink() instead. |
||
| 204 | * @since 0.6 |
||
| 205 | * |
||
| 206 | * @param SiteLink $siteLink |
||
| 207 | */ |
||
| 208 | public function addSiteLink( SiteLink $siteLink ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Removes the sitelink with specified site ID if the Item has such a sitelink. |
||
| 218 | * |
||
| 219 | * @deprecated since 0.8, use getSiteLinkList()->removeLinkWithSiteId() instead. |
||
| 220 | * @since 0.1 |
||
| 221 | * |
||
| 222 | * @param string $siteId the target site's id |
||
| 223 | */ |
||
| 224 | public function removeSiteLink( $siteId ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @deprecated since 0.8, use getSiteLinkList() instead, |
||
| 230 | * @since 0.6 |
||
| 231 | * |
||
| 232 | * @return SiteLink[] |
||
| 233 | */ |
||
| 234 | public function getSiteLinks() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @deprecated since 0.8, use getSiteLinkList()->getBySiteId() instead. |
||
| 240 | * @since 0.6 |
||
| 241 | * |
||
| 242 | * @param string $siteId |
||
| 243 | * |
||
| 244 | * @return SiteLink |
||
| 245 | * @throws OutOfBoundsException |
||
| 246 | */ |
||
| 247 | public function getSiteLink( $siteId ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @deprecated since 0.8, use getSiteLinkList()->hasLinkWithSiteId() instead. |
||
| 253 | * @since 0.4 |
||
| 254 | * |
||
| 255 | * @param string $siteId |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function hasLinkToSite( $siteId ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @deprecated since 0.8, use getSiteLinkList()->isEmpty() instead. |
||
| 265 | * @since 0.5 |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | 3 | */ |
|
| 269 | 3 | public function hasSiteLinks() { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @deprecated since 2.5, use new Item() instead. |
||
| 275 | * |
||
| 276 | * @return self |
||
| 277 | */ |
||
| 278 | public static function newEmpty() { |
||
| 281 | 2 | ||
| 282 | 2 | /** |
|
| 283 | 2 | * @see Entity::getType |
|
| 284 | 2 | * |
|
| 285 | * @since 0.1 |
||
| 286 | * |
||
| 287 | * @return string Returns the entity type "item". |
||
| 288 | */ |
||
| 289 | public function getType() { |
||
| 292 | 7 | ||
| 293 | /** |
||
| 294 | * Returns if the Item has no content. |
||
| 295 | * Having an id set does not count as having content. |
||
| 296 | * |
||
| 297 | * @since 0.1 |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | 1 | */ |
|
| 301 | 1 | public function isEmpty() { |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Removes all content from the Item. |
||
| 309 | 4 | * The id is not part of the content. |
|
| 310 | 4 | * |
|
| 311 | * @since 0.1 |
||
| 312 | */ |
||
| 313 | public function clear() { |
||
| 318 | 1 | ||
| 319 | 1 | /** |
|
| 320 | 1 | * @since 1.0 |
|
| 321 | * |
||
| 322 | * @return StatementList |
||
| 323 | */ |
||
| 324 | public function getStatements() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @since 1.0 |
||
| 330 | * |
||
| 331 | * @param StatementList $statements |
||
| 332 | */ |
||
| 333 | public function setStatements( StatementList $statements ) { |
||
| 336 | 18 | ||
| 337 | 18 | /** |
|
| 338 | * @see EntityDocument::equals |
||
| 339 | * |
||
| 340 | * @since 0.1 |
||
| 341 | * |
||
| 342 | 18 | * @param mixed $target |
|
| 343 | 18 | * |
|
| 344 | 18 | * @return bool |
|
| 345 | */ |
||
| 346 | public function equals( $target ) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @see EntityDocument::copy |
||
| 359 | * |
||
| 360 | * @since 0.1 |
||
| 361 | * |
||
| 362 | * @return self |
||
| 363 | */ |
||
| 364 | public function copy() { |
||
| 367 | |||
| 368 | } |
||
| 369 |