Complex classes like Variant 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 Variant, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Variant implements Entity, ReplaceableEntity, JsonSerializable |
||
15 | { |
||
16 | public const WEIGHT_UNIT_IMPERIAL = 'imperial'; |
||
17 | public const WEIGHT_UNIT_METRIC = 'metric'; |
||
18 | public const LENGTH_UNIT_IMPERIAL = 'imperial'; |
||
19 | public const LENGTH_UNIT_METRIC = 'metric'; |
||
20 | |||
21 | private $id; |
||
22 | private $images; |
||
23 | private $annotations; |
||
24 | private $prices; |
||
25 | private $codes; |
||
26 | private $name; |
||
27 | private $description; |
||
28 | private $notes; |
||
29 | private $weight_unit = self::WEIGHT_UNIT_METRIC; |
||
30 | private $length_unit = self::LENGTH_UNIT_METRIC; |
||
31 | private $weight; |
||
32 | private $width; |
||
33 | private $height; |
||
34 | private $depth; |
||
35 | private $creation; |
||
36 | private $modification; |
||
37 | |||
38 | /** |
||
39 | * @var Image |
||
40 | */ |
||
41 | private $pendingImage; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $deleteImage = false; |
||
47 | |||
48 | 4 | public function __construct() |
|
54 | |||
55 | public function __clone() |
||
61 | |||
62 | 3 | public function getId(): ? string |
|
63 | { |
||
64 | 3 | return $this->id; |
|
65 | } |
||
66 | |||
67 | 1 | public function getImages(): ? array |
|
71 | |||
72 | 3 | public function getAnnotations(): ? Annotations |
|
76 | |||
77 | 3 | public function getPrices(): ? Prices |
|
81 | |||
82 | 3 | public function getCodes(): ? Codes |
|
86 | |||
87 | 3 | public function getName(): ? string |
|
91 | |||
92 | 3 | public function getDescription(): ? string |
|
96 | |||
97 | 3 | public function getNotes(): ? string |
|
101 | |||
102 | 3 | public function getWeightUnit(): ? string |
|
106 | |||
107 | 3 | public function getLengthUnit(): ? string |
|
111 | |||
112 | 3 | public function getWeight(): ? float |
|
116 | |||
117 | 3 | public function getWidth(): ? float |
|
121 | |||
122 | 3 | public function getHeight(): ? float |
|
126 | |||
127 | 3 | public function getDepth(): ? float |
|
131 | |||
132 | public function getCreation(): ? DateTime |
||
136 | |||
137 | public function getModification(): ? DateTime |
||
141 | |||
142 | 3 | public function reset(array $data = null) |
|
201 | |||
202 | public function deleteImage() |
||
206 | |||
207 | 1 | public function setImage(Image $image = null) |
|
211 | |||
212 | 3 | public function setName(string $name) |
|
216 | |||
217 | public function setDescription(string $description = null) |
||
221 | |||
222 | public function setNotes(string $notes = null) |
||
226 | |||
227 | public function setWeightUnit(string $weight_unit = null) |
||
231 | |||
232 | public function setLengthUnit(string $length_unit = null) |
||
236 | |||
237 | public function setWeight(float $weight = null) |
||
241 | |||
242 | public function setWidth(float $width = null) |
||
246 | |||
247 | public function setHeight(float $height = null) |
||
251 | |||
252 | public function setDepth(float $depth = null) |
||
256 | |||
257 | 3 | public function jsonSerialize(): array |
|
283 | } |
||
284 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.