1 | <?php |
||
18 | abstract class MoipResource implements JsonSerializable |
||
19 | { |
||
20 | /** |
||
21 | * Version of API. |
||
22 | * |
||
23 | * @const string |
||
24 | */ |
||
25 | const VERSION = 'v2'; |
||
26 | |||
27 | /** |
||
28 | * @var \Moip\Moip |
||
29 | */ |
||
30 | protected $moip; |
||
31 | |||
32 | /** |
||
33 | * @var \stdClass |
||
34 | */ |
||
35 | protected $data; |
||
36 | |||
37 | /** |
||
38 | * Initialize a new instance. |
||
39 | */ |
||
40 | abstract protected function initialize(); |
||
41 | |||
42 | /** |
||
43 | * Mount information of a determined object. |
||
44 | * |
||
45 | * @param \stdClass $response |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | abstract protected function populate(stdClass $response); |
||
50 | |||
51 | /** |
||
52 | * Create a new instance. |
||
53 | * |
||
54 | * @param \Moip\Moip $moip |
||
55 | */ |
||
56 | public function __construct(Moip $moip) |
||
62 | |||
63 | /** |
||
64 | * Get a key of an object if it exists. |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @param \stdClass|null $data |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | protected function getIfSet($key, stdClass $data = null) |
||
81 | |||
82 | /** |
||
83 | * @return \Moip\Helper\Links |
||
84 | */ |
||
85 | public function getLinks() |
||
93 | |||
94 | /** |
||
95 | * @param $key |
||
96 | * @param $fmt |
||
97 | * @param stdClass|null $data |
||
98 | * |
||
99 | * @return bool|\DateTime|null |
||
100 | */ |
||
101 | protected function getIfSetDateFmt($key, $fmt, stdClass $data = null) |
||
110 | |||
111 | /** |
||
112 | * Get a key, representing a date (Y-m-d), of an object if it exists. |
||
113 | * |
||
114 | * @param string $key |
||
115 | * @param stdClass|null $data |
||
116 | * |
||
117 | * @return \DateTime|null |
||
118 | */ |
||
119 | protected function getIfSetDate($key, stdClass $data = null) |
||
123 | |||
124 | /** |
||
125 | * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
||
126 | * |
||
127 | * @param string $key |
||
128 | * @param stdClass|null $data |
||
129 | * |
||
130 | * @return \DateTime|null |
||
131 | */ |
||
132 | protected function getIfSetDateTime($key, stdClass $data = null) |
||
142 | |||
143 | /** |
||
144 | * Specify data which should be serialized to JSON. |
||
145 | * |
||
146 | * @return \stdClass |
||
147 | */ |
||
148 | public function jsonSerialize() |
||
152 | |||
153 | /** |
||
154 | * Generate URL to request. |
||
155 | * |
||
156 | * @param $action |
||
157 | * @param $id |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function generatePath($action, $id = null) |
||
169 | |||
170 | /** |
||
171 | * Generate URL to request a get list. |
||
172 | * |
||
173 | * @param Pagination $pagination |
||
174 | * @param Filters $filters |
||
175 | * @param string $qParam Query a specific value. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function generateListPath(Pagination $pagination = null, Filters $filters = null, $qParam = '') |
||
203 | |||
204 | /** |
||
205 | * Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a |
||
206 | * empty stdClass. |
||
207 | * |
||
208 | * @param string $path |
||
209 | * @param string $method |
||
210 | * @param mixed|null $payload |
||
211 | * |
||
212 | * @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent. |
||
213 | * @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key. |
||
214 | * @throws Exceptions\UnexpectedException if the API returns a 500 http status code or something unexpected happens (ie.: Network error). |
||
215 | * |
||
216 | * @return stdClass |
||
217 | */ |
||
218 | protected function httpRequest($path, $method, $payload = null) |
||
250 | |||
251 | /** |
||
252 | * Find by path. |
||
253 | * |
||
254 | * @param string $path |
||
255 | * |
||
256 | * @return stdClass |
||
257 | */ |
||
258 | public function getByPath($path) |
||
264 | |||
265 | /** |
||
266 | * Create a new item in Moip. |
||
267 | * |
||
268 | * @param string $path |
||
269 | * |
||
270 | * @return stdClass |
||
271 | */ |
||
272 | public function createResource($path) |
||
278 | |||
279 | /** |
||
280 | * Delete a new item in Moip. |
||
281 | * |
||
282 | * @param $path |
||
283 | * |
||
284 | * @return mixed |
||
285 | */ |
||
286 | public function deleteByPath($path) |
||
290 | } |
||
291 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: