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:
1 | <?php |
||
31 | class TenantSubscriberSpec extends ObjectBehavior |
||
32 | { |
||
33 | public function let(ContainerInterface $container) |
||
37 | |||
38 | public function it_is_initializable() |
||
42 | |||
43 | public function it_implements_event_subscriber_interface() |
||
47 | |||
48 | public function it_subscribes_to_events() |
||
49 | { |
||
50 | $this->getSubscribedEvents()->shouldReturn([Events::prePersist, Events::preUpdate]); |
||
51 | } |
||
52 | |||
53 | public function it_should_skip_when_tenant_code_is_already_set_on_tenant_aware_object( |
||
67 | |||
68 | View Code Duplication | public function it_sets_the_tenant_code_on_pre_persist_doctrine_event( |
|
|
|||
69 | TenantContextInterface $tenantContext, |
||
70 | LifecycleEventArgs $event, |
||
71 | TenantAwareInterface $tenantAware, |
||
72 | ContainerInterface $container |
||
73 | ) { |
||
74 | $tenant = new Tenant(); |
||
75 | $tenant->setSubdomain('example.com'); |
||
76 | $tenant->setName('Example'); |
||
77 | $tenant->setCode('123456'); |
||
78 | |||
79 | $tenantAware->getTenantCode()->shouldBeCalled()->willReturn(null); |
||
80 | $event->getEntity()->willReturn($tenantAware); |
||
81 | $tenantContext->getTenant()->shouldBeCalled()->willReturn($tenant); |
||
82 | |||
83 | $tenantAware->setTenantCode('123456')->shouldBeCalled(); |
||
84 | |||
85 | $container->get('swp_multi_tenancy.tenant_context')->willReturn($tenantContext); |
||
86 | |||
87 | $this->prePersist($event)->shouldBeNull(); |
||
88 | } |
||
89 | |||
90 | public function it_sets_only_tenant_aware_interface_implementation_on_pre_presist( |
||
101 | } |
||
102 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.