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 |
||
35 | class Bridges extends MediaBase |
||
36 | { |
||
37 | /** |
||
38 | * List all active bridges in Asterisk. |
||
39 | * |
||
40 | * @return Bridge[] |
||
41 | */ |
||
42 | 2 | public function getBridges() |
|
43 | { |
||
44 | 2 | $uri = '/bridges'; |
|
45 | 2 | $response = $this->client->getEndpoint()->get($uri); |
|
46 | |||
47 | 2 | $bridges = []; |
|
48 | 2 | foreach ((array)$response as $bridge) { |
|
49 | 2 | $bridges[] = new Bridge($this->client, $bridge); |
|
50 | 2 | } |
|
51 | |||
52 | 2 | return $bridges; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Create a new bridge. This bridge persists until it has been shut down, or Asterisk has been shut down. |
||
57 | * |
||
58 | * @param string $bridgeId Unique ID to give to the bridge being created. |
||
59 | * @param string $type Comma separated list of bridge type attributes (mixing, holding, dtmf_events, proxy_media). |
||
60 | * @param string $name Name to give to the bridge being created. |
||
61 | * @return Bridge |
||
62 | */ |
||
63 | 22 | View Code Duplication | public function createBridge($bridgeId, $type, $name) |
|
|||
64 | { |
||
65 | 22 | $uri = '/bridges'; |
|
66 | 22 | $response = $this->client->getEndpoint()->post($uri, array( |
|
67 | 22 | 'bridgeId' => $bridgeId, |
|
68 | 22 | 'type' => $type, |
|
69 | 22 | 'name' => $name, |
|
70 | 22 | )); |
|
71 | |||
72 | 22 | return new Bridge($this->client, $response); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Create a new bridge or updates an existing one. This bridge persists until it has been shut down, or Asterisk has been shut down. |
||
77 | * |
||
78 | * @param string $bridgeId Unique ID to give to the bridge being created. |
||
79 | * @param string $type Comma separated list of bridge type attributes (mixing, holding, dtmf_events, proxy_media) to set. |
||
80 | * @param string $name Set the name of the bridge. |
||
81 | * @return Bridge |
||
82 | */ |
||
83 | View Code Duplication | public function updateBridge($bridgeId, $type, $name) |
|
84 | { |
||
85 | $uri = "/bridges/$bridgeId"; |
||
86 | $response = $this->client->getEndpoint()->post($uri, array( |
||
87 | 'type' => $type, |
||
88 | 'name' => $name, |
||
89 | )); |
||
90 | |||
91 | return new Bridge($this->client, $response); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get bridge details. |
||
96 | * |
||
97 | * @param string $bridgeId Bridge's id |
||
98 | * @return Bridge |
||
99 | * @throws NotFoundException |
||
100 | */ |
||
101 | 9 | public function getBridge($bridgeId) |
|
102 | { |
||
103 | 9 | $uri = "/bridges/$bridgeId"; |
|
104 | try { |
||
105 | 9 | $response = $this->client->getEndpoint()->get($uri); |
|
106 | 9 | } catch (Pest_NotFound $e) { |
|
107 | 4 | throw new NotFoundException($e); |
|
108 | } |
||
109 | |||
110 | 5 | return new Bridge($this->client, $response); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Shut down a bridge. If any channels are in this bridge, they will be removed and resume whatever they were doing beforehand. |
||
115 | * |
||
116 | * @param string $bridgeId Bridge's id |
||
117 | * @throws NotFoundException |
||
118 | */ |
||
119 | 13 | View Code Duplication | public function deleteBridge($bridgeId) |
120 | { |
||
121 | 13 | $uri = "/bridges/$bridgeId"; |
|
122 | try { |
||
123 | 13 | $this->client->getEndpoint()->delete($uri); |
|
124 | 13 | } catch (Pest_NotFound $e) { |
|
125 | 2 | throw new NotFoundException($e); |
|
126 | } |
||
127 | 11 | } |
|
128 | |||
129 | /** |
||
130 | * Add a channel to a bridge. |
||
131 | * |
||
132 | * @param string $bridgeId Bridge's id |
||
133 | * @param string $channel (required) Ids of channels to add to bridge. Allows comma separated values. |
||
134 | * @param string $role Channel's role in the bridge |
||
135 | * @throws NotFoundException |
||
136 | * @throws ConflictException |
||
137 | * @throws UnprocessableEntityException |
||
138 | */ |
||
139 | 11 | public function addChannel($bridgeId, $channel, $role = null) |
|
140 | { |
||
141 | 11 | $uri = "/bridges/$bridgeId/addChannel"; |
|
142 | try { |
||
143 | 11 | $this->client->getEndpoint()->post($uri, array( |
|
144 | 11 | 'channel' => $channel, |
|
145 | 11 | 'role' => $role, |
|
146 | 11 | )); |
|
147 | 11 | } catch (Pest_BadRequest $e) { // Channel not found |
|
148 | 2 | throw new NotFoundException($e); |
|
149 | 2 | } catch (Pest_NotFound $e) { // Bridge not found |
|
150 | 2 | throw new NotFoundException($e); |
|
151 | } catch (Pest_Conflict $e) { // Bridge not in Stasis application; Channel currently recording |
||
152 | throw new ConflictException($e); |
||
153 | } catch (Pest_InvalidRecord $e) { // Channel not in Stasis application |
||
154 | throw new UnprocessableEntityException($e); |
||
155 | } |
||
156 | 7 | } |
|
157 | |||
158 | /** |
||
159 | * Remove a channel from a bridge. |
||
160 | * |
||
161 | * @param string $bridgeId Bridge's id |
||
162 | * @param string $channel (required) Ids of channels to remove from bridge. Allows comma separated values. |
||
163 | * @throws NotFoundException |
||
164 | * @throws ConflictException |
||
165 | * @throws UnprocessableEntityException |
||
166 | */ |
||
167 | 6 | View Code Duplication | public function removeChannel($bridgeId, $channel) |
168 | { |
||
169 | 6 | $uri = "/bridges/$bridgeId/removeChannel"; |
|
170 | try { |
||
171 | 6 | $this->client->getEndpoint()->post($uri, array( |
|
172 | 6 | 'channel' => $channel, |
|
173 | 6 | )); |
|
174 | 6 | } catch (Pest_BadRequest $e) { // Channel not found |
|
175 | 2 | throw new NotFoundException($e); |
|
176 | 2 | } catch (Pest_NotFound $e) { // Bridge not found |
|
177 | 2 | throw new NotFoundException($e); |
|
178 | } catch (Pest_Conflict $e) { // Bridge not in Stasis application |
||
179 | throw new ConflictException($e); |
||
180 | } catch (Pest_InvalidRecord $e) { // Channel not in Stasis application |
||
181 | throw new UnprocessableEntityException($e); |
||
182 | } |
||
183 | 2 | } |
|
184 | |||
185 | public function getType() |
||
189 | } |
||
190 |
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.