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 |
||
38 | abstract class MediaBase extends AriClientAware |
||
39 | { |
||
40 | /** |
||
41 | * @return string 'channels' or 'bridges' |
||
42 | */ |
||
43 | abstract public function getType(); |
||
44 | |||
45 | /** |
||
46 | * Play music on hold to a channel. Using media operations such as /play on a channel playing MOH in |
||
47 | * this manner will suspend MOH without resuming automatically. If continuing music on hold is |
||
48 | * desired, the stasis application must reinitiate music on hold. |
||
49 | * |
||
50 | * @param string $id Bridge/Channel's id |
||
51 | * @param string $mohClass Music on hold class to use |
||
52 | * @throws NotFoundException |
||
53 | * @throws ConflictException |
||
54 | */ |
||
55 | View Code Duplication | public function startMusicOnHold($id, $mohClass) |
|
|
|||
56 | { |
||
57 | $uri = "/{$this->getType()}/$id/moh"; |
||
58 | try { |
||
59 | $this->client->getEndpoint()->post($uri, array( |
||
60 | 'mohClass' => $mohClass, |
||
61 | )); |
||
62 | } catch (Pest_NotFound $e) { |
||
63 | throw new NotFoundException($e); |
||
64 | } catch (Pest_Conflict $e) { |
||
65 | throw new ConflictException($e); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Stop playing music on hold to a channel. |
||
71 | * |
||
72 | * @param string $id Bridge/Channel's id |
||
73 | * @throws NotFoundException |
||
74 | * @throws ConflictException |
||
75 | */ |
||
76 | View Code Duplication | public function stopMusicOnHold($id) |
|
87 | |||
88 | /** |
||
89 | * Start playback of media. The media URI may be any of a number of URI's. Currently sound:, |
||
90 | * recording:, number:, digits:, characters:, and tone: URI's are supported. This operation creates a |
||
91 | * playback resource that can be used to control the playback of media (pause, rewind, fast forward, |
||
92 | * etc.) |
||
93 | * |
||
94 | * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
||
95 | * |
||
96 | * @param string $id Bridge/Channel's id |
||
97 | * @param string $media (required) Media's URI to play. |
||
98 | * @param string $lang For sounds, selects language for sound. |
||
99 | * @param int $offsetms Number of media to skip before playing. |
||
100 | * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
||
101 | * @param string $playbackId Playback Id. |
||
102 | * @return Playback |
||
103 | * @throws NotFoundException |
||
104 | * @throws ConflictException |
||
105 | */ |
||
106 | 2 | View Code Duplication | public function playMedia($id, $media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null) |
126 | |||
127 | /** |
||
128 | * Start playback of media and specify the playbackId. The media URI may be any of a number of URI's. |
||
129 | * Currently sound: and recording: URI's are supported. This operation creates a playback resource |
||
130 | * that can be used to control the playback of media (pause, rewind, fast forward, etc.) |
||
131 | * |
||
132 | * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
||
133 | * |
||
134 | * @param string $id Channel's id |
||
135 | * @param string $media (required) Media's URI to play. |
||
136 | * @param string $lang For sounds, selects language for sound. |
||
137 | * @param int $offsetms Number of media to skip before playing. |
||
138 | * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
||
139 | * @param string $playbackId Playback Id. |
||
140 | * @return Playback |
||
141 | * @throws NotFoundException |
||
142 | * @throws ConflictException |
||
143 | */ |
||
144 | 4 | View Code Duplication | public function playMediaWithId( |
168 | |||
169 | /** |
||
170 | * Start a recording. Record audio from a channel. Note that this will not capture audio sent to the |
||
171 | * channel. The bridge itself has a record feature if that's what you want. |
||
172 | * |
||
173 | * @param string $id Channel/Bridge ID |
||
174 | * @param string $name (required) Recording's filename |
||
175 | * @param string $format (required) Format to encode audio in |
||
176 | * @param int $maxDurationSeconds Maximum duration of the recording, in seconds. 0 for no limit |
||
177 | * @param int $maxSilenceSeconds Maximum duration of silence, in seconds. 0 for no limit |
||
178 | * @param string $ifExists = fail - Action to take if a recording with the same name already exists. |
||
179 | * @param boolean $beep string = fail - Action to take if a recording with the same name already exists. |
||
180 | * @param string $terminateOn none - DTMF input to terminate recording |
||
181 | * @return LiveRecording |
||
182 | * @throws InvalidParameterException |
||
183 | * @throws NotFoundException |
||
184 | * @throws ConflictException |
||
185 | * @throws UnprocessableEntityException |
||
186 | */ |
||
187 | public function record( |
||
220 | |||
221 | } |
||
222 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.