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 |
||
10 | final class HookController |
||
11 | { |
||
12 | |||
13 | const SECRET_HEADER = 'X-Gitlab-Token'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $secret; |
||
19 | |||
20 | /** |
||
21 | * @var callable[] |
||
22 | */ |
||
23 | private $router; |
||
24 | |||
25 | |||
26 | 8 | public function __construct(ContainerInterface $ci, HookHandler $handler) |
|
46 | |||
47 | |||
48 | 8 | public function __invoke(Request $request, Response $response, array $args) |
|
65 | |||
66 | |||
67 | /** |
||
68 | * @param Request $request |
||
69 | * @return bool |
||
70 | */ |
||
71 | 8 | private function isValid(Request $request) |
|
80 | |||
81 | |||
82 | /** |
||
83 | * @param Request $request |
||
84 | * @return bool |
||
85 | */ |
||
86 | 7 | View Code Duplication | private function isSecured(Request $request) |
97 | |||
98 | |||
99 | /** |
||
100 | * @param Request $request |
||
101 | * @return bool |
||
102 | */ |
||
103 | 6 | private function isHandled(Request $request) |
|
108 | |||
109 | |||
110 | /** |
||
111 | * @param Request $request |
||
112 | */ |
||
113 | 5 | private function handle(Request $request) |
|
118 | |||
119 | } |
||
120 |
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.