Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OwnerFormExtension 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 OwnerFormExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class OwnerFormExtension extends AbstractTypeExtension |
||
40 | { |
||
41 | /** @var DoctrineHelper */ |
||
42 | protected $doctrineHelper; |
||
43 | |||
44 | /** @var OwnershipMetadataProvider */ |
||
45 | protected $ownershipMetadataProvider; |
||
46 | |||
47 | /** @var BusinessUnitManager */ |
||
48 | protected $businessUnitManager; |
||
49 | |||
50 | /** @var SecurityFacade */ |
||
51 | protected $securityFacade; |
||
52 | |||
53 | /** @var string */ |
||
54 | protected $fieldName; |
||
55 | |||
56 | /** @var string */ |
||
57 | protected $fieldLabel = 'oro.user.owner.label'; |
||
58 | |||
59 | /** @var bool */ |
||
60 | protected $isAssignGranted; |
||
61 | |||
62 | /** @var string */ |
||
63 | protected $accessLevel; |
||
64 | |||
65 | /** @var User */ |
||
66 | protected $currentUser; |
||
67 | |||
68 | /** @var AclVoter */ |
||
69 | protected $aclVoter; |
||
70 | |||
71 | /** @var OwnerTreeProvider */ |
||
72 | protected $treeProvider; |
||
73 | |||
74 | /** @var int */ |
||
75 | protected $oldOwner; |
||
76 | |||
77 | /** @var EntityOwnerAccessor */ |
||
78 | protected $entityOwnerAccessor; |
||
79 | |||
80 | /** |
||
81 | * @param DoctrineHelper $doctrineHelper |
||
82 | * @param OwnershipMetadataProvider $ownershipMetadataProvider |
||
83 | * @param BusinessUnitManager $businessUnitManager |
||
84 | * @param SecurityFacade $securityFacade |
||
85 | * @param AclVoter $aclVoter |
||
86 | * @param OwnerTreeProvider $treeProvider |
||
87 | * @param EntityOwnerAccessor $entityOwnerAccessor |
||
88 | */ |
||
89 | public function __construct( |
||
90 | DoctrineHelper $doctrineHelper, |
||
91 | OwnershipMetadataProvider $ownershipMetadataProvider, |
||
92 | BusinessUnitManager $businessUnitManager, |
||
93 | SecurityFacade $securityFacade, |
||
94 | AclVoter $aclVoter, |
||
95 | OwnerTreeProvider $treeProvider, |
||
96 | EntityOwnerAccessor $entityOwnerAccessor |
||
97 | ) { |
||
98 | $this->doctrineHelper = $doctrineHelper; |
||
99 | $this->ownershipMetadataProvider = $ownershipMetadataProvider; |
||
100 | $this->businessUnitManager = $businessUnitManager; |
||
101 | $this->securityFacade = $securityFacade; |
||
102 | $this->aclVoter = $aclVoter; |
||
103 | $this->treeProvider = $treeProvider; |
||
104 | $this->entityOwnerAccessor = $entityOwnerAccessor; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the name of the type being extended. |
||
109 | * |
||
110 | * @return string The name of the type being extended |
||
111 | */ |
||
112 | public function getExtendedType() |
||
116 | |||
117 | /** |
||
118 | * @param FormBuilderInterface $builder |
||
119 | * @param array $options |
||
120 | * |
||
121 | * @throws \LogicException when getOwner method isn't implemented for entity with ownership type |
||
122 | * |
||
123 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
124 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
125 | */ |
||
126 | public function buildForm(FormBuilderInterface $builder, array $options) |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function setDefaultOptions(OptionsResolverInterface $resolver) |
||
200 | |||
201 | /** |
||
202 | * Save old owner id of record |
||
203 | * |
||
204 | * @param FormEvent $event |
||
205 | */ |
||
206 | public function preSubmit(FormEvent $event) |
||
216 | |||
217 | /** |
||
218 | * Validate owner |
||
219 | * |
||
220 | * @param FormEvent $event |
||
221 | */ |
||
222 | public function postSubmit(FormEvent $event) |
||
267 | |||
268 | /** |
||
269 | * Process form after data is set and remove/disable owner field depending on permissions |
||
270 | * |
||
271 | * @param FormEvent $event |
||
272 | */ |
||
273 | public function preSetData(FormEvent $event) |
||
304 | |||
305 | /** |
||
306 | * @param FormBuilderInterface|FormInterface $builder |
||
307 | * @param $dataClass |
||
308 | * @param string $permission |
||
309 | * @param array $data |
||
310 | * @param int $entityId |
||
311 | */ |
||
312 | protected function addUserOwnerField($builder, $dataClass, $permission = "CREATE", $data = null, $entityId = 0) |
||
354 | |||
355 | /** |
||
356 | * Check if current entity is BusinessUnit |
||
357 | * |
||
358 | * @param string $className |
||
359 | * |
||
360 | * @return bool |
||
361 | */ |
||
362 | protected function checkIsBusinessUnitEntity($className) |
||
371 | |||
372 | /** |
||
373 | * @param FormBuilderInterface $builder |
||
374 | * @param User $user |
||
375 | * @param string $className |
||
376 | */ |
||
377 | protected function addBusinessUnitOwnerField($builder, User $user, $className) |
||
444 | |||
445 | /** |
||
446 | * @param Organization $organization |
||
447 | * |
||
448 | * @return null|BusinessUnit |
||
449 | */ |
||
450 | protected function getCurrentBusinessUnit(Organization $organization) |
||
467 | |||
468 | /** |
||
469 | * @return null|User |
||
470 | */ |
||
471 | protected function getCurrentUser() |
||
482 | |||
483 | /** |
||
484 | * @return bool|Organization |
||
485 | */ |
||
486 | protected function getCurrentOrganization() |
||
495 | |||
496 | /** |
||
497 | * @return int|null |
||
498 | */ |
||
499 | protected function getOrganizationContextId() |
||
503 | |||
504 | /** |
||
505 | * Check is granting user to object in given permission |
||
506 | * |
||
507 | * @param string $permission |
||
508 | * @param object|string $object |
||
509 | */ |
||
510 | protected function checkIsGranted($permission, $object) |
||
517 | |||
518 | /** |
||
519 | * Get metadata for entity |
||
520 | * |
||
521 | * @param object|string $entity |
||
522 | * |
||
523 | * @return bool|OwnershipMetadataInterface |
||
524 | * @throws \LogicException |
||
525 | */ |
||
526 | View Code Duplication | protected function getMetadata($entity) |
|
541 | |||
542 | /** |
||
543 | * Get business units ids for current user for current access level |
||
544 | * |
||
545 | * @return array |
||
546 | * value -> business unit id |
||
547 | */ |
||
548 | protected function getBusinessUnitIds() |
||
549 | { |
||
550 | if (AccessLevel::SYSTEM_LEVEL == $this->accessLevel) { |
||
551 | return $this->businessUnitManager->getBusinessUnitIds(); |
||
552 | } elseif (AccessLevel::LOCAL_LEVEL == $this->accessLevel) { |
||
553 | return $this->treeProvider->getTree()->getUserBusinessUnitIds( |
||
554 | $this->currentUser->getId(), |
||
555 | $this->getOrganizationContextId() |
||
556 | ); |
||
557 | View Code Duplication | } elseif (AccessLevel::DEEP_LEVEL === $this->accessLevel) { |
|
558 | return $this->treeProvider->getTree()->getUserSubordinateBusinessUnitIds( |
||
559 | $this->currentUser->getId(), |
||
560 | $this->getOrganizationContextId() |
||
561 | ); |
||
562 | } elseif (AccessLevel::GLOBAL_LEVEL === $this->accessLevel) { |
||
563 | return $this->businessUnitManager->getBusinessUnitIds($this->getOrganizationContextId()); |
||
564 | } |
||
565 | |||
566 | return []; |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Get organization from security facade |
||
571 | * |
||
572 | * @return bool|Organization |
||
573 | */ |
||
574 | protected function getOrganization() |
||
578 | } |
||
579 |