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 |
||
18 | class Bbb |
||
19 | { |
||
20 | use initMeeting; |
||
21 | |||
22 | /** |
||
23 | * @var |
||
24 | */ |
||
25 | private $response; |
||
26 | /** |
||
27 | * @var BigBlueButton |
||
28 | */ |
||
29 | protected $bbb; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Bbb constructor. |
||
34 | * |
||
35 | * @param BigBlueButton $bbb |
||
36 | */ |
||
37 | public function __construct(BigBlueButton $bbb) |
||
41 | |||
42 | /** |
||
43 | * @return BigBlueButton |
||
44 | */ |
||
45 | public function make() |
||
49 | /** |
||
50 | * Return a list of all meetings |
||
51 | * |
||
52 | * @return \Illuminate\Support\Collection |
||
53 | */ |
||
54 | public function all() |
||
70 | |||
71 | |||
72 | /** |
||
73 | * $meeting |
||
74 | * |
||
75 | * @param $meeting |
||
76 | * |
||
77 | * required fields |
||
78 | * meetingID |
||
79 | * meetingName |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | View Code Duplication | public function create($meeting) |
|
96 | |||
97 | /** |
||
98 | * @param $meeting |
||
99 | * |
||
100 | * required fields: |
||
101 | * meetingID |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | View Code Duplication | public function isMeetingRunning($meeting) |
|
121 | |||
122 | /** |
||
123 | * Join meeting |
||
124 | * |
||
125 | * @param $meeting |
||
126 | * required fields |
||
127 | * |
||
128 | * meetingID |
||
129 | * userName join by name |
||
130 | * password which role want to join |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function join($meeting) |
||
146 | |||
147 | /** |
||
148 | * Returns information about the meeting |
||
149 | * |
||
150 | * @param $meeting |
||
151 | * required fields |
||
152 | * meetingID |
||
153 | * moderatorPW must be there moderator password |
||
154 | * |
||
155 | * @return \Illuminate\Support\Collection |
||
156 | */ |
||
157 | View Code Duplication | public function getMeetingInfo($meeting) |
|
170 | |||
171 | /** |
||
172 | * @param $parameters |
||
173 | * |
||
174 | * required fields |
||
175 | * meetingID |
||
176 | * meetingName |
||
177 | * userName |
||
178 | * attendeePW |
||
179 | * moderatorPW |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function start($parameters) |
||
187 | |||
188 | /** |
||
189 | * Close meeting |
||
190 | * |
||
191 | * @param $meeting |
||
192 | * required fields: |
||
193 | * meetingID |
||
194 | * moderatorPW close meeting must be there moderator password |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function close($meeting) |
||
211 | |||
212 | /** |
||
213 | * |
||
214 | * @param $recording |
||
215 | * required fields |
||
216 | * meetingID |
||
217 | * |
||
218 | * optional fields |
||
219 | * recordID |
||
220 | * state |
||
221 | * @return \Illuminate\Support\Collection |
||
222 | */ |
||
223 | public function getRecordings($recording) |
||
241 | |||
242 | /** |
||
243 | * @param $recording |
||
244 | * recordID as string(sepeate by commma) |
||
245 | * publish as bool |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | View Code Duplication | public function publishRecordings($recording) |
|
265 | |||
266 | /** |
||
267 | * @param $recording |
||
268 | * |
||
269 | * required fields |
||
270 | * recordingID |
||
271 | * |
||
272 | * @return \Illuminate\Support\Collection |
||
273 | */ |
||
274 | public function deleteRecordings($recording) |
||
283 | |||
284 | /** |
||
285 | * @param $configXml |
||
286 | * |
||
287 | * @return \Illuminate\Support\Collection |
||
288 | */ |
||
289 | public function setConfigXml($configXml) |
||
298 | |||
299 | /** |
||
300 | * @return \BigBlueButton\Responses\GetDefaultConfigXMLResponse |
||
301 | */ |
||
302 | public function getDefaultConfigXml() |
||
307 | |||
308 | /** |
||
309 | * @return \Illuminate\Support\Collection |
||
310 | */ |
||
311 | public function getApiVersion() |
||
316 | |||
317 | |||
318 | |||
319 | } |
||
320 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: