1 | <?php |
||
11 | abstract class Controller |
||
12 | { |
||
13 | /** |
||
14 | * Server request |
||
15 | * @var ServerRequestInterface |
||
16 | **/ |
||
17 | protected $request = null; |
||
18 | |||
19 | /** |
||
20 | * Response |
||
21 | * @var ResponseInterface |
||
22 | **/ |
||
23 | protected $response = null; |
||
24 | |||
25 | /** |
||
26 | * Run the controller |
||
27 | * |
||
28 | * @return ResponseInterface |
||
29 | */ |
||
30 | abstract public function run(); |
||
31 | |||
32 | /** |
||
33 | * Get request, set for controller |
||
34 | * |
||
35 | * @return ServerRequestInterface |
||
36 | */ |
||
37 | 1 | public function getRequest() |
|
41 | |||
42 | /** |
||
43 | * Get response. set for controller |
||
44 | * |
||
45 | * @return ResponseInterface |
||
46 | */ |
||
47 | 16 | public function getResponse() |
|
51 | |||
52 | /** |
||
53 | * Run the controller as function |
||
54 | * |
||
55 | * @param ServerRequestInterface $request |
||
56 | * @param ResponseInterface $response |
||
57 | * @return ResponseInterface |
||
58 | */ |
||
59 | 14 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response) |
|
66 | |||
67 | /** |
||
68 | * Check if response is 2xx succesful, or empty |
||
69 | * |
||
70 | * @return boolean |
||
71 | */ |
||
72 | 14 | public function isSuccessful() |
|
78 | |||
79 | /** |
||
80 | * Check if response is a 3xx redirect |
||
81 | * |
||
82 | * @return boolean |
||
83 | */ |
||
84 | 14 | public function isRedirection() |
|
90 | |||
91 | /** |
||
92 | * Check if response is a 4xx client error |
||
93 | * |
||
94 | * @return boolean |
||
95 | */ |
||
96 | 14 | public function isClientError() |
|
102 | |||
103 | /** |
||
104 | * Check if response is a 5xx redirect |
||
105 | * |
||
106 | * @return boolean |
||
107 | */ |
||
108 | 14 | public function isServerError() |
|
112 | |||
113 | /** |
||
114 | * Check if response is 4xx or 5xx error |
||
115 | * |
||
116 | * @return boolean |
||
117 | */ |
||
118 | 13 | public function isError() |
|
122 | |||
123 | /** |
||
124 | * Encode data to send to client |
||
125 | * |
||
126 | * @param mixed $data |
||
127 | * @return string |
||
128 | */ |
||
129 | 13 | public function encodeData($data) |
|
148 | |||
149 | /** |
||
150 | * Encode data as xml |
||
151 | * |
||
152 | * @param \SimpleXMLElement $data |
||
153 | * @return string |
||
154 | */ |
||
155 | 2 | protected function encodeDataAsXml(\SimpleXMLElement $data) |
|
159 | |||
160 | /** |
||
161 | * Encode data as json |
||
162 | * |
||
163 | * @param mixed |
||
164 | * @return string |
||
165 | */ |
||
166 | 5 | protected function encodeDataAsJson($data) |
|
174 | |||
175 | /** |
||
176 | * Check if we should respond with jsonp |
||
177 | * |
||
178 | * @return boolean |
||
179 | */ |
||
180 | 5 | protected function isJsonp() |
|
186 | |||
187 | /** |
||
188 | * Get status code of response |
||
189 | * |
||
190 | * @return int |
||
191 | */ |
||
192 | 14 | protected function getResponseStatusCode() |
|
198 | } |
||
199 | |||
200 |
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: