| Total Complexity | 41 |
| Total Lines | 245 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 130 | } |
||
| 131 | |||
| 132 | public function isIncludingTax(): bool |
||
| 133 | { |
||
| 134 | return $this->includingTax; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function setIncludingTax(bool $includingTax): void |
||
| 140 | } |
||
| 141 | |||
| 142 | public function getAsMoney(): Money |
||
| 143 | { |
||
| 144 | if (!$this->amount) { |
||
|
|
|||
| 145 | return Money::zero(Currency::of($this->currency)); |
||
| 146 | } |
||
| 147 | |||
| 148 | return Money::ofMinor($this->amount, Currency::of($this->currency)); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getProduct(): Product |
||
| 152 | { |
||
| 153 | return $this->product; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function setProduct(Product $product): void |
||
| 157 | { |
||
| 158 | $this->product = $product; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function isPublic(): bool |
||
| 162 | { |
||
| 163 | return true === $this->public; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function setPublic(?bool $public): void |
||
| 167 | { |
||
| 168 | $this->public = $public; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function getPaymentProviderDetailsUrl(): ?string |
||
| 172 | { |
||
| 173 | return $this->paymentProviderDetailsUrl; |
||
| 174 | } |
||
| 175 | |||
| 176 | public function setPaymentProviderDetailsUrl(?string $paymentProviderDetailsUrl): void |
||
| 177 | { |
||
| 178 | $this->paymentProviderDetailsUrl = $paymentProviderDetailsUrl; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function getDisplayName(): string |
||
| 182 | { |
||
| 183 | if ($this->recurring) { |
||
| 184 | $type = 'EmbeddedSubscription - '.$this->schedule; |
||
| 185 | } else { |
||
| 186 | $type = 'one-off'; |
||
| 187 | } |
||
| 188 | |||
| 189 | return (string) $this->getAsMoney().' - '.$type.' - '.$this->getProduct()?->getName(); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function setDeletedAt(\DateTimeInterface $dateTime): DeletableInterface |
||
| 193 | { |
||
| 194 | $this->deletedAt = $dateTime; |
||
| 195 | } |
||
| 196 | |||
| 197 | public function isDeleted(): bool |
||
| 198 | { |
||
| 199 | return true === $this->isDeleted; |
||
| 200 | } |
||
| 201 | |||
| 202 | public function markAsDeleted(): DeletableInterface |
||
| 203 | { |
||
| 204 | $this->deletedAt = new \DateTime('now'); |
||
| 205 | $this->isDeleted = true; |
||
| 206 | |||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function unmarkAsDeleted(): DeletableInterface |
||
| 211 | { |
||
| 212 | $this->deletedAt = null; |
||
| 213 | $this->isDeleted = false; |
||
| 214 | |||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getIsDeleted(): ?bool |
||
| 221 | } |
||
| 222 | |||
| 223 | public function setIsDeleted(?bool $isDeleted): void |
||
| 224 | { |
||
| 225 | $this->isDeleted = $isDeleted; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getCreatedAt(): \DateTimeInterface |
||
| 231 | } |
||
| 232 | |||
| 233 | public function setCreatedAt(\DateTimeInterface $createdAt): void |
||
| 234 | { |
||
| 235 | $this->createdAt = $createdAt; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function getTierComponents(): Collection|array |
||
| 239 | { |
||
| 240 | return $this->tierComponents; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function setTierComponents(Collection|array $tierComponents): void |
||
| 244 | { |
||
| 245 | $this->tierComponents = $tierComponents; |
||
| 246 | } |
||
| 247 | |||
| 248 | public function getType(): PriceType |
||
| 249 | { |
||
| 250 | return $this->type; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function setType(PriceType $type): void |
||
| 256 | } |
||
| 257 | |||
| 258 | public function getUnits(): ?int |
||
| 261 | } |
||
| 262 | |||
| 263 | public function setUnits(?int $units): void |
||
| 264 | { |
||
| 265 | $this->units = $units; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function getUsage(): bool |
||
| 269 | { |
||
| 270 | return true === $this->usage; |
||
| 271 | } |
||
| 272 | |||
| 273 | public function setUsage(?bool $usage): void |
||
| 276 | } |
||
| 277 | } |
||
| 278 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: