| Conditions | 7 |
| Paths | 17 |
| Total Lines | 34 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 12 | public function send($message, $username=null, $emoji=null, $channel=null) |
||
| 13 | { |
||
| 14 | if (! config('slack.incoming-webhook')) { |
||
| 15 | return; |
||
| 16 | } |
||
| 17 | |||
| 18 | $emoji = trim($emoji); |
||
| 19 | $channel = trim($channel); |
||
| 20 | $username = trim($username); |
||
| 21 | |||
| 22 | $payload['text'] = $message; |
||
|
|
|||
| 23 | $payload['username'] = empty($username) ? config('slack.default_username') : $username; |
||
| 24 | $payload['icon_emoji'] = empty($emoji) ? config('slack.default_emoji') : $emoji; |
||
| 25 | $payload['channel'] = $channel; |
||
| 26 | |||
| 27 | $headers = [ |
||
| 28 | 'Content-Type' => 'application/json', |
||
| 29 | ]; |
||
| 30 | |||
| 31 | $guzzleClient = new Client; |
||
| 32 | |||
| 33 | try { |
||
| 34 | $response = $guzzleClient->post(config('slack.incoming-webhook'), [ |
||
| 35 | 'headers' => $headers, |
||
| 36 | 'body' => json_encode($payload), |
||
| 37 | ]); |
||
| 38 | } catch (\Exception $e) { |
||
| 39 | Log::error($e->getMessage()); |
||
| 40 | } |
||
| 41 | |||
| 42 | if (!isset($response) || $response->getStatusCode() != 200) { |
||
| 43 | Log::error(self::DEFAULT_ERROR_MESSAGE); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.