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 |
||
14 | class TenantResolver |
||
15 | { |
||
16 | const STRATEGY_TENANT_AWARE_SUBDOMAIN = 'tenant_aware'; |
||
17 | const STRATEGY_FIXED_SUBDOMAIN = 'fixed'; |
||
18 | |||
19 | /** |
||
20 | * @var RequestStack |
||
21 | */ |
||
22 | protected $requestStack; |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $domain; |
||
27 | |||
28 | /** |
||
29 | * @var EntityRepository |
||
30 | */ |
||
31 | protected $tenantRepository; |
||
32 | |||
33 | /** |
||
34 | * @var MultiTenantTenantInterface |
||
35 | */ |
||
36 | protected $tenant; |
||
37 | |||
38 | protected $strategy; |
||
39 | |||
40 | protected $token; |
||
41 | |||
42 | public function __construct($requestStack, $domain, $tenantRepository, TokenStorage $token, $strategy) |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Returns an tenant id based on current url |
||
54 | * |
||
55 | * @return int |
||
56 | * @throws \Exception |
||
57 | */ |
||
58 | public function getTenantId() |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Returns an tenant entity based on current url |
||
70 | * |
||
71 | * @return MultiTenantTenantInterface |
||
72 | * @throws \Exception |
||
73 | */ |
||
74 | public function getTenant() |
||
82 | |||
83 | public function isSubdomain() |
||
97 | |||
98 | /** |
||
99 | * @return MultiTenantTenantInterface |
||
100 | * @throws \Exception |
||
101 | */ |
||
102 | protected function resolveTenant() |
||
118 | |||
119 | public function getStrategy() |
||
123 | |||
124 | public function needStartScreen() |
||
131 | |||
132 | /** |
||
133 | * @return Tenant |
||
134 | */ |
||
135 | protected function resolveTenantFromUser() |
||
140 | |||
141 | /** |
||
142 | * @return Tenant |
||
143 | * @throws \Exception |
||
144 | */ |
||
145 | protected function resolveTenantFromSubdomain() |
||
171 | |||
172 | /** |
||
173 | * @param Tenant $tenant |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function overrideTenant(Tenant $tenant) |
||
182 | } |
||
183 |
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.