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 |
||
13 | class Honeypot |
||
14 | { |
||
15 | use Utils\FormTrait; |
||
16 | |||
17 | /** |
||
18 | * @var string The honeypot input name |
||
19 | */ |
||
20 | protected $inputName = 'hpt_name'; |
||
21 | |||
22 | /** |
||
23 | * @var string The honeypot class name |
||
24 | */ |
||
25 | protected $inputClass = 'hpt_input'; |
||
26 | |||
27 | /** |
||
28 | * Set the field name. |
||
29 | * |
||
30 | * @param string $inputName |
||
31 | * |
||
32 | * @return self |
||
33 | */ |
||
34 | public function inputName($inputName) |
||
40 | |||
41 | /** |
||
42 | * Set the field class. |
||
43 | * |
||
44 | * @param string $inputClass |
||
45 | * |
||
46 | * @return self |
||
47 | */ |
||
48 | public function inputClass($inputClass) |
||
54 | |||
55 | /** |
||
56 | * Execute the middleware. |
||
57 | * |
||
58 | * @param ServerRequestInterface $request |
||
59 | * @param ResponseInterface $response |
||
60 | * @param callable $next |
||
61 | * |
||
62 | * @return ResponseInterface |
||
63 | */ |
||
64 | View Code Duplication | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
82 | |||
83 | /** |
||
84 | * Check whether the request is valid. |
||
85 | * |
||
86 | * @param ServerRequestInterface $request |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | protected function isValid(ServerRequestInterface $request) |
||
96 | } |
||
97 |
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.