Complex classes like Opportunity 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 Opportunity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class Opportunity extends ExtendOpportunity implements |
||
69 | EmailHolderInterface, |
||
70 | ChannelAwareInterface, |
||
71 | MultiCurrencyHolderInterface |
||
72 | { |
||
73 | use ChannelEntityTrait; |
||
74 | |||
75 | const INTERNAL_STATUS_CODE = 'opportunity_status'; |
||
76 | |||
77 | const STATUS_LOST = 'lost'; |
||
78 | const STATUS_WON = 'won'; |
||
79 | |||
80 | /** |
||
81 | * The key in system config for probability - status map |
||
82 | */ |
||
83 | const PROBABILITIES_CONFIG_KEY = 'oro_sales.default_opportunity_probabilities'; |
||
84 | |||
85 | /** |
||
86 | * @var int |
||
87 | * |
||
88 | * @ORM\Id |
||
89 | * @ORM\Column(type="integer", name="id") |
||
90 | * @ORM\GeneratedValue(strategy="AUTO") |
||
91 | * @ConfigField( |
||
92 | * defaultValues={ |
||
93 | * "importexport"={ |
||
94 | * "order"=0 |
||
95 | * } |
||
96 | * } |
||
97 | * ) |
||
98 | */ |
||
99 | protected $id; |
||
100 | |||
101 | /** |
||
102 | * @var OpportunityCloseReason |
||
103 | * |
||
104 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\SalesBundle\Entity\OpportunityCloseReason") |
||
105 | * @ORM\JoinColumn(name="close_reason_name", referencedColumnName="name") |
||
106 | * @ConfigField( |
||
107 | * defaultValues={ |
||
108 | * "dataaudit"={"auditable"=true}, |
||
109 | * "importexport"={ |
||
110 | * "order"=100, |
||
111 | * "short"=true |
||
112 | * } |
||
113 | * } |
||
114 | * ) |
||
115 | **/ |
||
116 | protected $closeReason; |
||
117 | |||
118 | /** |
||
119 | * @var Contact |
||
120 | * |
||
121 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\ContactBundle\Entity\Contact", cascade={"persist"}) |
||
122 | * @ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="SET NULL") |
||
123 | * @ConfigField( |
||
124 | * defaultValues={ |
||
125 | * "dataaudit"={"auditable"=true}, |
||
126 | * "importexport"={ |
||
127 | * "order"=120, |
||
128 | * "short"=true |
||
129 | * }, |
||
130 | * "form"={ |
||
131 | * "form_type"="oro_contact_select" |
||
132 | * } |
||
133 | * } |
||
134 | * ) |
||
135 | **/ |
||
136 | protected $contact; |
||
137 | |||
138 | /** |
||
139 | * @var Lead |
||
140 | * |
||
141 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\SalesBundle\Entity\Lead", inversedBy="opportunities") |
||
142 | * @ORM\JoinColumn(name="lead_id", referencedColumnName="id", onDelete="SET NULL") |
||
143 | * @ConfigField( |
||
144 | * defaultValues={ |
||
145 | * "dataaudit"={"auditable"=true}, |
||
146 | * "importexport"={ |
||
147 | * "order"=130, |
||
148 | * "short"=true |
||
149 | * } |
||
150 | * } |
||
151 | * ) |
||
152 | **/ |
||
153 | protected $lead; |
||
154 | |||
155 | /** |
||
156 | * @var User |
||
157 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User") |
||
158 | * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL") |
||
159 | * @ConfigField( |
||
160 | * defaultValues={ |
||
161 | * "dataaudit"={"auditable"=true, "immutable"=true}, |
||
162 | * "importexport"={ |
||
163 | * "order"=140, |
||
164 | * "short"=true |
||
165 | * } |
||
166 | * } |
||
167 | * ) |
||
168 | */ |
||
169 | protected $owner; |
||
170 | |||
171 | /** |
||
172 | * @var string |
||
173 | * |
||
174 | * @ORM\Column(name="name", type="string", length=255, nullable=false) |
||
175 | * @ConfigField( |
||
176 | * defaultValues={ |
||
177 | * "dataaudit"={"auditable"=true}, |
||
178 | * "importexport"={ |
||
179 | * "order"=10, |
||
180 | * "identity"=true |
||
181 | * } |
||
182 | * } |
||
183 | * ) |
||
184 | */ |
||
185 | protected $name; |
||
186 | |||
187 | /** |
||
188 | * @var \DateTime |
||
189 | * |
||
190 | * @ORM\Column(name="close_date", type="date", nullable=true) |
||
191 | * @ConfigField( |
||
192 | * defaultValues={ |
||
193 | * "dataaudit"={"auditable"=true, "immutable"=true}, |
||
194 | * "importexport"={ |
||
195 | * "order"=20 |
||
196 | * } |
||
197 | * } |
||
198 | * ) |
||
199 | */ |
||
200 | protected $closeDate; |
||
201 | |||
202 | /** |
||
203 | * @var float |
||
204 | * |
||
205 | * @ORM\Column(name="probability", type="percent", nullable=true) |
||
206 | * @ConfigField( |
||
207 | * defaultValues={ |
||
208 | * "form"={ |
||
209 | * "form_type"="oro_percent", |
||
210 | * "form_options"={ |
||
211 | * "constraints"={{"Range":{"min":0, "max":100}}}, |
||
212 | * } |
||
213 | * }, |
||
214 | * "dataaudit"={"auditable"=true, "immutable"=true}, |
||
215 | * "importexport"={ |
||
216 | * "order"=30 |
||
217 | * } |
||
218 | * } |
||
219 | * ) |
||
220 | */ |
||
221 | protected $probability; |
||
222 | |||
223 | /** |
||
224 | * Changes to this value object wont affect entity change set |
||
225 | * To change persisted price value you should create and set new Multicurrency |
||
226 | * |
||
227 | * @var Multicurrency |
||
228 | */ |
||
229 | protected $budgetAmount; |
||
230 | |||
231 | /** |
||
232 | * @var string |
||
233 | * |
||
234 | * @ORM\Column(name="budget_amount_currency", type="currency", length=3, nullable=true) |
||
235 | * @ConfigField( |
||
236 | * defaultValues={ |
||
237 | * "dataaudit"={"auditable"=true, "immutable"=true}, |
||
238 | * "importexport"={ |
||
239 | * "order"=55 |
||
240 | * } |
||
241 | * } |
||
242 | * ) |
||
243 | */ |
||
244 | protected $budgetAmountCurrency; |
||
245 | |||
246 | /** |
||
247 | * @var double |
||
248 | * |
||
249 | * @ORM\Column(name="budget_amount_value", type="money_value", nullable=true) |
||
250 | * @ConfigField( |
||
251 | * defaultValues={ |
||
252 | * "form"={ |
||
253 | * "form_type"="oro_money", |
||
254 | * "form_options"={ |
||
255 | * "constraints"={{"Range":{"min":0}}}, |
||
256 | * } |
||
257 | * }, |
||
258 | * "dataaudit"={ |
||
259 | * "auditable"=true |
||
260 | * }, |
||
261 | * "importexport"={ |
||
262 | * "order"=50 |
||
263 | * }, |
||
264 | * "multicurrency"={ |
||
265 | * "target" = "budgetAmount", |
||
266 | * "virtual_field" = "budgetAmountBaseCurrency" |
||
267 | * } |
||
268 | * } |
||
269 | * ) |
||
270 | */ |
||
271 | protected $budgetAmountValue; |
||
272 | |||
273 | /** |
||
274 | * Changes to this value object wont affect entity change set |
||
275 | * To change persisted price value you should create and set new Multicurrency |
||
276 | * |
||
277 | * @var Multicurrency |
||
278 | */ |
||
279 | protected $closeRevenue; |
||
280 | |||
281 | /** |
||
282 | * @var string |
||
283 | * |
||
284 | * @ORM\Column(name="close_revenue_currency", type="currency", length=3, nullable=true) |
||
285 | * @ConfigField( |
||
286 | * defaultValues={ |
||
287 | * "dataaudit"={"auditable"=true, "immutable"=true}, |
||
288 | * "importexport"={ |
||
289 | * "order"=65 |
||
290 | * } |
||
291 | * } |
||
292 | * ) |
||
293 | */ |
||
294 | protected $closeRevenueCurrency; |
||
295 | |||
296 | /** |
||
297 | * @var double |
||
298 | * |
||
299 | * @ORM\Column(name="close_revenue_value", type="money_value", nullable=true) |
||
300 | * @ConfigField( |
||
301 | * defaultValues={ |
||
302 | * "form"={ |
||
303 | * "form_type"="oro_money", |
||
304 | * "form_options"={ |
||
305 | * "constraints"={{"Range":{"min":0}}}, |
||
306 | * } |
||
307 | * }, |
||
308 | * "dataaudit"={ |
||
309 | * "auditable"=true |
||
310 | * }, |
||
311 | * "importexport"={ |
||
312 | * "order"=60 |
||
313 | * }, |
||
314 | * "multicurrency"={ |
||
315 | * "target" = "closeRevenue", |
||
316 | * "virtual_field" = "closeRevenueBaseCurrency" |
||
317 | * } |
||
318 | * } |
||
319 | * ) |
||
320 | */ |
||
321 | protected $closeRevenueValue; |
||
322 | |||
323 | /** |
||
324 | * @var string |
||
325 | * |
||
326 | * @ORM\Column(name="customer_need", type="text", nullable=true) |
||
327 | * @ConfigField( |
||
328 | * defaultValues={ |
||
329 | * "dataaudit"={"auditable"=true}, |
||
330 | * "importexport"={ |
||
331 | * "order"=60 |
||
332 | * } |
||
333 | * } |
||
334 | * ) |
||
335 | */ |
||
336 | protected $customerNeed; |
||
337 | |||
338 | /** |
||
339 | * @var string |
||
340 | * |
||
341 | * @ORM\Column(name="proposed_solution", type="text", nullable=true) |
||
342 | * @ConfigField( |
||
343 | * defaultValues={ |
||
344 | * "dataaudit"={"auditable"=true}, |
||
345 | * "importexport"={ |
||
346 | * "order"=70 |
||
347 | * } |
||
348 | * } |
||
349 | * ) |
||
350 | */ |
||
351 | protected $proposedSolution; |
||
352 | |||
353 | /** |
||
354 | * @var \DateTime |
||
355 | * |
||
356 | * @ORM\Column(name="created_at", type="datetime") |
||
357 | * @ConfigField( |
||
358 | * defaultValues={ |
||
359 | * "entity"={ |
||
360 | * "label"="oro.ui.created_at" |
||
361 | * }, |
||
362 | * "importexport"={ |
||
363 | * "excluded"=true |
||
364 | * } |
||
365 | * } |
||
366 | * ) |
||
367 | */ |
||
368 | protected $createdAt; |
||
369 | |||
370 | /** |
||
371 | * @var \DateTime |
||
372 | * |
||
373 | * @ORM\Column(name="updated_at", type="datetime") |
||
374 | * @ConfigField( |
||
375 | * defaultValues={ |
||
376 | * "entity"={ |
||
377 | * "label"="oro.ui.updated_at" |
||
378 | * }, |
||
379 | * "importexport"={ |
||
380 | * "excluded"=true |
||
381 | * } |
||
382 | * } |
||
383 | * ) |
||
384 | */ |
||
385 | protected $updatedAt; |
||
386 | |||
387 | /** |
||
388 | * @var string |
||
389 | * |
||
390 | * @ORM\Column(name="notes", type="text", nullable=true) |
||
391 | * @ConfigField( |
||
392 | * defaultValues={ |
||
393 | * "dataaudit"={ |
||
394 | * "auditable"=true |
||
395 | * }, |
||
396 | * "importexport"={ |
||
397 | * "order"=80 |
||
398 | * } |
||
399 | * } |
||
400 | * ) |
||
401 | */ |
||
402 | protected $notes; |
||
403 | |||
404 | /** |
||
405 | * @var Organization |
||
406 | * |
||
407 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization") |
||
408 | * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL") |
||
409 | */ |
||
410 | protected $organization; |
||
411 | |||
412 | /** |
||
413 | * @var B2bCustomer |
||
414 | * |
||
415 | * @ORM\ManyToOne( |
||
416 | * targetEntity="Oro\Bundle\SalesBundle\Entity\B2bCustomer", |
||
417 | * inversedBy="opportunities", |
||
418 | * cascade={"persist"} |
||
419 | * ) |
||
420 | * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="SET NULL") |
||
421 | * @ConfigField( |
||
422 | * defaultValues={ |
||
423 | * "dataaudit"={"auditable"=true}, |
||
424 | * "importexport"={ |
||
425 | * "order"=110, |
||
426 | * "short"=true |
||
427 | * }, |
||
428 | * "form"={ |
||
429 | * "form_type"="oro_sales_b2bcustomer_select" |
||
430 | * } |
||
431 | * } |
||
432 | * ) |
||
433 | */ |
||
434 | protected $customer; |
||
435 | |||
436 | /** |
||
437 | * @var \DateTime |
||
438 | * |
||
439 | * @ORM\Column(type="datetime", name="closed_at", nullable=true) |
||
440 | * @ConfigField( |
||
441 | * defaultValues={ |
||
442 | * "dataaudit"={"auditable"=true, "immutable"=true}, |
||
443 | * "importexport"={"excluded"=true, "immutable"=true} |
||
444 | * } |
||
445 | * ) |
||
446 | */ |
||
447 | protected $closedAt; |
||
448 | |||
449 | /** |
||
450 | * @return int |
||
451 | */ |
||
452 | public function getId() |
||
456 | |||
457 | /** |
||
458 | * @param Lead $lead |
||
459 | * @return Opportunity |
||
460 | */ |
||
461 | public function setLead($lead) |
||
467 | |||
468 | /** |
||
469 | * @return Lead |
||
470 | */ |
||
471 | public function getLead() |
||
475 | |||
476 | /** |
||
477 | * @return MultiCurrency|null |
||
478 | */ |
||
479 | public function getBudgetAmount() |
||
483 | |||
484 | /** |
||
485 | * @param MultiCurrency $budgetAmount|null |
||
|
|||
486 | * @return Opportunity |
||
487 | */ |
||
488 | public function setBudgetAmount(MultiCurrency $budgetAmount = null) |
||
495 | |||
496 | /** |
||
497 | * @ORM\PostLoad |
||
498 | */ |
||
499 | public function loadMultiCurrencyFields() |
||
504 | |||
505 | /** |
||
506 | * @ORM\PrePersist |
||
507 | * @ORM\PreUpdate |
||
508 | * |
||
509 | * @return void |
||
510 | */ |
||
511 | public function updateMultiCurrencyFields() |
||
528 | |||
529 | /** |
||
530 | * @param string $currency |
||
531 | * @return Opportunity |
||
532 | */ |
||
533 | public function setBudgetAmountCurrency($currency) |
||
543 | |||
544 | /** |
||
545 | * @param string $budgetAmountValue |
||
546 | * @return Opportunity |
||
547 | */ |
||
548 | public function setBudgetAmountValue($budgetAmountValue) |
||
558 | |||
559 | /** |
||
560 | * @return string |
||
561 | */ |
||
562 | public function getBudgetAmountCurrency() |
||
566 | |||
567 | /** |
||
568 | * @return float |
||
569 | */ |
||
570 | public function getBudgetAmountValue() |
||
574 | |||
575 | /** |
||
576 | * @param string $currency |
||
577 | * @return Opportunity |
||
578 | */ |
||
579 | public function setCloseRevenueCurrency($currency) |
||
589 | |||
590 | /** |
||
591 | * @param float $closeRevenueValue |
||
592 | * @return Opportunity |
||
593 | */ |
||
594 | public function setCloseRevenueValue($closeRevenueValue) |
||
604 | |||
605 | /** |
||
606 | * @return string |
||
607 | */ |
||
608 | public function getCloseRevenueCurrency() |
||
612 | |||
613 | /** |
||
614 | * @return float |
||
615 | */ |
||
616 | public function getCloseRevenueValue() |
||
620 | |||
621 | /** |
||
622 | * @param \DateTime $closeDate |
||
623 | * @return Opportunity |
||
624 | */ |
||
625 | public function setCloseDate($closeDate) |
||
631 | |||
632 | /** |
||
633 | * @return \DateTime |
||
634 | */ |
||
635 | public function getCloseDate() |
||
639 | |||
640 | /** |
||
641 | * @param Contact $contact |
||
642 | * @return Opportunity |
||
643 | */ |
||
644 | public function setContact($contact) |
||
650 | |||
651 | /** |
||
652 | * @return Contact |
||
653 | */ |
||
654 | public function getContact() |
||
658 | |||
659 | /** |
||
660 | * @param string $customerNeed |
||
661 | * @return Opportunity |
||
662 | */ |
||
663 | public function setCustomerNeed($customerNeed) |
||
669 | |||
670 | /** |
||
671 | * @return string |
||
672 | */ |
||
673 | public function getCustomerNeed() |
||
677 | |||
678 | /** |
||
679 | * @param float $probability |
||
680 | * @return Opportunity |
||
681 | */ |
||
682 | public function setProbability($probability) |
||
688 | |||
689 | /** |
||
690 | * @return float |
||
691 | */ |
||
692 | public function getProbability() |
||
696 | |||
697 | /** |
||
698 | * @param string $proposedSolution |
||
699 | * @return Opportunity |
||
700 | */ |
||
701 | public function setProposedSolution($proposedSolution) |
||
707 | |||
708 | /** |
||
709 | * @return string |
||
710 | */ |
||
711 | public function getProposedSolution() |
||
715 | |||
716 | /** |
||
717 | * @param string $name |
||
718 | * @return Opportunity |
||
719 | */ |
||
720 | public function setName($name) |
||
726 | |||
727 | /** |
||
728 | * @return string |
||
729 | */ |
||
730 | public function getName() |
||
734 | |||
735 | /** |
||
736 | * @param OpportunityCloseReason $closeReason |
||
737 | * @return Opportunity |
||
738 | */ |
||
739 | public function setCloseReason($closeReason) |
||
745 | |||
746 | /** |
||
747 | * @return OpportunityCloseReason |
||
748 | */ |
||
749 | public function getCloseReason() |
||
753 | |||
754 | /** |
||
755 | * @param MultiCurrency $closeRevenue|null |
||
756 | * @return Opportunity |
||
757 | */ |
||
758 | public function setCloseRevenue(MultiCurrency $closeRevenue = null) |
||
767 | |||
768 | /** |
||
769 | * @return MultiCurrency|null |
||
770 | */ |
||
771 | public function getCloseRevenue() |
||
775 | |||
776 | /** |
||
777 | * @return \DateTime |
||
778 | */ |
||
779 | public function getCreatedAt() |
||
783 | |||
784 | /** |
||
785 | * @param \DateTime $created |
||
786 | * @return Opportunity |
||
787 | */ |
||
788 | public function setCreatedAt($created) |
||
794 | |||
795 | /** |
||
796 | * @return \DateTime |
||
797 | */ |
||
798 | public function getUpdatedAt() |
||
802 | |||
803 | /** |
||
804 | * @param \DateTime $updated |
||
805 | * @return Opportunity |
||
806 | */ |
||
807 | public function setUpdatedAt($updated) |
||
813 | |||
814 | /** |
||
815 | * Get the primary email address of the related contact |
||
816 | * |
||
817 | * @return string |
||
818 | */ |
||
819 | public function getEmail() |
||
828 | |||
829 | public function __toString() |
||
833 | /** |
||
834 | * @ORM\PrePersist |
||
835 | */ |
||
836 | public function beforeSave() |
||
841 | |||
842 | /** |
||
843 | * @ORM\PreUpdate |
||
844 | */ |
||
845 | public function beforeUpdate() |
||
849 | |||
850 | /** |
||
851 | * @return User |
||
852 | */ |
||
853 | public function getOwner() |
||
857 | |||
858 | /** |
||
859 | * @param User $owningUser |
||
860 | * @return Opportunity |
||
861 | */ |
||
862 | public function setOwner($owningUser) |
||
868 | |||
869 | /** |
||
870 | * @return string |
||
871 | */ |
||
872 | public function getNotes() |
||
876 | |||
877 | /** |
||
878 | * @param string $notes |
||
879 | * @return Opportunity |
||
880 | */ |
||
881 | public function setNotes($notes) |
||
887 | |||
888 | /** |
||
889 | * @param B2bCustomer $customer |
||
890 | * @TODO remove null after BAP-5248 |
||
891 | */ |
||
892 | public function setCustomer(B2bCustomer $customer = null) |
||
896 | |||
897 | /** |
||
898 | * @return B2bCustomer |
||
899 | */ |
||
900 | public function getCustomer() |
||
904 | |||
905 | /** |
||
906 | * Set organization |
||
907 | * |
||
908 | * @param Organization $organization |
||
909 | * @return Opportunity |
||
910 | */ |
||
911 | public function setOrganization(Organization $organization = null) |
||
917 | |||
918 | /** |
||
919 | * Get organization |
||
920 | * |
||
921 | * @return Organization |
||
922 | */ |
||
923 | public function getOrganization() |
||
927 | |||
928 | /** |
||
929 | * Remove Customer |
||
930 | * |
||
931 | * @return Lead |
||
932 | */ |
||
933 | public function removeCustomer() |
||
937 | |||
938 | /** |
||
939 | * @param \DateTime $closedAt |
||
940 | */ |
||
941 | public function setClosedAt(\DateTime $closedAt = null) |
||
945 | |||
946 | /** |
||
947 | * @return \DateTime |
||
948 | */ |
||
949 | public function getClosedAt() |
||
953 | } |
||
954 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.