Conditions | 26 |
Paths | 173 |
Total Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
134 | public function postMessage($channel, $text, $parse = "none", $link_names = 1, $attachments = NULL, |
||
135 | $unfurl_links = TRUE, $unfurl_media = FALSE, $username = NULL, $as_user = TRUE, $icon_url = NULL, |
||
136 | $icon_emoji = NULL, $thread_ts = NULL, $reply_broadcast = TRUE) { |
||
137 | |||
138 | // Check if the type of the variables is valid. |
||
139 | if (!is_string($channel)) { |
||
140 | throw new InvalidArgumentException("The type of the channel variable is not valid."); |
||
141 | } |
||
142 | if (!is_string($text)) { |
||
143 | throw new InvalidArgumentException("The type of the text variable is not valid."); |
||
144 | } |
||
145 | if (!is_string($parse)) { |
||
146 | throw new InvalidArgumentException("The type of the parse variable is not valid."); |
||
147 | } |
||
148 | if (!is_integer($link_names)) { |
||
149 | throw new InvalidArgumentException("The type of the link_names variable is not valid."); |
||
150 | } |
||
151 | if (($attachments != NULL) && !is_string($attachments)) { |
||
152 | throw new InvalidArgumentException("The type of the attachments variable is not valid."); |
||
153 | } |
||
154 | if (!is_bool($unfurl_links)) { |
||
155 | throw new InvalidArgumentException("The type of the unfurl_links variable is not valid."); |
||
156 | } |
||
157 | if (!is_bool($unfurl_media)) { |
||
158 | throw new InvalidArgumentException("The type of the unfurl_media variable is not valid."); |
||
159 | } |
||
160 | if (($username != NULL) && !is_string($username)) { |
||
161 | throw new InvalidArgumentException("The type of the username variable is not valid."); |
||
162 | } |
||
163 | if (!is_bool($as_user)) { |
||
164 | throw new InvalidArgumentException("The type of the as_user variable is not valid."); |
||
165 | } |
||
166 | if (($icon_url != NULL) && !is_string($icon_url)) { |
||
167 | throw new InvalidArgumentException("The type of the icon_url variable is not valid."); |
||
168 | } |
||
169 | if (($icon_emoji != NULL) && !is_string($icon_emoji)) { |
||
170 | throw new InvalidArgumentException("The type of the icon_emoji variable is not valid."); |
||
171 | } |
||
172 | if (($thread_ts != NULL) && !is_float($thread_ts)) { |
||
173 | throw new InvalidArgumentException("The type of the thread_ts variable is not valid."); |
||
174 | } |
||
175 | if (!is_bool($reply_broadcast)) { |
||
176 | throw new InvalidArgumentException("The type of the reply_broadcast variable is not valid."); |
||
177 | } |
||
178 | |||
179 | // Set the arguments of the request |
||
180 | $arguments = array( |
||
181 | "channel" => $channel, |
||
182 | "text" => $text, |
||
183 | "parse" => $parse, |
||
184 | "link_names" => $link_names, |
||
185 | "unfurl_links" => $unfurl_links, |
||
186 | "unfurl_media" => $unfurl_media, |
||
187 | "as_user" => $as_user, |
||
188 | "reply_broadcast" => $reply_broadcast |
||
189 | ); |
||
190 | |||
191 | if($attachments != NULL) { |
||
192 | $arguments["attachments"] = $attachments; |
||
193 | } |
||
194 | if($username != NULL) { |
||
195 | $arguments["username"] = $username; |
||
196 | } |
||
197 | if($icon_url != NULL) { |
||
198 | $arguments["icon_url"] = $icon_url; |
||
199 | } |
||
200 | if($icon_emoji != NULL) { |
||
201 | $arguments["icon_emoji"] = $icon_emoji; |
||
202 | } |
||
203 | if($thread_ts != NULL) { |
||
204 | $arguments["thread_ts"] = (string)$thread_ts; |
||
205 | } |
||
206 | |||
207 | $this->setUrl("chat.postMessage", $arguments); |
||
208 | |||
209 | // Send the request |
||
210 | try { |
||
211 | $client = new \GuzzleHttp\Client(); |
||
212 | $json_response = $client->request('GET', $this->getUrl(), []); |
||
213 | $response = json_decode( $json_response->getBody() ); |
||
214 | } |
||
215 | catch (RequestException $e) { |
||
216 | throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
||
217 | } |
||
218 | |||
219 | if($response->{'ok'} === FALSE) { |
||
220 | throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
||
221 | } |
||
222 | |||
223 | return $json_response->getBody(); |
||
224 | } |
||
225 | |||
298 |
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.