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 | * Return a list of all meetings |
||
| 53 | * |
||
| 54 | * @return \Illuminate\Support\Collection |
||
| 55 | */ |
||
| 56 | public function all() |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * $meeting |
||
| 76 | * |
||
| 77 | * @param $meeting |
||
| 78 | * |
||
| 79 | * required fields |
||
| 80 | * meetingID |
||
| 81 | * meetingName |
||
| 82 | * |
||
| 83 | * @return mixed |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function create($meeting) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param $meeting |
||
| 101 | * |
||
| 102 | * required fields: |
||
| 103 | * meetingID |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function isMeetingRunning($meeting) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Join meeting |
||
| 126 | * |
||
| 127 | * @param $meeting |
||
| 128 | * required fields |
||
| 129 | * |
||
| 130 | * meetingID |
||
| 131 | * userName join by name |
||
| 132 | * password which role want to join |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function join($meeting) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns information about the meeting |
||
| 151 | * |
||
| 152 | * @param $meeting |
||
| 153 | * required fields |
||
| 154 | * meetingID |
||
| 155 | * moderatorPW must be there moderator password |
||
| 156 | * |
||
| 157 | * @return \Illuminate\Support\Collection |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function getMeetingInfo($meeting) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param $parameters |
||
| 175 | * |
||
| 176 | * required fields |
||
| 177 | * meetingID |
||
| 178 | * meetingName |
||
| 179 | * userName |
||
| 180 | * attendeePW |
||
| 181 | * moderatorPW |
||
| 182 | * |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | public function start($parameters) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Close meeting |
||
| 192 | * |
||
| 193 | * @param $meeting |
||
| 194 | * required fields: |
||
| 195 | * meetingID |
||
| 196 | * moderatorPW close meeting must be there moderator password |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function close($meeting) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * |
||
| 216 | * @param $recording |
||
| 217 | * required fields |
||
| 218 | * meetingID |
||
| 219 | * |
||
| 220 | * optional fields |
||
| 221 | * recordID |
||
| 222 | * state |
||
| 223 | * @return \Illuminate\Support\Collection |
||
| 224 | */ |
||
| 225 | public function getRecordings($recording) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param $recording |
||
| 246 | * recordID as string(sepeate by commma) |
||
| 247 | * publish as bool |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | View Code Duplication | public function publishRecordings($recording) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param $recording |
||
| 270 | * |
||
| 271 | * required fields |
||
| 272 | * recordingID |
||
| 273 | * |
||
| 274 | * @return \Illuminate\Support\Collection |
||
| 275 | */ |
||
| 276 | public function deleteRecordings($recording) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param $configXml |
||
| 288 | * |
||
| 289 | * @return \Illuminate\Support\Collection |
||
| 290 | */ |
||
| 291 | public function setConfigXml($configXml) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return \BigBlueButton\Responses\GetDefaultConfigXMLResponse |
||
| 303 | */ |
||
| 304 | public function getDefaultConfigXml() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return \Illuminate\Support\Collection |
||
| 312 | */ |
||
| 313 | public function getApiVersion() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $hooks |
||
| 321 | * |
||
| 322 | * @return \Illuminate\Support\Collection |
||
| 323 | */ |
||
| 324 | public function hooksCreate($hooks) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param $hooks |
||
| 336 | * |
||
| 337 | * @return \Illuminate\Support\Collection |
||
| 338 | */ |
||
| 339 | public function hooksDestroy($hooks) |
||
| 348 | |||
| 349 | } |
||
| 350 |
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: