Complex classes like Pilot 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 Pilot, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class Pilot |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var PilotId |
||
24 | */ |
||
25 | private $pilotId; |
||
26 | /** |
||
27 | * @var PilotLicenceNumber |
||
28 | */ |
||
29 | private $pilotLicenceNumber; |
||
30 | /** |
||
31 | * @var PilotTrainingLicenceNumber |
||
32 | */ |
||
33 | private $trainingPilotLicenceNumber; |
||
34 | /** |
||
35 | * @var LastTrainingDate |
||
36 | */ |
||
37 | private $lastTrainingFlightDate; |
||
38 | /** |
||
39 | * @var IsOwner |
||
40 | */ |
||
41 | private $isPilotClassA; |
||
42 | /** |
||
43 | * @var IsOwner |
||
44 | */ |
||
45 | private $isPilotClassB; |
||
46 | /** |
||
47 | * @var IsOwner |
||
48 | */ |
||
49 | private $isPilotClassC; |
||
50 | /** |
||
51 | * @var IsOwner |
||
52 | */ |
||
53 | private $isPilotClassD; |
||
54 | /** |
||
55 | * @var IsOwner |
||
56 | */ |
||
57 | private $isPilotCaz; |
||
58 | /** |
||
59 | * @var IsOwner |
||
60 | */ |
||
61 | private $isMedicalOwner; |
||
62 | /** |
||
63 | * @var EndDate |
||
64 | */ |
||
65 | private $endMedicalDate; |
||
66 | |||
67 | /** |
||
68 | * @var StartDate |
||
69 | */ |
||
70 | private $startMedicalDate; |
||
71 | /** |
||
72 | * @var IsOwner |
||
73 | */ |
||
74 | private $hasQualifStatic; |
||
75 | /** |
||
76 | * @var IsOwner |
||
77 | */ |
||
78 | private $hasQualifNight; |
||
79 | /** |
||
80 | * @var IsOwner |
||
81 | */ |
||
82 | private $hasQualifPro; |
||
83 | /** |
||
84 | * @var LastTrainingDate |
||
85 | */ |
||
86 | private $lastOpcDate; |
||
87 | /** |
||
88 | * @var LastTrainingDate |
||
89 | */ |
||
90 | private $lastProRefreshDate; |
||
91 | /** |
||
92 | * @var IsOwner |
||
93 | */ |
||
94 | private $hasQualifInstructor; |
||
95 | /** |
||
96 | * @var LastTrainingDate |
||
97 | */ |
||
98 | private $lastInstructorRefreshDate; |
||
99 | /** |
||
100 | * @var IsOwner |
||
101 | */ |
||
102 | private $hasQualifExaminator; |
||
103 | /** |
||
104 | * @var LastTrainingDate |
||
105 | */ |
||
106 | private $lastExaminatorRefreshDate; |
||
107 | /** |
||
108 | * @var IsOwner |
||
109 | */ |
||
110 | private $hasRadio; |
||
111 | /** |
||
112 | * @var RadioLicenceNumber |
||
113 | */ |
||
114 | private $radioLicenceNumber; |
||
115 | /** |
||
116 | * @var RadioLicenceDate |
||
117 | */ |
||
118 | private $radioLicenceDate; |
||
119 | /** |
||
120 | * @var IsOwner |
||
121 | */ |
||
122 | private $hasTrainingFirstHelp; |
||
123 | /** |
||
124 | * @var LastTrainingDate |
||
125 | */ |
||
126 | private $lastTrainingFirstHelpDate; |
||
127 | /** |
||
128 | * @var FirstHelpCertificationNumber |
||
129 | */ |
||
130 | private $certificationNumberTrainingFirstHelp; |
||
131 | /** |
||
132 | * @var IsOwner |
||
133 | */ |
||
134 | private $hasTrainingFire; |
||
135 | /** |
||
136 | * @var LastTrainingDate |
||
137 | */ |
||
138 | private $lastTrainingFireDate; |
||
139 | /** |
||
140 | * @var FireCertificationNumber |
||
141 | */ |
||
142 | private $certificationNumberTrainingFire; |
||
143 | |||
144 | private function __construct( |
||
207 | |||
208 | public static function create(PilotId $id) |
||
243 | |||
244 | public static function fromState(array $state): self |
||
279 | |||
280 | public function state(): array |
||
316 | |||
317 | public function id(): PilotId |
||
321 | |||
322 | public function medical() |
||
326 | |||
327 | public function removeMedical() |
||
331 | |||
332 | public function hasClassA() |
||
336 | |||
337 | public function removeClassA() |
||
341 | |||
342 | public function hasClassB() |
||
346 | |||
347 | public function removeClassB() |
||
351 | |||
352 | public function hasClassC() |
||
356 | |||
357 | public function removeClassC() |
||
361 | |||
362 | public function hasClassD() |
||
366 | |||
367 | public function removeClassD() |
||
371 | |||
372 | public function gazPilot() |
||
376 | |||
377 | public function removeGazPilot() |
||
381 | |||
382 | public function staticQualif() |
||
386 | |||
387 | public function removeStaticQualif() |
||
391 | |||
392 | public function nightQualif() |
||
396 | |||
397 | public function removeNightQualif() |
||
401 | |||
402 | public function proQualif() |
||
406 | |||
407 | public function removeProQualif() |
||
411 | |||
412 | public function instructorQualif() |
||
416 | |||
417 | public function removeInstructorQualif() |
||
421 | |||
422 | public function examinatorQualif() |
||
426 | |||
427 | public function removeExaminatorQualif() |
||
431 | |||
432 | public function radio() |
||
436 | |||
437 | public function removeRadio() |
||
441 | |||
442 | public function trainingFirstHelp() |
||
446 | |||
447 | public function removeTrainingFirstHelp() |
||
451 | |||
452 | public function trainingFire() |
||
456 | |||
457 | public function removeTrainingFire() |
||
461 | |||
462 | public function attributePilotLicenceNumber(PilotLicenceNumber $value) |
||
466 | |||
467 | public function attributeTrainingPilotLicenceNumber(PilotTrainingLicenceNumber $value) |
||
471 | |||
472 | public function attributeRadioLicenceNumber(RadioLicenceNumber $value) |
||
476 | |||
477 | public function attributeCertificationNumberTrainingFirstHelp(FirstHelpCertificationNumber $value) |
||
481 | |||
482 | public function attributeCertificationNumberTrainingFire(FireCertificationNumber $value) |
||
486 | |||
487 | public function attributeLastTrainingFlightDate(LastTrainingDate $value) |
||
491 | |||
492 | public function attributeEndMedicalDate(EndDate $value) |
||
496 | |||
497 | public function attributeStartMedicalDate(StartDate $value) |
||
501 | |||
502 | public function attributeLastOpcDate(LastTrainingDate $value) |
||
506 | |||
507 | public function attributeLastProRefreshDate(LastTrainingDate $value) |
||
511 | |||
512 | public function attributeLastInstructorRefreshDate(LastTrainingDate $value) |
||
516 | |||
517 | public function attributeLastExaminatorRefreshDate(LastTrainingDate $value) |
||
521 | |||
522 | public function attributeLastTrainingFireDate(LastTrainingDate $value) |
||
526 | |||
527 | public function attributeRadioLicenceDate(RadioLicenceDate $value) |
||
531 | |||
532 | public function attributeLastTrainingFirstHelpDate(LastTrainingDate $value) |
||
536 | } |
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..