Total Complexity | 40 |
Total Lines | 241 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Price 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.
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 Price, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Price implements CrudEntityInterface, DeletableInterface, PriceInterface |
||
32 | { |
||
33 | protected $id; |
||
34 | |||
35 | protected ?int $amount; |
||
36 | |||
37 | protected string $currency; |
||
38 | |||
39 | protected bool $recurring; |
||
40 | |||
41 | protected ?string $schedule = null; |
||
42 | |||
43 | protected ?string $externalReference = null; |
||
44 | |||
45 | protected bool $includingTax = true; |
||
46 | |||
47 | protected Product $product; |
||
48 | |||
49 | protected ?bool $public = true; |
||
50 | |||
51 | protected ?bool $isDeleted = false; |
||
52 | |||
53 | protected \DateTimeInterface $createdAt; |
||
54 | |||
55 | protected ?\DateTimeInterface $deletedAt = null; |
||
56 | |||
57 | protected ?string $paymentProviderDetailsUrl = null; |
||
58 | |||
59 | protected array|Collection $tierComponents = []; |
||
60 | |||
61 | protected PriceType $type; |
||
62 | |||
63 | private ?int $units = null; |
||
64 | |||
65 | private ?bool $usage = false; |
||
66 | |||
67 | public function getId() |
||
68 | { |
||
69 | return $this->id; |
||
70 | } |
||
71 | |||
72 | public function setId($id): void |
||
73 | { |
||
74 | $this->id = $id; |
||
75 | } |
||
76 | |||
77 | public function getAmount(): ?int |
||
78 | { |
||
79 | return $this->amount; |
||
80 | } |
||
81 | |||
82 | public function setAmount(?int $amount): void |
||
83 | { |
||
84 | $this->amount = $amount; |
||
85 | } |
||
86 | |||
87 | public function getCurrency(): string |
||
88 | { |
||
89 | return strtoupper($this->currency); |
||
90 | } |
||
91 | |||
92 | public function setCurrency(string $currency): void |
||
93 | { |
||
94 | $this->currency = $currency; |
||
95 | } |
||
96 | |||
97 | public function getExternalReference(): ?string |
||
98 | { |
||
99 | return $this->externalReference; |
||
100 | } |
||
101 | |||
102 | public function setExternalReference(?string $externalReference): void |
||
103 | { |
||
104 | $this->externalReference = $externalReference; |
||
105 | } |
||
106 | |||
107 | public function hasExternalReference(): bool |
||
108 | { |
||
109 | return isset($this->externalReference); |
||
110 | } |
||
111 | |||
112 | public function isRecurring(): bool |
||
113 | { |
||
114 | return $this->recurring; |
||
115 | } |
||
116 | |||
117 | public function setRecurring(bool $recurring): void |
||
118 | { |
||
119 | $this->recurring = $recurring; |
||
120 | } |
||
121 | |||
122 | public function getSchedule(): ?string |
||
123 | { |
||
124 | return $this->schedule; |
||
125 | } |
||
126 | |||
127 | public function setSchedule(?string $schedule): void |
||
128 | { |
||
129 | $this->schedule = $schedule; |
||
130 | } |
||
131 | |||
132 | public function isIncludingTax(): bool |
||
133 | { |
||
134 | return $this->includingTax; |
||
135 | } |
||
136 | |||
137 | public function setIncludingTax(bool $includingTax): void |
||
138 | { |
||
139 | $this->includingTax = $includingTax; |
||
140 | } |
||
141 | |||
142 | public function getAsMoney(): Money |
||
143 | { |
||
144 | return Money::ofMinor($this->amount, Currency::of($this->currency)); |
||
145 | } |
||
146 | |||
147 | public function getProduct(): Product |
||
148 | { |
||
149 | return $this->product; |
||
150 | } |
||
151 | |||
152 | public function setProduct(Product $product): void |
||
153 | { |
||
154 | $this->product = $product; |
||
155 | } |
||
156 | |||
157 | public function isPublic(): bool |
||
158 | { |
||
159 | return true === $this->public; |
||
160 | } |
||
161 | |||
162 | public function setPublic(?bool $public): void |
||
163 | { |
||
164 | $this->public = $public; |
||
165 | } |
||
166 | |||
167 | public function getPaymentProviderDetailsUrl(): ?string |
||
168 | { |
||
169 | return $this->paymentProviderDetailsUrl; |
||
170 | } |
||
171 | |||
172 | public function setPaymentProviderDetailsUrl(?string $paymentProviderDetailsUrl): void |
||
173 | { |
||
174 | $this->paymentProviderDetailsUrl = $paymentProviderDetailsUrl; |
||
175 | } |
||
176 | |||
177 | public function getDisplayName(): string |
||
178 | { |
||
179 | if ($this->recurring) { |
||
180 | $type = 'EmbeddedSubscription - '.$this->schedule; |
||
181 | } else { |
||
182 | $type = 'one-off'; |
||
183 | } |
||
184 | |||
185 | return (string) $this->getAsMoney().' - '.$type.' - '.$this->getProduct()?->getName(); |
||
186 | } |
||
187 | |||
188 | public function setDeletedAt(\DateTimeInterface $dateTime): DeletableInterface |
||
189 | { |
||
190 | $this->deletedAt = $dateTime; |
||
|
|||
191 | } |
||
192 | |||
193 | public function isDeleted(): bool |
||
194 | { |
||
195 | return true === $this->isDeleted; |
||
196 | } |
||
197 | |||
198 | public function markAsDeleted(): DeletableInterface |
||
199 | { |
||
200 | $this->deletedAt = new \DateTime('now'); |
||
201 | $this->isDeleted = true; |
||
202 | |||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | public function unmarkAsDeleted(): DeletableInterface |
||
207 | { |
||
208 | $this->deletedAt = null; |
||
209 | $this->isDeleted = false; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | public function getIsDeleted(): ?bool |
||
217 | } |
||
218 | |||
219 | public function setIsDeleted(?bool $isDeleted): void |
||
220 | { |
||
221 | $this->isDeleted = $isDeleted; |
||
222 | } |
||
223 | |||
224 | public function getCreatedAt(): \DateTimeInterface |
||
227 | } |
||
228 | |||
229 | public function setCreatedAt(\DateTimeInterface $createdAt): void |
||
230 | { |
||
231 | $this->createdAt = $createdAt; |
||
232 | } |
||
233 | |||
234 | public function getTierComponents(): Collection|array |
||
235 | { |
||
236 | return $this->tierComponents; |
||
237 | } |
||
238 | |||
239 | public function setTierComponents(Collection|array $tierComponents): void |
||
240 | { |
||
241 | $this->tierComponents = $tierComponents; |
||
242 | } |
||
243 | |||
244 | public function getType(): PriceType |
||
245 | { |
||
246 | return $this->type; |
||
247 | } |
||
248 | |||
249 | public function setType(PriceType $type): void |
||
252 | } |
||
253 | |||
254 | public function getUnits(): ?int |
||
255 | { |
||
256 | return $this->units; |
||
257 | } |
||
258 | |||
259 | public function setUnits(?int $units): void |
||
260 | { |
||
261 | $this->units = $units; |
||
262 | } |
||
263 | |||
264 | public function getUsage(): bool |
||
265 | { |
||
266 | return true === $this->usage; |
||
267 | } |
||
268 | |||
269 | public function setUsage(?bool $usage): void |
||
270 | { |
||
271 | $this->usage = $usage; |
||
272 | } |
||
273 | } |
||
274 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: