1 | <?php |
||
11 | class HipChat |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $token; |
||
17 | |||
18 | /** |
||
19 | * @var HttpClient |
||
20 | */ |
||
21 | protected $http; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $url; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $room; |
||
32 | |||
33 | /** |
||
34 | * Create a new instance. |
||
35 | * |
||
36 | * @param HttpClient $http |
||
37 | * @param string $token |
||
38 | * @param string|null $url |
||
39 | */ |
||
40 | public function __construct(HttpClient $http, $url, $token, $room) |
||
47 | |||
48 | /** |
||
49 | * Returns default room id or name. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public function room() |
||
57 | |||
58 | /** |
||
59 | * Returns HipChat base url. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function url() |
||
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.