Complex classes like Metadata 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 Metadata, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Metadata implements MetadataInterface |
||
| 23 | { |
||
| 24 | public const SERVICE_KEY = 'service'; |
||
| 25 | |||
| 26 | public const SUBJECT_KEY = 'subject'; |
||
| 27 | |||
| 28 | public const PLACE_KEY = 'place'; |
||
| 29 | |||
| 30 | /** @var int */ |
||
| 31 | protected $id; |
||
| 32 | |||
| 33 | /** @var Collection|SubjectInterface[] */ |
||
| 34 | protected $subjects; |
||
| 35 | |||
| 36 | /** @var Collection|ServiceInterface[] */ |
||
| 37 | protected $services; |
||
| 38 | |||
| 39 | /** @var Collection|PlaceInterface[] */ |
||
| 40 | protected $places; |
||
| 41 | |||
| 42 | /** @var string|null */ |
||
| 43 | protected $profile; |
||
| 44 | |||
| 45 | /** @var string|null */ |
||
| 46 | protected $guid; |
||
| 47 | |||
| 48 | /** @var string|null */ |
||
| 49 | protected $urgency; |
||
| 50 | |||
| 51 | /** @var string|null */ |
||
| 52 | protected $priority; |
||
| 53 | |||
| 54 | /** @var string|null */ |
||
| 55 | protected $located; |
||
| 56 | |||
| 57 | /** @var string|null */ |
||
| 58 | protected $byline; |
||
| 59 | |||
| 60 | /** @var string|null */ |
||
| 61 | protected $language; |
||
| 62 | |||
| 63 | /** @var string|null */ |
||
| 64 | protected $edNote; |
||
| 65 | |||
| 66 | /** @var string|null */ |
||
| 67 | protected $genre; |
||
| 68 | |||
| 69 | /** @var ArticleInterface */ |
||
| 70 | protected $article; |
||
| 71 | |||
| 72 | public function __construct() |
||
| 78 | |||
| 79 | public function getId(): int |
||
| 83 | |||
| 84 | public function getSubjects(): Collection |
||
| 88 | |||
| 89 | public function addSubject(SubjectInterface $subject): void |
||
| 96 | |||
| 97 | public function removeSubject(SubjectInterface $subject): void |
||
| 104 | |||
| 105 | public function hasSubject(SubjectInterface $subject): bool |
||
| 109 | |||
| 110 | public function getServices(): Collection |
||
| 114 | |||
| 115 | public function addService(ServiceInterface $service): void |
||
| 122 | |||
| 123 | public function removeService(ServiceInterface $service): void |
||
| 130 | |||
| 131 | public function hasService(ServiceInterface $service): bool |
||
| 135 | |||
| 136 | public function getPlaces(): Collection |
||
| 140 | |||
| 141 | public function addPlace(PlaceInterface $place): void |
||
| 148 | |||
| 149 | public function removePlace(PlaceInterface $place): void |
||
| 156 | |||
| 157 | public function hasPlace(PlaceInterface $place): bool |
||
| 161 | |||
| 162 | public function getProfile(): ?string |
||
| 166 | |||
| 167 | public function setProfile(?string $profile): void |
||
| 171 | |||
| 172 | public function getGuid(): ?string |
||
| 176 | |||
| 177 | public function setGuid(?string $guid): void |
||
| 181 | |||
| 182 | public function getUrgency(): ?int |
||
| 186 | |||
| 187 | public function setUrgency(?int $urgency): void |
||
| 191 | |||
| 192 | public function getPriority(): ?int |
||
| 196 | |||
| 197 | public function setPriority(?int $priority): void |
||
| 201 | |||
| 202 | public function getLocated(): ?string |
||
| 206 | |||
| 207 | public function setLocated(?string $located): void |
||
| 211 | |||
| 212 | public function getByline(): ?string |
||
| 216 | |||
| 217 | public function setByline(?string $byline): void |
||
| 221 | |||
| 222 | public function getLanguage(): ?string |
||
| 226 | |||
| 227 | public function setLanguage(?string $language): void |
||
| 231 | |||
| 232 | public function getEdNote(): ?string |
||
| 236 | |||
| 237 | public function setEdNote(?string $edNote): void |
||
| 241 | |||
| 242 | public function getGenre(): ?string |
||
| 246 | |||
| 247 | public function setGenre(?string $genre): void |
||
| 251 | |||
| 252 | public function getArticle(): ?ArticleInterface |
||
| 256 | |||
| 257 | public function setArticle(?ArticleInterface $article): void |
||
| 261 | } |
||
| 262 |