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 BusClient extends Client |
||
14 | { |
||
15 | /** |
||
16 | * Return the itinerary of one or more lines. |
||
17 | * |
||
18 | * @param int[] $lines |
||
19 | * @param \DateTime $date |
||
20 | * @return \stdClass |
||
21 | * @throws \RuntimeException |
||
22 | */ |
||
23 | 3 | public function getRouteLines(array $lines, DateTime $date) |
|
31 | |||
32 | /** |
||
33 | * Return calendar details for the given date interval. |
||
34 | * |
||
35 | * @param \DateTime $startDate |
||
36 | * @param \DateTime $endDate |
||
37 | * @return \stdClass |
||
38 | * @throws \RuntimeException |
||
39 | */ |
||
40 | 3 | public function getCalendar(DateTime $startDate, DateTime $endDate) |
|
48 | |||
49 | /** |
||
50 | * Return a list with line details. |
||
51 | * |
||
52 | * @param int[] $lines |
||
53 | * @param \DateTime $date |
||
54 | * @return \stdClass |
||
55 | * @throws \RuntimeException |
||
56 | */ |
||
57 | 3 | View Code Duplication | public function getListLines(array $lines, DateTime $date) |
65 | |||
66 | /** |
||
67 | * Return details about all line groups. |
||
68 | * |
||
69 | * @return \stdClass |
||
70 | * @throws \RuntimeException |
||
71 | */ |
||
72 | 3 | public function getGroups() |
|
76 | |||
77 | /** |
||
78 | * Return start and end operation times of one or more lines. |
||
79 | * |
||
80 | * @param int[] $lines |
||
81 | * @param \DateTime $date |
||
82 | * @return \stdClass |
||
83 | * @throws \RuntimeException |
||
84 | */ |
||
85 | 3 | View Code Duplication | public function getTimesLines(array $lines, DateTime $date) |
93 | |||
94 | /** |
||
95 | * Return timetable details of one or more lines. |
||
96 | * |
||
97 | * @param int[] $lines |
||
98 | * @param \DateTime $date |
||
99 | * @return \stdClass |
||
100 | * @throws \RuntimeException |
||
101 | */ |
||
102 | 3 | View Code Duplication | public function getTimeTableLines(array $lines, DateTime $date) |
110 | |||
111 | /** |
||
112 | * Return details of one or more bus stops including name, served lines |
||
113 | * and geographic coordinates. |
||
114 | * |
||
115 | * @param int[] $stopIds |
||
116 | * @return \stdClass |
||
117 | * @throws \RuntimeException |
||
118 | */ |
||
119 | 3 | public function getNodesLines(array $stopIds) |
|
126 | |||
127 | /** |
||
128 | * Make an arbitrary call to the BUS service. |
||
129 | * |
||
130 | * @param string $endpoint |
||
131 | * @param array $params |
||
132 | * @return \stdClass |
||
133 | */ |
||
134 | 21 | protected function callBusService($endpoint, array $params = []) |
|
139 | } |
||
140 |
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.