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:
Complex classes like Bbb often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Bbb, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Bbb |
||
25 | { |
||
26 | use InitMeeting, |
||
27 | InitRecordings, |
||
28 | InitHooks, |
||
29 | InitConfigXml, |
||
30 | InitExtra; |
||
31 | |||
32 | /** |
||
33 | * @var |
||
34 | */ |
||
35 | private $response; |
||
36 | /** |
||
37 | * @var BigBlueButton |
||
38 | */ |
||
39 | protected $bbb; |
||
40 | |||
41 | /** |
||
42 | * Bbb constructor. |
||
43 | * |
||
44 | * @param BigBlueButton $bbb |
||
45 | */ |
||
46 | public function __construct(BigBlueButton $bbb) |
||
50 | |||
51 | /** |
||
52 | * for specific server instance. |
||
53 | * |
||
54 | * @param $serverName |
||
55 | * |
||
56 | * @return Bbb |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | public function server($serverName) |
||
72 | |||
73 | /** |
||
74 | * @return self |
||
75 | */ |
||
76 | public function make() |
||
80 | |||
81 | /** |
||
82 | * @return BigBlueButton |
||
83 | */ |
||
84 | public function source() |
||
88 | |||
89 | /** |
||
90 | * check url and secret is working. |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function isConnect() |
||
102 | |||
103 | /** |
||
104 | * Return a list of all meetings. |
||
105 | * |
||
106 | * @return \Illuminate\Support\Collection |
||
107 | */ |
||
108 | public function all() |
||
109 | { |
||
110 | $this->response = $this->bbb->getMeetings(); |
||
111 | View Code Duplication | if ($this->response->success()) { |
|
112 | if (! is_null($this->response->getRawXml()->meetings->meeting) && count($this->response->getRawXml()->meetings->meeting) > 0) { |
||
113 | $meetings = []; |
||
114 | foreach ($this->response->getRawXml()->meetings->meeting as $meeting) { |
||
115 | $meetings[] = XmlToArray($meeting); |
||
116 | } |
||
117 | |||
118 | return collect(XmlToArray($meetings)); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return collect([]); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * $meeting. |
||
127 | * |
||
128 | * @param $meeting |
||
129 | * |
||
130 | * required fields |
||
131 | * meetingID |
||
132 | * meetingName |
||
133 | * |
||
134 | * @return mixed |
||
135 | */ |
||
136 | View Code Duplication | public function create($meeting) |
|
149 | |||
150 | /** |
||
151 | * @param $meeting |
||
152 | * |
||
153 | * required fields: |
||
154 | * meetingID |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | View Code Duplication | public function isMeetingRunning($meeting) |
|
174 | |||
175 | /** |
||
176 | * Join meeting. |
||
177 | * |
||
178 | * @param $meeting |
||
179 | * required fields |
||
180 | * |
||
181 | * meetingID |
||
182 | * userName join by name |
||
183 | * password which role want to join |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | public function join($meeting) |
||
199 | |||
200 | /** |
||
201 | * Returns information about the meeting. |
||
202 | * |
||
203 | * @param $meeting |
||
204 | * required fields |
||
205 | * meetingID |
||
206 | * moderatorPW must be there moderator password |
||
207 | * |
||
208 | * @return \Illuminate\Support\Collection |
||
209 | */ |
||
210 | View Code Duplication | public function getMeetingInfo($meeting) |
|
223 | |||
224 | /** |
||
225 | * @param $parameters |
||
226 | * |
||
227 | * required fields |
||
228 | * meetingID |
||
229 | * meetingName |
||
230 | * userName |
||
231 | * attendeePW |
||
232 | * moderatorPW |
||
233 | * |
||
234 | * @return mixed |
||
235 | */ |
||
236 | public function start($parameters) |
||
240 | |||
241 | /** |
||
242 | * Close meeting. |
||
243 | * |
||
244 | * @param $meeting |
||
245 | * required fields: |
||
246 | * meetingID |
||
247 | * moderatorPW close meeting must be there moderator password |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function close($meeting) |
||
264 | |||
265 | /** |
||
266 | * @param $recording |
||
267 | * required fields |
||
268 | * meetingID |
||
269 | * |
||
270 | * optional fields |
||
271 | * recordID |
||
272 | * state |
||
273 | * |
||
274 | * @return \Illuminate\Support\Collection |
||
275 | */ |
||
276 | public function getRecordings($recording) |
||
277 | { |
||
278 | if (! $recording instanceof GetRecordingsParameters) { |
||
279 | $recording = $this->initGetRecordings($recording); |
||
280 | } |
||
281 | |||
282 | $this->response = $this->bbb->getRecordings($recording); |
||
283 | View Code Duplication | if ($this->response->success() && ! is_null($this->response->getRawXml()->recordings->recording) && count($this->response->getRawXml()->recordings->recording) > 0) { |
|
284 | $recordings = []; |
||
285 | foreach ($this->response->getRawXml()->recordings->recording as $r) { |
||
286 | $recordings[] = XmlToArray($r); |
||
287 | } |
||
288 | |||
289 | return collect($recordings); |
||
290 | } |
||
291 | |||
292 | return collect([]); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param $recording |
||
297 | * recordID as string(separated by comma) |
||
298 | * publish as bool |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | View Code Duplication | public function publishRecordings($recording) |
|
318 | |||
319 | /** |
||
320 | * @param $recording |
||
321 | * |
||
322 | * required fields |
||
323 | * recordingID |
||
324 | * |
||
325 | * @return \Illuminate\Support\Collection |
||
326 | */ |
||
327 | public function deleteRecordings($recording) |
||
337 | |||
338 | /** |
||
339 | * @param $configXml |
||
340 | * |
||
341 | * @return \Illuminate\Support\Collection |
||
342 | * @throws \Exception |
||
343 | */ |
||
344 | public function setConfigXml($configXml) |
||
354 | |||
355 | /** |
||
356 | * @return \BigBlueButton\Responses\GetDefaultConfigXMLResponse |
||
357 | */ |
||
358 | public function getDefaultConfigXml() |
||
364 | |||
365 | /** |
||
366 | * @return \Illuminate\Support\Collection |
||
367 | */ |
||
368 | public function getApiVersion() |
||
374 | |||
375 | /** |
||
376 | * @param $hooks |
||
377 | * |
||
378 | * @return \Illuminate\Support\Collection |
||
379 | */ |
||
380 | public function hooksCreate($hooks) |
||
390 | |||
391 | /** |
||
392 | * @param $hooks |
||
393 | * |
||
394 | * @return \Illuminate\Support\Collection |
||
395 | */ |
||
396 | public function hooksDestroy($hooks) |
||
406 | } |
||
407 |
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: