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 |
||
12 | class SessionSecurityListener |
||
13 | { |
||
14 | /** |
||
15 | * @var LoggerInterface |
||
16 | */ |
||
17 | private $logger; |
||
18 | |||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | private $ipCheck; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $userAgentCheck; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $ip; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $userAgent; |
||
38 | |||
39 | /** |
||
40 | * @param bool $ipCheck |
||
41 | * @param bool $userAgentCheck |
||
42 | * @param LoggerInterface $logger |
||
43 | */ |
||
44 | public function __construct($ipCheck, $userAgentCheck, LoggerInterface $logger) |
||
50 | |||
51 | /** |
||
52 | * @param FilterResponseEvent $event |
||
53 | */ |
||
54 | public function onKernelResponse(FilterResponseEvent $event) |
||
72 | |||
73 | /** |
||
74 | * @param GetResponseEvent $event |
||
75 | */ |
||
76 | public function onKernelRequest(GetResponseEvent $event) |
||
107 | |||
108 | /** |
||
109 | * @param SessionInterface $session |
||
110 | * @param Request $request |
||
111 | */ |
||
112 | private function invalidateSession(SessionInterface $session, Request $request) |
||
118 | |||
119 | /** |
||
120 | * @param Request $request |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | private function getIp(Request $request) |
||
144 | |||
145 | /** |
||
146 | * @param Request $request |
||
147 | * |
||
148 | * @return array|string |
||
149 | */ |
||
150 | private function getUserAgent(Request $request) |
||
158 | } |
||
159 |
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.