Complex classes like UserSubscription 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 UserSubscription, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class UserSubscription implements DiscountableInterface, ProlongableItemInterface, PriceableInterface |
||
23 | { |
||
24 | const TYPE_PAID = 'P'; |
||
25 | const TYPE_PAID_NOW = 'PN'; |
||
26 | const TYPE_TRIAL = 'T'; |
||
27 | |||
28 | /** |
||
29 | * @ORM\Id |
||
30 | * @ORM\GeneratedValue |
||
31 | * @ORM\Column(type="integer", name="Id") |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $id; |
||
36 | |||
37 | /** |
||
38 | * @ORM\ManyToOne(targetEntity="Newscoop\Entity\User") |
||
39 | * @ORM\JoinColumn(name="IdUser", referencedColumnName="Id") |
||
40 | * |
||
41 | * @var Newscoop\Entity\User |
||
42 | */ |
||
43 | protected $user; |
||
44 | |||
45 | /** |
||
46 | * @ORM\ManyToOne(targetEntity="Newscoop\PaywallBundle\Entity\Subscription") |
||
47 | * @ORM\JoinColumn(name="IdSubscription", referencedColumnName="id") |
||
48 | * |
||
49 | * @var Newscoop\PaywallBundle\Entity\Subscriptions |
||
50 | */ |
||
51 | protected $subscription; |
||
52 | |||
53 | /** |
||
54 | * @ORM\ManyToOne(targetEntity="Order", inversedBy="items") |
||
55 | * @ORM\JoinColumn(name="order_id", referencedColumnName="id", nullable=false) |
||
56 | * |
||
57 | * @var Order |
||
58 | */ |
||
59 | protected $order; |
||
60 | |||
61 | /** |
||
62 | * @ORM\Column(type="integer", name="discount_total") |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $discountTotal = 0; |
||
67 | |||
68 | /** |
||
69 | * @ORM\ManyToOne(targetEntity="Newscoop\Entity\Publication") |
||
70 | * @ORM\JoinColumn(name="IdPublication", referencedColumnName="Id") |
||
71 | * |
||
72 | * @var Newscoop\Entity\Publication |
||
73 | */ |
||
74 | protected $publication; |
||
75 | |||
76 | /** |
||
77 | * @ORM\Column(type="decimal", name="ToPay") |
||
78 | * |
||
79 | * @var float |
||
80 | */ |
||
81 | protected $toPay = 0.0; |
||
82 | |||
83 | /** |
||
84 | * @ORM\Column(name="Type") |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $type; |
||
89 | |||
90 | /** |
||
91 | * @ORM\Column(name="Currency") |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $currency; |
||
96 | |||
97 | /** |
||
98 | * @ORM\ManyToOne(targetEntity="Newscoop\PaywallBundle\Entity\Trial") |
||
99 | * @ORM\JoinColumn(name="trial_id", referencedColumnName="id") |
||
100 | * |
||
101 | * @var Newscoop\PaywallBundle\Entity\Trial |
||
102 | */ |
||
103 | protected $trial; |
||
104 | |||
105 | /** |
||
106 | * Subscription status visible for admin. |
||
107 | * |
||
108 | * @ORM\Column(name="Active") |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $active; |
||
113 | |||
114 | /** |
||
115 | * @ORM\Column(type="datetime", name="created_at") |
||
116 | * |
||
117 | * @var DateTime |
||
118 | */ |
||
119 | protected $created_at; |
||
120 | |||
121 | /** |
||
122 | * @ORM\Column(type="datetime", name="expire_at", nullable=true) |
||
123 | * |
||
124 | * @var DateTime |
||
125 | */ |
||
126 | protected $expire_at; |
||
127 | |||
128 | /** |
||
129 | * Custom field. |
||
130 | * |
||
131 | * @ORM\Column(type="boolean", name="custom") |
||
132 | * |
||
133 | * @var bool |
||
134 | */ |
||
135 | protected $custom; |
||
136 | |||
137 | /** |
||
138 | * Second custom field. |
||
139 | * |
||
140 | * @ORM\Column(type="boolean", name="custom_2") |
||
141 | * |
||
142 | * @var bool |
||
143 | */ |
||
144 | protected $customOther; |
||
145 | |||
146 | /** |
||
147 | * To hide from users totally. |
||
148 | * |
||
149 | * @ORM\Column(type="boolean", name="is_active") |
||
150 | * |
||
151 | * @var bool |
||
152 | */ |
||
153 | protected $is_active; |
||
154 | |||
155 | /** |
||
156 | * Is prolonged? |
||
157 | * |
||
158 | * @ORM\Column(type="boolean", name="prolonged") |
||
159 | * |
||
160 | * @var bool |
||
161 | */ |
||
162 | protected $prolonged = false; |
||
163 | |||
164 | /** |
||
165 | * @ORM\Column(type="datetime", name="notify_sent_first", nullable=true) |
||
166 | * |
||
167 | * @var \DateTime |
||
168 | */ |
||
169 | protected $notifySentLevelOne; |
||
170 | |||
171 | /** |
||
172 | * @ORM\Column(type="datetime", name="notify_sent_second", nullable=true) |
||
173 | * |
||
174 | * @var \DateTime |
||
175 | */ |
||
176 | protected $notifySentLevelTwo; |
||
177 | |||
178 | /** |
||
179 | * @ORM\Column(type="datetime", name="updated_at", nullable=true) |
||
180 | * |
||
181 | * @var \DateTime |
||
182 | */ |
||
183 | protected $updated; |
||
184 | |||
185 | /** |
||
186 | * @ORM\Column(type="array", name="duration") |
||
187 | * |
||
188 | * @var array |
||
189 | */ |
||
190 | protected $duration; |
||
191 | |||
192 | protected $discount; |
||
193 | |||
194 | /** |
||
195 | * @ORM\OneToMany(targetEntity="Modification", mappedBy="orderItem", orphanRemoval=true, cascade={"all"}) |
||
196 | * |
||
197 | * @var ArrayCollection |
||
198 | */ |
||
199 | protected $modifications; |
||
200 | |||
201 | /** |
||
202 | * @ORM\ManyToMany(targetEntity="Discount", cascade={"persist"}) |
||
203 | * @ORM\JoinTable(name="plugin_paywall_order_item_discount", |
||
204 | * joinColumns={ |
||
205 | * @ORM\JoinColumn(name="order_item_id", referencedColumnName="Id") |
||
206 | * }, |
||
207 | * inverseJoinColumns={ |
||
208 | * @ORM\JoinColumn(name="discount_id", referencedColumnName="id") |
||
209 | * } |
||
210 | * ) |
||
211 | * |
||
212 | * @var Discount |
||
213 | */ |
||
214 | protected $discounts; |
||
215 | |||
216 | /** |
||
217 | * @ORM\ManyToOne(targetEntity="UserSubscription", inversedBy="children") |
||
218 | * @ORM\JoinColumn(name="parent_id", referencedColumnName="Id", onDelete="SET NULL") |
||
219 | * |
||
220 | * @var UserSubscription |
||
221 | */ |
||
222 | protected $parent; |
||
223 | |||
224 | /** |
||
225 | * @ORM\OneToMany(targetEntity="UserSubscription", mappedBy="parent") |
||
226 | * @ORM\OrderBy({"created_at" = "DESC"}) |
||
227 | */ |
||
228 | protected $children; |
||
229 | |||
230 | /** |
||
231 | * @ORM\Column(type="datetime", name="starts_at", nullable=true) |
||
232 | * |
||
233 | * @var DateTime |
||
234 | */ |
||
235 | protected $startsAt; |
||
236 | |||
237 | public function __construct() |
||
238 | { |
||
239 | $this->currency = ''; |
||
240 | $this->active = 'N'; |
||
241 | $this->created_at = new \DateTime(); |
||
|
|||
242 | $this->is_active = false; |
||
243 | $this->custom = false; |
||
244 | $this->customOther = false; |
||
245 | $this->type = self::TYPE_PAID; |
||
246 | $this->notifySentLevelOne = null; |
||
247 | $this->notifySentLevelTwo = null; |
||
248 | $this->modifications = new ArrayCollection(); |
||
249 | $this->discounts = new ArrayCollection(); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get id. |
||
254 | * |
||
255 | * @return int |
||
256 | */ |
||
257 | public function getId() |
||
258 | { |
||
259 | return (int) $this->id; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Set subscription. |
||
264 | * |
||
265 | * @param Newscoop\PaywallBundle\Entity\Subscription $subscription |
||
266 | */ |
||
267 | public function setSubscription($subscription) |
||
268 | { |
||
269 | $this->subscription = $subscription; |
||
270 | |||
271 | return $this; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get subscription. |
||
276 | * |
||
277 | * @return Newscoop\PaywallBundle\Entity\Subscription_specification |
||
278 | */ |
||
279 | public function getSubscription() |
||
280 | { |
||
281 | return $this->subscription; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Set user. |
||
286 | * |
||
287 | * @param Newscoop\Entity\User $user |
||
288 | */ |
||
289 | public function setUser(User $user) |
||
290 | { |
||
291 | $this->user = $user; |
||
292 | |||
293 | return $this; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Get user. |
||
298 | * |
||
299 | * @return Newscoop\Entity\User |
||
300 | */ |
||
301 | public function getUser() |
||
302 | { |
||
303 | return $this->user; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Set publication. |
||
308 | * |
||
309 | * @param Newscoop\Entity\Publication $publication |
||
310 | * |
||
311 | * @return Newscoop\Entity\Subscription |
||
312 | */ |
||
313 | public function setPublication(Publication $publication) |
||
314 | { |
||
315 | $this->publication = $publication; |
||
316 | |||
317 | return $this; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Get publication. |
||
322 | * |
||
323 | * @return Newscoop\Entity\Publication |
||
324 | */ |
||
325 | public function getPublication() |
||
329 | |||
330 | /** |
||
331 | * Get publication name. |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getPublicationName() |
||
336 | { |
||
337 | return $this->publication->getName(); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Get publication id. |
||
342 | * |
||
343 | * @return int |
||
344 | */ |
||
345 | public function getPublicationId() |
||
346 | { |
||
347 | return $this->publication->getId(); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Set to pay. |
||
352 | * |
||
353 | * @param float $toPay |
||
354 | * |
||
355 | * @return Newscoop\Entity\Subscription |
||
356 | */ |
||
357 | public function setToPay($toPay) |
||
358 | { |
||
359 | $this->toPay = (float) $toPay; |
||
360 | |||
361 | return $this; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Get to pay. |
||
366 | * |
||
367 | * @return float |
||
368 | */ |
||
369 | public function getToPay() |
||
373 | |||
374 | /** |
||
375 | * Set to pay. |
||
376 | * |
||
377 | * @param float $toPay |
||
378 | * |
||
379 | * @return Newscoop\Entity\Subscription |
||
380 | */ |
||
381 | public function setPrice($toPay) |
||
382 | { |
||
383 | $this->toPay = (float) $toPay; |
||
384 | |||
385 | return $this; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Get to pay. |
||
390 | * |
||
391 | * @return float |
||
392 | */ |
||
393 | public function getPrice() |
||
394 | { |
||
395 | return (float) $this->toPay; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Set type. |
||
400 | * |
||
401 | * @param string $type |
||
402 | * |
||
403 | * @return Newscoop\Entity\Subscription |
||
404 | */ |
||
405 | public function setType($type) |
||
406 | { |
||
407 | $this->type = strtoupper($type) === self::TYPE_TRIAL ? self::TYPE_TRIAL : self::TYPE_PAID; |
||
408 | |||
409 | return $this; |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Get type. |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | public function getType() |
||
421 | |||
422 | /** |
||
423 | * Test if is trial. |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function isTrial() |
||
431 | |||
432 | /** |
||
433 | * Set active. |
||
434 | * |
||
435 | * @param bool $active |
||
436 | * |
||
437 | * @return Newscoop\Entity\Subscription |
||
438 | */ |
||
439 | public function setActive($active) |
||
440 | { |
||
441 | $this->active = 'N'; |
||
442 | if ($active) { |
||
443 | $this->active = 'Y'; |
||
444 | } |
||
445 | |||
446 | return $this; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Is active. |
||
451 | * |
||
452 | * @return bool |
||
453 | */ |
||
454 | public function isActive() |
||
455 | { |
||
456 | return strtoupper($this->active) === 'Y'; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Get currency. |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | public function getCurrency() |
||
468 | |||
469 | /** |
||
470 | * Set currency. |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | public function setCurrency($currency) |
||
475 | { |
||
476 | $this->currency = $currency; |
||
477 | |||
478 | return $this; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Get trial. |
||
483 | * |
||
484 | * @return Trial |
||
485 | */ |
||
486 | public function getTrial() |
||
490 | |||
491 | /** |
||
492 | * Set trial. |
||
493 | * |
||
494 | * @return Trial |
||
495 | */ |
||
496 | public function setTrial($trial) |
||
502 | |||
503 | /** |
||
504 | * Get create date. |
||
505 | * |
||
506 | * @return datetime |
||
507 | */ |
||
508 | public function getCreatedAt() |
||
512 | |||
513 | /** |
||
514 | * Set create date. |
||
515 | * |
||
516 | * @param datetime $created_at |
||
517 | * |
||
518 | * @return datetime |
||
519 | */ |
||
520 | public function setCreatedAt(\DateTime $created_at) |
||
526 | |||
527 | /** |
||
528 | * Get expire date. |
||
529 | * |
||
530 | * @return datetime |
||
531 | */ |
||
532 | public function getExpireAt() |
||
536 | |||
537 | /** |
||
538 | * Set expire date. |
||
539 | * |
||
540 | * @param datetime $expire_at |
||
541 | * |
||
542 | * @return datetime |
||
543 | */ |
||
544 | public function setExpireAt(\DateTime $expire_at = null) |
||
550 | |||
551 | /** |
||
552 | * Get status. |
||
553 | * |
||
554 | * @return bool |
||
555 | */ |
||
556 | public function getIsActive() |
||
560 | |||
561 | /** |
||
562 | * Set status. |
||
563 | * |
||
564 | * @param bool $is_active |
||
565 | * |
||
566 | * @return bool |
||
567 | */ |
||
568 | public function setIsActive($is_active) |
||
574 | |||
575 | /** |
||
576 | * Gets the Custom field. |
||
577 | * |
||
578 | * @return bool |
||
579 | */ |
||
580 | public function getCustom() |
||
584 | |||
585 | /** |
||
586 | * Sets the Custom field. |
||
587 | * |
||
588 | * @param bool $custom the custom |
||
589 | * |
||
590 | * @return self |
||
591 | */ |
||
592 | public function setCustom($custom) |
||
598 | |||
599 | /** |
||
600 | * Gets the Second custom field. |
||
601 | * |
||
602 | * @return bool |
||
603 | */ |
||
604 | public function getCustomOther() |
||
608 | |||
609 | /** |
||
610 | * Sets the Second custom field. |
||
611 | * |
||
612 | * @param bool $customOther the custom other |
||
613 | * |
||
614 | * @return self |
||
615 | */ |
||
616 | public function setCustomOther($customOther) |
||
622 | |||
623 | /** |
||
624 | * Gets the value of updated. |
||
625 | * |
||
626 | * @return DateTime |
||
627 | */ |
||
628 | public function getUpdated() |
||
632 | |||
633 | /** |
||
634 | * Sets the value of updated. |
||
635 | * |
||
636 | * @param DateTime $updated the updated |
||
637 | * |
||
638 | * @return self |
||
639 | */ |
||
640 | public function setUpdated(\DateTime $updated) |
||
646 | |||
647 | /** |
||
648 | * Gets the value of notifySentLevelOne. |
||
649 | * |
||
650 | * @return \DateTime |
||
651 | */ |
||
652 | public function getNotifySentLevelOne() |
||
656 | |||
657 | /** |
||
658 | * Sets the value of notifySentLevelOne. |
||
659 | * |
||
660 | * @param \DateTime $notifySentLevelOne the notify sent level one |
||
661 | * |
||
662 | * @return self |
||
663 | */ |
||
664 | public function setNotifySentLevelOne(\DateTime $notifySentLevelOne) |
||
670 | |||
671 | /** |
||
672 | * Gets the value of notifySentLevelTwo. |
||
673 | * |
||
674 | * @return \DateTime |
||
675 | */ |
||
676 | public function getNotifySentLevelTwo() |
||
680 | |||
681 | /** |
||
682 | * Sets the value of notifySentLevelTwo. |
||
683 | * |
||
684 | * @param \DateTime $notifySentLevelTwo the notify sent level two |
||
685 | * |
||
686 | * @return self |
||
687 | */ |
||
688 | public function setNotifySentLevelTwo(\DateTime $notifySentLevelTwo) |
||
694 | |||
695 | /** |
||
696 | * Gets the value of discount. |
||
697 | * |
||
698 | * @return array |
||
699 | */ |
||
700 | public function getDiscount() |
||
704 | |||
705 | /** |
||
706 | * Sets the value of discount. |
||
707 | * |
||
708 | * @param array $discount the discount |
||
709 | * |
||
710 | * @return self |
||
711 | */ |
||
712 | public function setDiscount($discount) |
||
718 | |||
719 | /** |
||
720 | * Gets the value of duration. |
||
721 | * |
||
722 | * @return array |
||
723 | */ |
||
724 | public function getDuration() |
||
728 | |||
729 | /** |
||
730 | * Sets the value of duration. |
||
731 | * |
||
732 | * @param array $duration the duration |
||
733 | * |
||
734 | * @return self |
||
735 | */ |
||
736 | public function setDuration($duration) |
||
742 | |||
743 | /** |
||
744 | * Gets the value of order. |
||
745 | * |
||
746 | * @return Order |
||
747 | */ |
||
748 | public function getOrder() |
||
752 | |||
753 | /** |
||
754 | * Sets the value of order. |
||
755 | * |
||
756 | * @param Order $order the order |
||
757 | * |
||
758 | * @return self |
||
759 | */ |
||
760 | public function setOrder(Order $order) |
||
766 | |||
767 | /** |
||
768 | * {@inheritdoc} |
||
769 | */ |
||
770 | public function getDiscountTotal() |
||
774 | |||
775 | /** |
||
776 | * {@inheritdoc} |
||
777 | */ |
||
778 | public function setDiscountTotal($discountTotal) |
||
784 | |||
785 | /** |
||
786 | * Gets the value of modifications. |
||
787 | * Can filter modifications by type. |
||
788 | * |
||
789 | * @return ArrayCollection |
||
790 | */ |
||
791 | public function getModifications($type = null) |
||
801 | |||
802 | /** |
||
803 | * Sets the value of modifications. |
||
804 | * |
||
805 | * @param ArrayCollection $modifications the modifications |
||
806 | * |
||
807 | * @return self |
||
808 | */ |
||
809 | public function setModifications(ArrayCollection $modifications) |
||
815 | |||
816 | public function addModification(Modification $modification) |
||
825 | |||
826 | public function hasModification(Modification $modification) |
||
830 | |||
831 | public function removeModification(Modification $modification) |
||
840 | |||
841 | public function addDiscount($discount) |
||
849 | |||
850 | public function hasDiscount($discount) |
||
854 | |||
855 | public function removeDiscount($discount) |
||
863 | |||
864 | /** |
||
865 | * Gets the discounts. |
||
866 | * |
||
867 | * @return Discount |
||
868 | */ |
||
869 | public function getDiscounts() |
||
873 | |||
874 | /** |
||
875 | * Sets the discounts. |
||
876 | * |
||
877 | * @param ArrayCollection $discounts the discounts |
||
878 | * |
||
879 | * @return self |
||
880 | */ |
||
881 | public function setDiscounts($discounts) |
||
887 | |||
888 | public function calculateToPay() |
||
894 | |||
895 | /** |
||
896 | * Calculates all the modifications together with |
||
897 | * the total discount and unit price for single item. |
||
898 | * |
||
899 | * @return self |
||
900 | */ |
||
901 | public function calculateModificationsAndToPay() |
||
916 | |||
917 | /** |
||
918 | * {@inheritdoc} |
||
919 | */ |
||
920 | public function getProlonged() |
||
924 | |||
925 | /** |
||
926 | * {@inheritdoc} |
||
927 | */ |
||
928 | public function setProlonged($prolonged) |
||
934 | |||
935 | /** |
||
936 | * Gets the value of parent. |
||
937 | * |
||
938 | * @return UserSubscription |
||
939 | */ |
||
940 | public function getParent() |
||
944 | |||
945 | /** |
||
946 | * Sets the value of parent. |
||
947 | * |
||
948 | * @param UserSubscription $parent the parent |
||
949 | * |
||
950 | * @return self |
||
951 | */ |
||
952 | public function setParent(UserSubscription $parent) |
||
958 | |||
959 | /** |
||
960 | * Gets the value of children. |
||
961 | * |
||
962 | * @return mixed |
||
963 | */ |
||
964 | public function getChildren() |
||
968 | |||
969 | /** |
||
970 | * Sets the value of children. |
||
971 | * |
||
972 | * @param mixed $children the children |
||
973 | * |
||
974 | * @return self |
||
975 | */ |
||
976 | public function setChildren($children) |
||
982 | |||
983 | /** |
||
984 | * Gets the value of startsAt. |
||
985 | * |
||
986 | * @return DateTime |
||
987 | */ |
||
988 | public function getStartsAt() |
||
992 | |||
993 | /** |
||
994 | * Sets the value of startsAt. |
||
995 | * |
||
996 | * @param DateTime $startsAt the starts at |
||
997 | * |
||
998 | * @return self |
||
999 | */ |
||
1000 | public function setStartsAt(\DateTime $startsAt) |
||
1006 | } |
||
1007 |
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..