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:
| 1 | <?php |
||
| 34 | class Bridge extends Resource |
||
| 35 | { |
||
| 36 | const TYPE_MIXING = 'mixing'; |
||
| 37 | const TYPE_HOLDING = 'holding'; |
||
| 38 | const TYPE_DTMF_EVENTS = 'dtmf_events'; |
||
| 39 | const TYPE_PROXY_MEDIA = 'proxy_media'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string Bridging class |
||
| 43 | */ |
||
| 44 | private $bridgeClass; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string Type of bridge technology (mixing, holding, dtmf_events, proxy_media) |
||
| 48 | */ |
||
| 49 | private $bridgeType; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array Ids of channels participating in this bridge |
||
| 53 | */ |
||
| 54 | private $channelIds; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string Entity that created the bridge |
||
| 58 | */ |
||
| 59 | private $creator; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string Unique identifier for this bridge |
||
| 63 | */ |
||
| 64 | private $id; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string Unique identifier for this bridge |
||
| 68 | */ |
||
| 69 | private $name; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string Name of the current bridging technology |
||
| 73 | */ |
||
| 74 | private $technology; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return string Bridging class |
||
| 78 | */ |
||
| 79 | public function getBridgeClass() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string Type of bridge technology (mixing, holding, dtmf_events, proxy_media) |
||
| 86 | */ |
||
| 87 | public function getBridgeType() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return array Ids of channels participating in this bridge |
||
| 94 | */ |
||
| 95 | public function getChannelIds() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string Entity that created the bridge |
||
| 102 | */ |
||
| 103 | public function getCreator() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string Unique identifier for this bridge |
||
| 110 | */ |
||
| 111 | public function getId() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string Unique identifier for this bridge |
||
| 118 | */ |
||
| 119 | public function getName() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string Name of the current bridging technology |
||
| 126 | */ |
||
| 127 | public function getTechnology() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param callable $callback |
||
| 134 | */ |
||
| 135 | public function onBridgeCreated(callable $callback) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param callable $callback |
||
| 142 | */ |
||
| 143 | public function onceBridgeCreated(callable $callback) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param callable $callback |
||
| 150 | */ |
||
| 151 | public function onBridgeDestroyed(callable $callback) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param callable $callback |
||
| 158 | */ |
||
| 159 | public function onceBridgeDestroyed(callable $callback) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param callable $callback |
||
| 166 | */ |
||
| 167 | public function onBridgeVideoSourceChanged(callable $callback) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param callable $callback |
||
| 174 | */ |
||
| 175 | public function onceBridgeVideoSourceChanged(callable $callback) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param callable $callback |
||
| 182 | */ |
||
| 183 | public function onPeerStatusChange(callable $callback) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param callable $callback |
||
| 190 | */ |
||
| 191 | public function oncePeerStatusChange(callable $callback) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param callable $callback |
||
| 198 | */ |
||
| 199 | public function onBridgeBlindTransfer(callable $callback) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param callable $callback |
||
| 206 | */ |
||
| 207 | public function onceBridgeBlindTransfer(callable $callback) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param callable $callback |
||
| 214 | */ |
||
| 215 | public function onBridgeAttendedTransfer(callable $callback) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param callable $callback |
||
| 222 | */ |
||
| 223 | public function onceBridgeAttendedTransfer(callable $callback) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Shut down a bridge. If any channels are in this bridge, they will be removed and resume whatever they were doing beforehand. |
||
| 230 | * |
||
| 231 | * @throws NotFoundException |
||
| 232 | */ |
||
| 233 | public function deleteBridge() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Add a channel to a bridge. |
||
| 240 | * |
||
| 241 | * @param string $channel (required) Ids of channels to add to bridge. Allows comma separated values. |
||
| 242 | * @param string $role Channel's role in the bridge |
||
| 243 | * @throws NotFoundException |
||
| 244 | * @throws ConflictException |
||
| 245 | * @throws UnprocessableEntityException |
||
| 246 | */ |
||
| 247 | public function addChannel($channel, $role = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Remove a channel from a bridge. |
||
| 254 | * |
||
| 255 | * @param string $channel (required) Ids of channels to remove from bridge. Allows comma separated values. |
||
| 256 | * @throws NotFoundException |
||
| 257 | * @throws ConflictException |
||
| 258 | * @throws UnprocessableEntityException |
||
| 259 | */ |
||
| 260 | public function removeChannel($channel) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Play music on hold to a bridge or change the MOH class that is playing. |
||
| 267 | * |
||
| 268 | * @param string $mohClass Music on hold class to use |
||
| 269 | * @throws NotFoundException |
||
| 270 | * @throws ConflictException |
||
| 271 | */ |
||
| 272 | public function startMusicOnHold($mohClass) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Stop playing music on hold to a bridge. This will only stop music on hold being played via POST bridges/{bridgeId}/moh. |
||
| 279 | * |
||
| 280 | * @throws NotFoundException |
||
| 281 | * @throws ConflictException |
||
| 282 | */ |
||
| 283 | public function stopMusicOnHold() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Start playback of media on a bridge. The media URI may be any of a number of URI's. Currently |
||
| 290 | * sound:, recording:, number:, digits:, characters:, and tone: URI's are supported. This operation |
||
| 291 | * creates a playback resource that can be used to control the playback of media (pause, rewind, |
||
| 292 | * fast forward, etc.) |
||
| 293 | * |
||
| 294 | * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
||
| 295 | * |
||
| 296 | * @param string $media (required) Media's URI to play. |
||
| 297 | * @param string $lang For sounds, selects language for sound. |
||
| 298 | * @param int $offsetms Number of media to skip before playing. |
||
| 299 | * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
||
| 300 | * @param string $playbackId Playback Id. |
||
| 301 | * @return Playback |
||
| 302 | * @throws NotFoundException |
||
| 303 | * @throws ConflictException |
||
| 304 | */ |
||
| 305 | public function playMedia($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Start playback of media on a bridge. The media URI may be any of a number of URI's. Currently |
||
| 312 | * sound:, recording:, number:, digits:, characters:, and tone: URI's are supported. This operation |
||
| 313 | * creates a playback resource that can be used to control the playback of media (pause, rewind, |
||
| 314 | * fast forward, etc.) |
||
| 315 | * |
||
| 316 | * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
||
| 317 | * |
||
| 318 | * @param string $media (required) Media's URI to play. |
||
| 319 | * @param string $lang For sounds, selects language for sound. |
||
| 320 | * @param int $offsetms Number of media to skip before playing. |
||
| 321 | * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
||
| 322 | * @param string $playbackId Playback Id. |
||
| 323 | * @return Playback |
||
| 324 | * @throws NotFoundException |
||
| 325 | * @throws ConflictException |
||
| 326 | */ |
||
| 327 | public function playMediaWithId($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Start a recording. Record audio from a channel. Note that this will not capture audio sent to the |
||
| 334 | * channel. The bridge itself has a record feature if that's what you want. |
||
| 335 | * |
||
| 336 | * @param string $name (required) Recording's filename |
||
| 337 | * @param string $format (required) Format to encode audio in |
||
| 338 | * @param int $maxDurationSeconds Maximum duration of the recording, in seconds. 0 for no limit. Allowed range: Min: 0; Max: None |
||
| 339 | * @param int $maxSilenceSeconds Maximum duration of silence, in seconds. 0 for no limit. Allowed range: Min: 0; Max: None |
||
| 340 | * @param string $ifExists = Action to take if a recording with the same name already exists. default: fail, Allowed values: fail, overwrite, append |
||
| 341 | * @param boolean $beep Play beep when recording begins |
||
| 342 | * @param string $terminateOn DTMF input to terminate recording. Default: none, Allowed values: none, any, *, # |
||
| 343 | * @return LiveRecording |
||
| 344 | * @throws InvalidParameterException |
||
| 345 | * @throws NotFoundException |
||
| 346 | * @throws ConflictException |
||
| 347 | * @throws UnprocessableEntityException |
||
| 348 | */ |
||
| 349 | View Code Duplication | public function record( |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param AriClient $client |
||
| 364 | * @param string $response |
||
| 365 | */ |
||
| 366 | public function __construct(AriClient $client, $response) |
||
| 378 | |||
| 379 | } |
||
| 380 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: