Complex classes like Ticket 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 Ticket, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Ticket |
||
14 | { |
||
15 | /** @var string */ |
||
16 | private const DONE_STATUS = 'Done'; |
||
17 | |||
18 | /** @var ?int */ |
||
19 | private $id; |
||
20 | |||
21 | /** @var ?string */ |
||
22 | private $key; |
||
23 | |||
24 | /** @var ?string */ |
||
25 | private $assigneeName; |
||
26 | |||
27 | /** @var ?string */ |
||
28 | private $assigneeDisplayName; |
||
29 | |||
30 | /** @var ?string */ |
||
31 | private $assigneeEmail; |
||
32 | |||
33 | /** @var ?bool */ |
||
34 | private $isAssigneeActive; |
||
35 | |||
36 | /** @var ?string */ |
||
37 | private $ticketStatus; |
||
38 | |||
39 | /** @var bool */ |
||
40 | private $isDone; |
||
41 | |||
42 | /** @var ?string */ |
||
43 | private $ticketStatusCategory; |
||
44 | |||
45 | /** @var ?string */ |
||
46 | private $components; |
||
47 | |||
48 | /** @var ?string */ |
||
49 | private $type; |
||
50 | |||
51 | /** @var ?string */ |
||
52 | private $project; |
||
53 | |||
54 | /** @var ?string */ |
||
55 | private $fixVersion; |
||
56 | |||
57 | /** @var ?string */ |
||
58 | private $summary; |
||
59 | |||
60 | /** @var ?string */ |
||
61 | private $branch; |
||
62 | |||
63 | /** @var bool */ |
||
64 | private $hasBranch; |
||
65 | |||
66 | /** @var ?string */ |
||
67 | private $lastUpdate; |
||
68 | |||
69 | /** @var ?string */ |
||
70 | private $url; |
||
71 | |||
72 | /** @var ?string */ |
||
73 | private $pullRequestStatus; |
||
74 | |||
75 | /** @var ?string */ |
||
76 | private $pullRequestName; |
||
77 | |||
78 | /** @var ?string */ |
||
79 | private $repository; |
||
80 | |||
81 | /** @var ?bool */ |
||
82 | private $hasDirectory; |
||
83 | |||
84 | /** @var ?string */ |
||
85 | private $directory; |
||
86 | |||
87 | 10 | public function __construct( |
|
118 | |||
119 | /** |
||
120 | * Compares passed status to tickets status. |
||
121 | * Returns true if same, false if not. |
||
122 | */ |
||
123 | 2 | public function compareStatus(string $status) : bool |
|
128 | |||
129 | 2 | public function id() : int |
|
133 | |||
134 | 2 | public function key() : string |
|
138 | |||
139 | 2 | public function assigneeName() : ?string |
|
143 | |||
144 | 2 | public function assigneeDisplayName() : ?string |
|
148 | |||
149 | 2 | public function assigneeEmail() : ?string |
|
153 | |||
154 | 2 | public function isAssigneeActive() : ?bool |
|
158 | |||
159 | 2 | public function ticketStatus() : string |
|
163 | |||
164 | 2 | public function ticketStatusCategory() : ?string |
|
168 | |||
169 | 2 | public function components() : ?string |
|
173 | |||
174 | 2 | public function type() : ?string |
|
178 | |||
179 | 2 | public function project() : string |
|
183 | |||
184 | 2 | public function fixVersion() : ?string |
|
188 | |||
189 | 2 | public function summary() : ?string |
|
193 | |||
194 | 2 | public function branch() : ?string |
|
198 | |||
199 | 2 | public function lastUpdate() : string |
|
203 | |||
204 | 2 | public function url() : ?string |
|
208 | |||
209 | 2 | public function pullRquestStatus() : ?string |
|
213 | |||
214 | 2 | public function repository() : ?string |
|
218 | |||
219 | 2 | public function hasDirectory() : bool |
|
223 | |||
224 | 2 | public function directory() : ?string |
|
228 | |||
229 | 2 | public function pullRequestName() : ?string |
|
233 | |||
234 | 2 | public function isDone() : bool |
|
238 | |||
239 | 3 | public function hasBranch() : bool |
|
243 | |||
244 | 6 | public function setRepository(?string $repository) : void |
|
248 | |||
249 | 7 | public function setIsDone(string $status) : void |
|
256 | |||
257 | 6 | public function setHasBranch(?string $branch) : void |
|
264 | |||
265 | /** |
||
266 | * @throws NullArgumentException |
||
267 | */ |
||
268 | 10 | private function setId(?int $id) : void |
|
275 | |||
276 | /** |
||
277 | * @throws NullArgumentException |
||
278 | */ |
||
279 | 9 | private function setKey(?string $key) : void |
|
286 | |||
287 | 8 | private function setAssigneeName(?string $assigneeName) : void |
|
291 | |||
292 | 8 | private function setAssigneeDisplayName(?string $assigneeDisplayName) : void |
|
296 | |||
297 | 8 | private function setAssigneeEmail(?string $assigneeEmail) : void |
|
301 | |||
302 | 8 | private function setIsAssigneeActive(?bool $isAssigneeActive) : void |
|
306 | |||
307 | /** |
||
308 | * @throws NullArgumentException |
||
309 | */ |
||
310 | 8 | private function setTicketStatus(?string $ticketStatus) : void |
|
317 | |||
318 | 7 | private function setTicketStatusCategory(?string $ticketStatusCategory) : void |
|
322 | |||
323 | 7 | private function setComponents(?string $components) : void |
|
327 | |||
328 | 7 | private function setType(?string $type) : void |
|
332 | |||
333 | /** |
||
334 | * @throws NullArgumentException |
||
335 | */ |
||
336 | 7 | private function setProject(?string $project) : void |
|
343 | |||
344 | 6 | private function setFixVersion(?string $fixVersion) : void |
|
348 | |||
349 | 6 | private function setSummary(?string $summary) : void |
|
353 | |||
354 | 6 | private function setBranch(?string $branch) : void |
|
358 | |||
359 | 6 | private function setLastUpdate(?string $lastUpdate) : void |
|
363 | |||
364 | 6 | private function setUrl(?string $url) : void |
|
368 | |||
369 | 6 | private function setPullRequestStatus(?string $pullRequestStatus) : void |
|
373 | |||
374 | 6 | private function setHasDirectory(?bool $hasDirectory) : void |
|
381 | |||
382 | 5 | private function setDirectory(?string $directory) : void |
|
386 | |||
387 | 6 | private function setPullRequestName(?string $pullRequestName) : void |
|
391 | } |
||
392 |