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 |
||
20 | final class BashRestController |
||
21 | { |
||
22 | |||
23 | const SECRET_HEADER = 'X-Secret'; |
||
24 | |||
25 | /** |
||
26 | * @var Executor |
||
27 | */ |
||
28 | private $executor; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $secret; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $scripts; |
||
39 | |||
40 | 6 | public function __construct(ContainerInterface $ci, Executor $executor) |
|
46 | |||
47 | |||
48 | /** |
||
49 | * @param Request $request |
||
50 | * @param Response $response |
||
51 | * @param array $args |
||
52 | * @return Response |
||
53 | */ |
||
54 | 6 | public function __invoke(Request $request, Response $response, array $args) |
|
70 | |||
71 | |||
72 | /** |
||
73 | * @param Request $request |
||
74 | * @return bool |
||
75 | */ |
||
76 | 6 | View Code Duplication | private function isSecured(Request $request) |
87 | |||
88 | |||
89 | /** |
||
90 | * @param array|NULL $data |
||
91 | * @return array |
||
92 | */ |
||
93 | 4 | private function flatten($data) |
|
117 | |||
118 | |||
119 | /** |
||
120 | * @param string $projectName |
||
121 | * @param string $action |
||
122 | * @return bool |
||
123 | */ |
||
124 | 5 | private function isHandled($projectName, $action) |
|
128 | |||
129 | |||
130 | /** |
||
131 | * @param array $actual |
||
132 | * @param array $flattened |
||
133 | * @param array $toProcess |
||
134 | * @return array |
||
135 | */ |
||
136 | 2 | private function flattenProcessArray(array $actual, array &$flattened, array &$toProcess) |
|
154 | |||
155 | |||
156 | /** |
||
157 | * @param Request $request |
||
158 | * @param string $projectName |
||
159 | * @param string $action |
||
160 | * @return array |
||
161 | */ |
||
162 | 4 | private function handle(Request $request, $projectName, $action) |
|
171 | |||
172 | } |
||
173 |
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.