1 | <?php |
||
11 | class HipChat |
||
12 | { |
||
13 | /** @var string */ |
||
14 | protected $token; |
||
15 | |||
16 | /** @var HttpClient */ |
||
17 | protected $http; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $url; |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $room; |
||
24 | |||
25 | /** |
||
26 | * @param HttpClient $http |
||
27 | * @param string $token |
||
28 | * @param string|null $url |
||
29 | */ |
||
30 | public function __construct(HttpClient $http, $url, $token, $room) |
||
37 | |||
38 | /** |
||
39 | * Returns default room id or name. |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | public function room() |
||
47 | |||
48 | /** |
||
49 | * Returns HipChat base url. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public function url() |
||
57 | |||
58 | /** |
||
59 | * Set the HipChat token to use. |
||
60 | * |
||
61 | * @param string |
||
62 | */ |
||
63 | public function token($token) |
||
67 | |||
68 | /** |
||
69 | * Send a message. |
||
70 | * |
||
71 | * @param string|int $to |
||
72 | * @param array $message |
||
73 | * @return \Psr\Http\Message\ResponseInterface |
||
74 | */ |
||
75 | public function sendMessage($to, $message) |
||
84 | |||
85 | /** |
||
86 | * Share a file. |
||
87 | * |
||
88 | * @param string|int $to |
||
89 | * @param array $file |
||
90 | * @return \Psr\Http\Message\ResponseInterface |
||
91 | */ |
||
92 | public function shareFile($to, $file) |
||
120 | |||
121 | /** |
||
122 | * Make a simple post request. |
||
123 | * |
||
124 | * @param string $url |
||
125 | * @param array $options |
||
126 | * @return \Psr\Http\Message\ResponseInterface |
||
127 | */ |
||
128 | protected function post($url, $options) |
||
132 | |||
133 | /** |
||
134 | * Make a multipart/related request. |
||
135 | * Unfortunately Guzzle doesn't support multipart/related requests out of the box. |
||
136 | * |
||
137 | * @param $url |
||
138 | * @param $options |
||
139 | * @return \Psr\Http\Message\ResponseInterface |
||
140 | */ |
||
141 | protected function postMultipartRelated($url, $options) |
||
157 | |||
158 | /** |
||
159 | * Get common request headers. |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | protected function getHeaders() |
||
169 | } |
||
170 |
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.