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 |
||
25 | class CookieManager extends Component implements MiddlewareInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var EncrypterInterface |
||
29 | */ |
||
30 | private $encrypter = null; |
||
31 | |||
32 | /** |
||
33 | * @var HttpConfig |
||
34 | */ |
||
35 | private $httpConfig = null; |
||
36 | |||
37 | /** |
||
38 | * @invisible |
||
39 | * @var ContainerInterface |
||
40 | */ |
||
41 | protected $container = null; |
||
42 | |||
43 | /** |
||
44 | * @param HttpConfig $httpConfig |
||
45 | * @param ContainerInterface $container Lazy access to encrypter. |
||
46 | */ |
||
47 | public function __construct(HttpConfig $httpConfig, ContainerInterface $container) |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | View Code Duplication | public function __invoke(Request $request, Response $response, callable $next = null) |
|
78 | |||
79 | /** |
||
80 | * Unpack incoming cookies and decrypt their content. |
||
81 | * |
||
82 | * @param Request $request |
||
83 | * |
||
84 | * @return Request |
||
85 | */ |
||
86 | protected function unpackCookies(Request $request): Request |
||
101 | |||
102 | /** |
||
103 | * Pack outcoming cookies with encrypted value. |
||
104 | * |
||
105 | * @param Response $response |
||
106 | * @param CookieQueue $queue |
||
107 | * |
||
108 | * @return Response |
||
109 | * |
||
110 | * @throws \Spiral\Encrypter\Exceptions\EncryptException |
||
111 | */ |
||
112 | protected function packCookies(Response $response, CookieQueue $queue): Response |
||
131 | |||
132 | /** |
||
133 | * Check if cookie has to be protected. |
||
134 | * |
||
135 | * @param string $cookie |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | protected function isProtected(string $cookie): bool |
||
148 | |||
149 | /** |
||
150 | * @param string|array $cookie |
||
151 | * |
||
152 | * @return array|mixed|null |
||
153 | */ |
||
154 | private function decodeCookie($cookie) |
||
178 | |||
179 | /** |
||
180 | * Get or create encrypter instance. |
||
181 | * |
||
182 | * @return EncrypterInterface |
||
183 | */ |
||
184 | protected function getEncrypter() |
||
193 | |||
194 | /** |
||
195 | * @param Cookie $cookie |
||
196 | * |
||
197 | * @return Cookie |
||
198 | */ |
||
199 | private function encodeCookie(Cookie $cookie): Cookie |
||
210 | |||
211 | /** |
||
212 | * Sign string. |
||
213 | * |
||
214 | * @param string|null $value |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | private function hmacSign($value): string |
||
226 | } |
||
227 |
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.