Complex classes like Promotion 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 Promotion, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Promotion implements PromotionInterface |
||
| 21 | { |
||
| 22 | use TimestampableTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var mixed |
||
| 26 | */ |
||
| 27 | protected $id; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $code; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $name; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $description; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * When exclusive, promotion with top priority will be applied |
||
| 46 | * |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | protected $priority = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Cannot be applied together with other promotions |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $exclusive = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $usageLimit; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | protected $used = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \DateTimeInterface |
||
| 70 | */ |
||
| 71 | protected $startsAt; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var \DateTimeInterface |
||
| 75 | */ |
||
| 76 | protected $endsAt; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | protected $couponBased = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var Collection|PromotionCouponInterface[] |
||
| 85 | */ |
||
| 86 | protected $coupons; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var Collection|PromotionRuleInterface[] |
||
| 90 | */ |
||
| 91 | protected $rules; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var Collection|PromotionActionInterface[] |
||
| 95 | */ |
||
| 96 | protected $actions; |
||
| 97 | |||
| 98 | public function __construct() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function getId() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function getCode(): ?string |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function setCode(?string $code): void |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function getName(): ?string |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function setName(?string $name): void |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function getDescription(): ?string |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function setDescription(?string $description): void |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | public function getPriority(): int |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | public function setPriority(?int $priority): void |
||
| 175 | { |
||
| 176 | $this->priority = $priority ?? -1; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | */ |
||
| 182 | public function isExclusive(): bool |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | public function setExclusive(?bool $exclusive): void |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | */ |
||
| 198 | public function getUsageLimit(): ?int |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | public function setUsageLimit(?int $usageLimit): void |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | public function getUsed(): int |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | public function setUsed(?int $used): void |
||
| 226 | |||
| 227 | public function incrementUsed(): void |
||
| 231 | |||
| 232 | public function decrementUsed(): void |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function getStartsAt(): ?\DateTimeInterface |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function setStartsAt(?\DateTimeInterface $startsAt): void |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function getEndsAt(): ?\DateTimeInterface |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function setEndsAt(?\DateTimeInterface $endsAt): void |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function isCouponBased(): bool |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function setCouponBased(?bool $couponBased): void |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function getCoupons(): Collection |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function hasCoupons(): bool |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | public function hasCoupon(PromotionCouponInterface $coupon): bool |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function addCoupon(PromotionCouponInterface $coupon): void |
||
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritdoc} |
||
| 322 | */ |
||
| 323 | public function removeCoupon(PromotionCouponInterface $coupon): void |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function getRules(): Collection |
||
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | public function hasRules(): bool |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function hasRule(PromotionRuleInterface $rule): bool |
||
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | public function addRule(PromotionRuleInterface $rule): void |
||
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | public function removeRule(PromotionRuleInterface $rule): void |
||
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritdoc} |
||
| 375 | */ |
||
| 376 | public function getActions(): Collection |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function hasActions(): bool |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | public function hasAction(PromotionActionInterface $action): bool |
||
| 396 | |||
| 397 | /** |
||
| 398 | * {@inheritdoc} |
||
| 399 | */ |
||
| 400 | public function addAction(PromotionActionInterface $action): void |
||
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | */ |
||
| 411 | public function removeAction(PromotionActionInterface $action): void |
||
| 416 | } |
||
| 417 |
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..