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 | /** |
||
| 43 | * Bbb constructor. |
||
| 44 | * |
||
| 45 | * @param BigBlueButton $bbb |
||
| 46 | */ |
||
| 47 | public function __construct(BigBlueButton $bbb) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return self |
||
| 54 | */ |
||
| 55 | public function make() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return BigBlueButton |
||
| 62 | */ |
||
| 63 | public function source() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * check url and secret is working |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public function isConnect() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return a list of all meetings |
||
| 84 | * |
||
| 85 | * @return \Illuminate\Support\Collection |
||
| 86 | */ |
||
| 87 | public function all() |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * $meeting |
||
| 107 | * |
||
| 108 | * @param $meeting |
||
| 109 | * |
||
| 110 | * required fields |
||
| 111 | * meetingID |
||
| 112 | * meetingName |
||
| 113 | * |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function create($meeting) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param $meeting |
||
| 132 | * |
||
| 133 | * required fields: |
||
| 134 | * meetingID |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function isMeetingRunning($meeting) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Join meeting |
||
| 157 | * |
||
| 158 | * @param $meeting |
||
| 159 | * required fields |
||
| 160 | * |
||
| 161 | * meetingID |
||
| 162 | * userName join by name |
||
| 163 | * password which role want to join |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function join($meeting) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns information about the meeting |
||
| 182 | * |
||
| 183 | * @param $meeting |
||
| 184 | * required fields |
||
| 185 | * meetingID |
||
| 186 | * moderatorPW must be there moderator password |
||
| 187 | * |
||
| 188 | * @return \Illuminate\Support\Collection |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function getMeetingInfo($meeting) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param $parameters |
||
| 206 | * |
||
| 207 | * required fields |
||
| 208 | * meetingID |
||
| 209 | * meetingName |
||
| 210 | * userName |
||
| 211 | * attendeePW |
||
| 212 | * moderatorPW |
||
| 213 | * |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | public function start($parameters) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Close meeting |
||
| 223 | * |
||
| 224 | * @param $meeting |
||
| 225 | * required fields: |
||
| 226 | * meetingID |
||
| 227 | * moderatorPW close meeting must be there moderator password |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function close($meeting) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * |
||
| 247 | * @param $recording |
||
| 248 | * required fields |
||
| 249 | * meetingID |
||
| 250 | * |
||
| 251 | * optional fields |
||
| 252 | * recordID |
||
| 253 | * state |
||
| 254 | * |
||
| 255 | * @return \Illuminate\Support\Collection |
||
| 256 | */ |
||
| 257 | public function getRecordings($recording) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param $recording |
||
| 278 | * recordID as string(separated by comma) |
||
| 279 | * publish as bool |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function publishRecordings($recording) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param $recording |
||
| 302 | * |
||
| 303 | * required fields |
||
| 304 | * recordingID |
||
| 305 | * |
||
| 306 | * @return \Illuminate\Support\Collection |
||
| 307 | */ |
||
| 308 | public function deleteRecordings($recording) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $configXml |
||
| 321 | * |
||
| 322 | * @return \Illuminate\Support\Collection |
||
| 323 | * @throws \Exception |
||
| 324 | */ |
||
| 325 | public function setConfigXml($configXml) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return \BigBlueButton\Responses\GetDefaultConfigXMLResponse |
||
| 338 | */ |
||
| 339 | public function getDefaultConfigXml() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return \Illuminate\Support\Collection |
||
| 348 | */ |
||
| 349 | public function getApiVersion() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param $hooks |
||
| 358 | * |
||
| 359 | * @return \Illuminate\Support\Collection |
||
| 360 | */ |
||
| 361 | public function hooksCreate($hooks) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param $hooks |
||
| 374 | * |
||
| 375 | * @return \Illuminate\Support\Collection |
||
| 376 | */ |
||
| 377 | public function hooksDestroy($hooks) |
||
| 387 | |||
| 388 | } |
||
| 389 |
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: