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 |
||
25 | class ElevationService extends AbstractService |
||
26 | { |
||
27 | /** |
||
28 | * @param HttpClient $client |
||
29 | * @param MessageFactory $messageFactory |
||
30 | */ |
||
31 | public function __construct(HttpClient $client, MessageFactory $messageFactory) |
||
35 | |||
36 | /** |
||
37 | * @param ElevationRequestInterface $request |
||
38 | * |
||
39 | * @return ElevationResponse |
||
40 | */ |
||
41 | View Code Duplication | public function process(ElevationRequestInterface $request) |
|
48 | |||
49 | /** |
||
50 | * @param string $data |
||
51 | * |
||
52 | * @return mixed[] |
||
53 | */ |
||
54 | View Code Duplication | private function parse($data) |
|
62 | |||
63 | /** |
||
64 | * @param mixed[] $data |
||
65 | * |
||
66 | * @return ElevationResponse |
||
67 | */ |
||
68 | View Code Duplication | private function buildResponse(array $data) |
|
76 | |||
77 | /** |
||
78 | * @param mixed[] $data |
||
79 | * |
||
80 | * @return ElevationResult[] |
||
81 | */ |
||
82 | private function buildResults(array $data) |
||
92 | |||
93 | /** |
||
94 | * @param mixed[] $data |
||
95 | * |
||
96 | * @return ElevationResult |
||
97 | */ |
||
98 | private function buildResult(array $data) |
||
110 | |||
111 | /** |
||
112 | * @param mixed[] $data |
||
113 | * |
||
114 | * @return Coordinate |
||
115 | */ |
||
116 | private function buildCoordinate(array $data) |
||
120 | } |
||
121 |
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.