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 |
||
19 | class Bbb |
||
20 | { |
||
21 | use initMeeting; |
||
22 | |||
23 | /** |
||
24 | * @var |
||
25 | */ |
||
26 | private $response; |
||
27 | /** |
||
28 | * @var BigBlueButton |
||
29 | */ |
||
30 | protected $bbb; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Bbb constructor. |
||
35 | * |
||
36 | * @param BigBlueButton $bbb |
||
37 | */ |
||
38 | public function __construct(BigBlueButton $bbb) |
||
42 | |||
43 | /** |
||
44 | * @return BigBlueButton |
||
45 | */ |
||
46 | public function make() |
||
50 | /** |
||
51 | * Return a list of all meetings |
||
52 | * |
||
53 | * @return \Illuminate\Support\Collection |
||
54 | */ |
||
55 | public function all() |
||
71 | |||
72 | |||
73 | /** |
||
74 | * $meeting |
||
75 | * |
||
76 | * @param $meeting |
||
77 | * |
||
78 | * required fields |
||
79 | * meetingID |
||
80 | * meetingName |
||
81 | * |
||
82 | * @return mixed |
||
83 | */ |
||
84 | View Code Duplication | public function create($meeting) |
|
97 | |||
98 | /** |
||
99 | * @param $meeting |
||
100 | * |
||
101 | * required fields: |
||
102 | * meetingID |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | View Code Duplication | public function isMeetingRunning($meeting) |
|
122 | |||
123 | /** |
||
124 | * Join meeting |
||
125 | * |
||
126 | * @param $meeting |
||
127 | * required fields |
||
128 | * |
||
129 | * meetingID |
||
130 | * userName join by name |
||
131 | * password which role want to join |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function join($meeting) |
||
147 | |||
148 | /** |
||
149 | * Returns information about the meeting |
||
150 | * |
||
151 | * @param $meeting |
||
152 | * required fields |
||
153 | * meetingID |
||
154 | * moderatorPW must be there moderator password |
||
155 | * |
||
156 | * @return \Illuminate\Support\Collection |
||
157 | */ |
||
158 | View Code Duplication | public function getMeetingInfo($meeting) |
|
171 | |||
172 | /** |
||
173 | * @param $parameters |
||
174 | * |
||
175 | * required fields |
||
176 | * meetingID |
||
177 | * meetingName |
||
178 | * userName |
||
179 | * attendeePW |
||
180 | * moderatorPW |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | public function start($parameters) |
||
188 | |||
189 | /** |
||
190 | * Close meeting |
||
191 | * |
||
192 | * @param $meeting |
||
193 | * required fields: |
||
194 | * meetingID |
||
195 | * moderatorPW close meeting must be there moderator password |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function close($meeting) |
||
212 | |||
213 | /** |
||
214 | * |
||
215 | * @param $recording |
||
216 | * required fields |
||
217 | * meetingID |
||
218 | * |
||
219 | * optional fields |
||
220 | * recordID |
||
221 | * state |
||
222 | * @return \Illuminate\Support\Collection |
||
223 | */ |
||
224 | public function getRecordings($recording) |
||
242 | |||
243 | /** |
||
244 | * @param $recording |
||
245 | * recordID as string(sepeate by commma) |
||
246 | * publish as bool |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | View Code Duplication | public function publishRecordings($recording) |
|
266 | |||
267 | /** |
||
268 | * @param $recording |
||
269 | * |
||
270 | * required fields |
||
271 | * recordingID |
||
272 | * |
||
273 | * @return \Illuminate\Support\Collection |
||
274 | */ |
||
275 | public function deleteRecordings($recording) |
||
284 | |||
285 | /** |
||
286 | * @param $configXml |
||
287 | * |
||
288 | * @return \Illuminate\Support\Collection |
||
289 | */ |
||
290 | public function setConfigXml($configXml) |
||
299 | |||
300 | /** |
||
301 | * @return \BigBlueButton\Responses\GetDefaultConfigXMLResponse |
||
302 | */ |
||
303 | public function getDefaultConfigXml() |
||
308 | |||
309 | /** |
||
310 | * @return \Illuminate\Support\Collection |
||
311 | */ |
||
312 | public function getApiVersion() |
||
317 | |||
318 | /** |
||
319 | * @param $hooks |
||
320 | * |
||
321 | * @return \Illuminate\Support\Collection |
||
322 | */ |
||
323 | public function hooksCreate($hooks) |
||
332 | |||
333 | |||
334 | } |
||
335 |
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: