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 | * Send a message. |
||
60 | * |
||
61 | * @param string|int $to |
||
62 | * @param array $message |
||
63 | * @return \Psr\Http\Message\ResponseInterface |
||
64 | */ |
||
65 | public function sendMessage($to, $message) |
||
74 | |||
75 | /** |
||
76 | * Share a file. |
||
77 | * |
||
78 | * @param string|int $to |
||
79 | * @param array $file |
||
80 | * @return \Psr\Http\Message\ResponseInterface |
||
81 | */ |
||
82 | public function shareFile($to, $file) |
||
110 | |||
111 | /** |
||
112 | * Make a simple post request. |
||
113 | * |
||
114 | * @param string $url |
||
115 | * @param array $options |
||
116 | * @return \Psr\Http\Message\ResponseInterface |
||
117 | */ |
||
118 | protected function post($url, $options) |
||
122 | |||
123 | /** |
||
124 | * Make a multipart/related request. |
||
125 | * Unfortunately Guzzle doesn't support multipart/related requests out of the box. |
||
126 | * |
||
127 | * @param $url |
||
128 | * @param $options |
||
129 | * @return \Psr\Http\Message\ResponseInterface |
||
130 | */ |
||
131 | protected function postMultipartRelated($url, $options) |
||
147 | |||
148 | /** |
||
149 | * Get common request headers. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | protected function getHeaders() |
||
159 | } |
||
160 |
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.