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 ArrayRouteProvider implements RouteProviderInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var array[] |
||
17 | */ |
||
18 | protected $routes = []; |
||
19 | |||
20 | /** |
||
21 | * @param array[] $data |
||
22 | */ |
||
23 | public function __construct(array $data) |
||
27 | |||
28 | /** |
||
29 | * @return Generator |
||
30 | */ |
||
31 | public function getRoutes(): Generator |
||
41 | |||
42 | /** |
||
43 | * @param array $data |
||
44 | * |
||
45 | * @return string[] |
||
46 | * |
||
47 | * @throws Exception |
||
48 | */ |
||
49 | View Code Duplication | protected function getMethods(array $data): array |
|
58 | |||
59 | /** |
||
60 | * @param array $data |
||
61 | * |
||
62 | * @return string |
||
63 | * |
||
64 | * @throws Exception |
||
65 | */ |
||
66 | View Code Duplication | protected function getPath(array $data): string |
|
75 | |||
76 | /** |
||
77 | * @param array $data |
||
78 | * |
||
79 | * @return callable |
||
80 | * |
||
81 | * @throws Exception |
||
82 | */ |
||
83 | protected function getHandler(array $data): callable |
||
103 | } |
||
104 |
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.