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 |
||
| 20 | class Bbb |
||
| 21 | { |
||
| 22 | use initMeeting; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var |
||
| 26 | */ |
||
| 27 | private $response; |
||
| 28 | /** |
||
| 29 | * @var BigBlueButton |
||
| 30 | */ |
||
| 31 | protected $bbb; |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * Bbb constructor. |
||
| 36 | * |
||
| 37 | * @param BigBlueButton $bbb |
||
| 38 | */ |
||
| 39 | public function __construct(BigBlueButton $bbb) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return BigBlueButton |
||
| 46 | */ |
||
| 47 | public function make() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * check url and secret is working |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public function isConnect() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return a list of all meetings |
||
| 68 | * |
||
| 69 | * @return \Illuminate\Support\Collection |
||
| 70 | */ |
||
| 71 | public function all() |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * $meeting |
||
| 91 | * |
||
| 92 | * @param $meeting |
||
| 93 | * |
||
| 94 | * required fields |
||
| 95 | * meetingID |
||
| 96 | * meetingName |
||
| 97 | * |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function create($meeting) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param $meeting |
||
| 116 | * |
||
| 117 | * required fields: |
||
| 118 | * meetingID |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function isMeetingRunning($meeting) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Join meeting |
||
| 141 | * |
||
| 142 | * @param $meeting |
||
| 143 | * required fields |
||
| 144 | * |
||
| 145 | * meetingID |
||
| 146 | * userName join by name |
||
| 147 | * password which role want to join |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function join($meeting) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns information about the meeting |
||
| 166 | * |
||
| 167 | * @param $meeting |
||
| 168 | * required fields |
||
| 169 | * meetingID |
||
| 170 | * moderatorPW must be there moderator password |
||
| 171 | * |
||
| 172 | * @return \Illuminate\Support\Collection |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function getMeetingInfo($meeting) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @param $parameters |
||
| 190 | * |
||
| 191 | * required fields |
||
| 192 | * meetingID |
||
| 193 | * meetingName |
||
| 194 | * userName |
||
| 195 | * attendeePW |
||
| 196 | * moderatorPW |
||
| 197 | * |
||
| 198 | * @return mixed |
||
| 199 | */ |
||
| 200 | public function start($parameters) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Close meeting |
||
| 207 | * |
||
| 208 | * @param $meeting |
||
| 209 | * required fields: |
||
| 210 | * meetingID |
||
| 211 | * moderatorPW close meeting must be there moderator password |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | public function close($meeting) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * |
||
| 231 | * @param $recording |
||
| 232 | * required fields |
||
| 233 | * meetingID |
||
| 234 | * |
||
| 235 | * optional fields |
||
| 236 | * recordID |
||
| 237 | * state |
||
| 238 | * |
||
| 239 | * @return \Illuminate\Support\Collection |
||
| 240 | */ |
||
| 241 | public function getRecordings($recording) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param $recording |
||
| 262 | * recordID as string(sepeate by commma) |
||
| 263 | * publish as bool |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | View Code Duplication | public function publishRecordings($recording) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param $recording |
||
| 286 | * |
||
| 287 | * required fields |
||
| 288 | * recordingID |
||
| 289 | * |
||
| 290 | * @return \Illuminate\Support\Collection |
||
| 291 | */ |
||
| 292 | public function deleteRecordings($recording) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param $configXml |
||
| 305 | * |
||
| 306 | * @return \Illuminate\Support\Collection |
||
| 307 | */ |
||
| 308 | public function setConfigXml($configXml) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return \BigBlueButton\Responses\GetDefaultConfigXMLResponse |
||
| 321 | */ |
||
| 322 | public function getDefaultConfigXml() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return \Illuminate\Support\Collection |
||
| 331 | */ |
||
| 332 | public function getApiVersion() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param $hooks |
||
| 341 | * |
||
| 342 | * @return \Illuminate\Support\Collection |
||
| 343 | */ |
||
| 344 | public function hooksCreate($hooks) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $hooks |
||
| 357 | * |
||
| 358 | * @return \Illuminate\Support\Collection |
||
| 359 | */ |
||
| 360 | public function hooksDestroy($hooks) |
||
| 370 | |||
| 371 | } |
||
| 372 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: