1 | <?php |
||
23 | class SlackBot |
||
24 | { |
||
25 | const QUOTE_DEFAULT = 0; |
||
26 | const QUOTE_DANGER = 1; |
||
27 | const QUOTE_SUCCESS = 2; |
||
28 | const QUOTE_WARNING = 3; |
||
29 | const QUOTE_INFO = 4; |
||
30 | |||
31 | /** @var array */ |
||
32 | private $config; |
||
33 | |||
34 | /** @var GuzzleClient */ |
||
35 | private $guzzleClient; |
||
36 | |||
37 | /** @var SlackMessageValidator */ |
||
38 | private $validator; |
||
39 | |||
40 | /** |
||
41 | * SlackBot constructor. |
||
42 | * |
||
43 | * @param array $config |
||
44 | * @param SlackMessageValidator $validator |
||
45 | */ |
||
46 | public function __construct(array $config, SlackMessageValidator $validator) |
||
52 | |||
53 | /** |
||
54 | * @return array |
||
55 | */ |
||
56 | public function getConfig(): array |
||
60 | |||
61 | /** |
||
62 | * @param array $config |
||
63 | */ |
||
64 | public function setConfig(array $config) |
||
68 | |||
69 | /** |
||
70 | * @param int $quoteType |
||
71 | * @return string |
||
72 | */ |
||
73 | public function quoteTypeColor(int $quoteType): string |
||
96 | |||
97 | /** |
||
98 | * @param SlackMessage $slackMessage |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function sendMessage(SlackMessage $slackMessage): bool |
||
105 | |||
106 | /** |
||
107 | * @param SlackMessage $slackMessage |
||
108 | * @return string |
||
109 | */ |
||
110 | private function buildPostBody(SlackMessage $slackMessage): string |
||
135 | |||
136 | /** |
||
137 | * @param string $postBody |
||
138 | * @return bool |
||
139 | */ |
||
140 | private function sendRequest(string $postBody): bool |
||
149 | } |
||
150 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.