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 |
||
17 | class AdminLocaleListener implements EventSubscriberInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var TokenStorageInterface |
||
21 | */ |
||
22 | private $tokenStorage; |
||
23 | |||
24 | /** |
||
25 | * @var TranslatorInterface |
||
26 | */ |
||
27 | private $translator; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $defaultAdminLocale; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $providerKey; |
||
38 | |||
39 | /** |
||
40 | * @var AdminRouteHelper |
||
41 | */ |
||
42 | private $adminRouteHelper; |
||
43 | |||
44 | /** |
||
45 | * @param TokenStorageInterface $tokenStorage |
||
46 | * @param TranslatorInterface $translator |
||
47 | * @param string $defaultAdminLocale |
||
48 | * @param AdminRouteHelper $adminRouteHelper |
||
49 | * @param string $providerKey Firewall name to check against |
||
50 | 1 | */ |
|
51 | public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator, AdminRouteHelper $adminRouteHelper, $defaultAdminLocale, $providerKey = 'main') |
||
59 | |||
60 | /** |
||
61 | * onKernelRequest |
||
62 | * |
||
63 | * @param GetResponseEvent|ResponseEvent $event |
||
64 | 1 | */ |
|
65 | public function onKernelRequest($event) |
||
84 | |||
85 | /** |
||
86 | 1 | * @param TokenInterface $token |
|
87 | * @param $providerKey |
||
88 | 1 | * |
|
89 | * @return bool |
||
90 | */ |
||
91 | private function isAdminToken($providerKey, TokenInterface $token = null) |
||
95 | |||
96 | /** |
||
97 | * getSubscribedEvents |
||
98 | 4 | */ |
|
99 | public static function getSubscribedEvents() |
||
106 | } |
||
107 |
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.