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 |
||
15 | class FormTimestamp |
||
16 | { |
||
17 | use Utils\FormTrait; |
||
18 | use Utils\CryptTrait; |
||
19 | |||
20 | /** |
||
21 | * @var string The honeypot input name |
||
22 | */ |
||
23 | protected $inputName = 'hpt_time'; |
||
24 | |||
25 | /** |
||
26 | * @var int Minimum seconds to determine whether the request is a bot |
||
27 | */ |
||
28 | protected $min = 3; |
||
29 | |||
30 | /** |
||
31 | * @var int Max seconds to expire the form. Zero to do not expire |
||
32 | */ |
||
33 | protected $max = 0; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | */ |
||
38 | public function __construct() |
||
42 | |||
43 | /** |
||
44 | * Set the field name. |
||
45 | * |
||
46 | * @param string $inputName |
||
47 | * |
||
48 | * @return self |
||
49 | */ |
||
50 | public function inputName($inputName) |
||
56 | |||
57 | /** |
||
58 | * Minimum time required. |
||
59 | * |
||
60 | * @param int $seconds |
||
61 | * |
||
62 | * @return self |
||
63 | */ |
||
64 | public function min($seconds) |
||
70 | |||
71 | /** |
||
72 | * Max time before expire the form. |
||
73 | * |
||
74 | * @param int $seconds |
||
75 | * |
||
76 | * @return self |
||
77 | */ |
||
78 | public function max($seconds) |
||
84 | |||
85 | /** |
||
86 | * Execute the middleware. |
||
87 | * |
||
88 | * @param ServerRequestInterface $request |
||
89 | * @param ResponseInterface $response |
||
90 | * @param callable $next |
||
91 | * |
||
92 | * @return ResponseInterface |
||
93 | */ |
||
94 | View Code Duplication | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
112 | |||
113 | /** |
||
114 | * Check whether the request is valid. |
||
115 | * |
||
116 | * @param ServerRequestInterface $request |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | protected function isValid(ServerRequestInterface $request) |
||
161 | } |
||
162 |
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.