1 | <?php declare(strict_types=1); |
||
20 | class Builder |
||
21 | { |
||
22 | /** |
||
23 | * The default domain to use for further link documentation. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $docDomain = 'http://docs.php-opencloud.com/en/latest/'; |
||
28 | |||
29 | /** |
||
30 | * The HTTP client required to validate the further links. |
||
31 | * |
||
32 | * @var ClientInterface |
||
33 | */ |
||
34 | private $client; |
||
35 | |||
36 | /** |
||
37 | * @param ClientInterface $client |
||
38 | */ |
||
39 | 2 | public function __construct(ClientInterface $client = null) |
|
43 | |||
44 | /** |
||
45 | * Internal method used when outputting headers in the error description. |
||
46 | * |
||
47 | * @param $name |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 4 | private function header(string $name): string |
|
55 | |||
56 | /** |
||
57 | * Before outputting custom links, it is validated to ensure that the user is not |
||
58 | * directed off to a broken link. If a 404 is detected, it is hidden. |
||
59 | * |
||
60 | * @param $link The proposed link |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | 2 | private function linkIsValid(string $link): bool |
|
74 | |||
75 | /** |
||
76 | 2 | * @param MessageInterface $message |
|
77 | * |
||
78 | 2 | * @codeCoverageIgnore |
|
79 | 2 | * @return string |
|
80 | 2 | */ |
|
81 | 2 | public function str(MessageInterface $message): string |
|
82 | 2 | { |
|
83 | 1 | if ($message instanceof RequestInterface) { |
|
84 | 1 | $msg = trim($message->getMethod() . ' ' |
|
85 | 2 | . $message->getRequestTarget()) |
|
86 | 2 | . ' HTTP/' . $message->getProtocolVersion(); |
|
87 | 2 | if (!$message->hasHeader('host')) { |
|
88 | 2 | $msg .= "\r\nHost: " . $message->getUri()->getHost(); |
|
89 | 2 | } |
|
90 | } elseif ($message instanceof ResponseInterface) { |
||
91 | 2 | $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' |
|
92 | 1 | . $message->getStatusCode() . ' ' |
|
93 | 2 | . $message->getReasonPhrase(); |
|
94 | } |
||
95 | 2 | ||
96 | 2 | foreach ($message->getHeaders() as $name => $values) { |
|
97 | 2 | $msg .= "\r\n{$name}: " . implode(', ', $values); |
|
|
|||
98 | } |
||
99 | 2 | ||
100 | if (ini_get('memory_limit') < 0 || $message->getBody()->getSize() < ini_get('memory_limit')) { |
||
101 | $msg .= "\r\n\r\n" . $message->getBody(); |
||
102 | } |
||
103 | |||
104 | return $msg; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Helper method responsible for constructing and returning {@see BadResponseError} exceptions. |
||
109 | * |
||
110 | 2 | * @param RequestInterface $request The faulty request |
|
111 | * @param ResponseInterface $response The error-filled response |
||
112 | 2 | * |
|
113 | * @return BadResponseError |
||
114 | 2 | */ |
|
115 | 2 | public function httpError(RequestInterface $request, ResponseInterface $response): BadResponseError |
|
116 | { |
||
117 | 2 | $message = $this->header('HTTP Error'); |
|
118 | 2 | ||
119 | $message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n", |
||
120 | 2 | $response->getStatusCode(), $response->getReasonPhrase()); |
|
121 | 2 | ||
122 | $message .= $this->header('Request'); |
||
123 | 2 | $message .= trim($this->str($request)) . PHP_EOL . PHP_EOL; |
|
124 | 2 | ||
125 | $message .= $this->header('Response'); |
||
126 | $message .= trim($this->str($response)) . PHP_EOL . PHP_EOL; |
||
127 | 2 | ||
128 | $message .= $this->header('Further information'); |
||
129 | 2 | $message .= $this->getStatusCodeMessage($response->getStatusCode()); |
|
130 | 2 | ||
131 | 2 | $message .= "Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging " |
|
132 | . "HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues."; |
||
133 | 2 | ||
134 | $e = new BadResponseError($message); |
||
135 | $e->setRequest($request); |
||
136 | 2 | $e->setResponse($response); |
|
137 | |||
138 | return $e; |
||
139 | 2 | } |
|
140 | 2 | ||
141 | 2 | private function getStatusCodeMessage(int $statusCode): string |
|
142 | 2 | { |
|
143 | 2 | $errors = [ |
|
144 | 400 => 'Please ensure that your input values are valid and well-formed. ', |
||
145 | 2 | 401 => 'Please ensure that your authentication credentials are valid. ', |
|
146 | 404 => "Please ensure that the resource you're trying to access actually exists. ", |
||
147 | 500 => 'Please try this operation again once you know the remote server is operational. ', |
||
148 | ]; |
||
149 | |||
150 | return isset($errors[$statusCode]) ? $errors[$statusCode] : ''; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Helper method responsible for constructing and returning {@see UserInputError} exceptions. |
||
155 | * |
||
156 | * @param string $expectedType The type that was expected from the user |
||
157 | 2 | * @param mixed $userValue The incorrect value the user actually provided |
|
158 | * @param string|null $furtherLink A link to further information if necessary (optional). |
||
159 | 2 | * |
|
160 | * @return UserInputError |
||
161 | 2 | */ |
|
162 | 2 | public function userInputError(string $expectedType, $userValue, string $furtherLink = null): UserInputError |
|
179 | } |
||
180 |
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: