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 | const ENDPOINT = 'https://openbus.emtmadrid.es:9443'; |
||
16 | |||
17 | /** |
||
18 | * Return the itinerary of one or more lines. |
||
19 | * |
||
20 | * @var int[] $lines |
||
21 | * @var \DateTime $date |
||
22 | * @return \stdClass |
||
23 | * @throws \RuntimeException |
||
24 | */ |
||
25 | public function getRouteLines(array $lines, DateTime $date) |
||
33 | |||
34 | /** |
||
35 | * Return calendar details for the given date interval. |
||
36 | * |
||
37 | * @var \DateTime $startDate |
||
38 | * @var \DateTime $endDate |
||
39 | * @return \stdClass |
||
40 | * @throws \RuntimeException |
||
41 | */ |
||
42 | public function getCalendar(DateTime $startDate, DateTime $endDate) |
||
50 | |||
51 | /** |
||
52 | * Return a list with line details. |
||
53 | * |
||
54 | * @var int[] $lines |
||
55 | * @var \DateTime $date |
||
56 | * @return \stdClass |
||
57 | * @throws \RuntimeException |
||
58 | */ |
||
59 | View Code Duplication | public function getListLines(array $lines, DateTime $date) |
|
67 | |||
68 | /** |
||
69 | * Return details about all line groups. |
||
70 | * |
||
71 | * @return \stdClass |
||
72 | * @throws \RuntimeException |
||
73 | */ |
||
74 | public function getGroups() |
||
78 | |||
79 | /** |
||
80 | * Return start and end operation times of one or more lines. |
||
81 | * |
||
82 | * @var int[] $lines |
||
83 | * @var \DateTime $date |
||
84 | * @return \stdClass |
||
85 | * @throws \RuntimeException |
||
86 | */ |
||
87 | View Code Duplication | public function getTimesLines(array $lines, DateTime $date) |
|
95 | |||
96 | /** |
||
97 | * Return timetable details of one or more lines. |
||
98 | * |
||
99 | * @var int[] $lines |
||
100 | * @var \DateTime $date |
||
101 | * @return \stdClass |
||
102 | * @throws \RuntimeException |
||
103 | */ |
||
104 | View Code Duplication | public function getTimeTableLines(array $lines, DateTime $date) |
|
112 | |||
113 | /** |
||
114 | * Return details of one or more bus stops including name, served lines |
||
115 | * and geographic coordinates. |
||
116 | * |
||
117 | * @var int[] $stopIds |
||
118 | * @return \stdClass |
||
119 | * @throws \RuntimeException |
||
120 | */ |
||
121 | public function getNodesLines(array $stopIds) |
||
128 | |||
129 | /** |
||
130 | * Set the RequestLauncher instance to be used by this client. |
||
131 | * |
||
132 | * @param \Afonso\Emt\RequestLauncher $launcher |
||
133 | */ |
||
134 | public function setRequestLauncher(RequestLauncher $launcher) |
||
138 | |||
139 | /** |
||
140 | * Make an arbitrary call to the Bus service. |
||
141 | * |
||
142 | * @param string $endpoint |
||
143 | * @param array $params |
||
144 | * @return \stdClass |
||
145 | */ |
||
146 | protected function callBusService($endpoint, array $params = []) |
||
151 | } |
||
152 |
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.