8000 minor #12190 [Routing] fix type inconsistencies in RequestContext (To… · symfony/symfony@4dad67e · GitHub
[go: up one dir, main page]

Skip to content

Commit 4dad67e

Browse files
committed
minor #12190 [Routing] fix type inconsistencies in RequestContext (Tobion)
This PR was merged into the 2.3 branch. Discussion ---------- [Routing] fix type inconsistencies in RequestContext | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Fixed inconsistencies in RequestContext: - [x] port setters and getters inconsistent with constructor (string vs int) - [x] host should be lowercased for easier case-insensitivity, see #9072 - [x] fix setQueryString with null - [x] fix return phpdoc typehint Commits ------- 676c4a0 [Routing] add tests for RequestContext f61607f [Routing] fix inconsistencies in RequestContext
2 parents a45c187 + 676c4a0 commit 4dad67e

File tree

2 files changed

+73
-24
lines changed

2 files changed

+73
-24
lines changed

src/Symfony/Component/Routing/RequestContext.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Holds information about the current request.
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
20+
* @author Tobias Schultze <http://tobion.de>
2021
*
2122
* @api
2223
*/
@@ -52,16 +53,21 @@ class RequestContext
5253
*/
5354
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
5455
{
55-
$this->baseUrl = $baseUrl;
56-
$this->method = strtoupper($method);
57-
$this->host = $host;
58-
$this->scheme = strtolower($scheme);
59-
$this->httpPort = $httpPort;
60-
$this->httpsPort = $httpsPort;
61-
$this->pathInfo = $path;
62-
$this->queryString = $queryString;
56+
$this->setBaseUrl($baseUrl);
57+
$this->setMethod($method);
58+
$this->setHost($host);
59+
$this->setScheme($scheme);
60+
$this->setHttpPort($httpPort);
61+
$this->setHttpsPort($httpsPort);
62+
$this->setPathInfo($path);
63+
$this->setQueryString($queryString);
6364
}
6465

66+
/**
67+
* Updates the RequestContext information based on a HttpFoundation Request.
68+
*
69+
* @param Request $request A Request instance
70+
*/
6571
public function fromRequest(Request $request)
6672
{
6773
$this->setBaseUrl($request->getBaseUrl());
@@ -71,7 +77,7 @@ public function fromRequest(Request $request)
7177
$this->setScheme($request->getScheme());
7278
$this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
7379
$this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
74-
$this->setQueryString($request->server->get('QUERY_STRING'));
80+
$this->setQueryString($request->server->get('QUERY_STRING', ''));
7581
}
7682

7783
/**
@@ -143,6 +149,8 @@ public function setMethod($method)
143149
/**
144150
* Gets the HTTP host.
145151
*
152+
* The host is always lowercased because it must be treated case-insensitive.
153+
*
146154
* @return string The HTTP host
147155
*/
148156
public function getHost()
@@ -159,7 +167,7 @@ public function getHost()
159167
*/
160168
public function setHost($host)
161169
{
162-
$this->host = $host;
170+
$this->host = strtolower($host);
163171
}
164172

165173
/**
@@ -187,7 +195,7 @@ public function setScheme($scheme)
187195
/**
188196
* Gets the HTTP port.
189197
*
190-
* @return string The HTTP port
198+
* @return int The HTTP port
191199
*/
192200
public function getHttpPort()
193201
{
@@ -197,19 +205,19 @@ public function getHttpPort()
197205
/**
198206
* Sets the HTTP port.
199207
*
200-
* @param string $httpPort The HTTP port
208+
* @param int $httpPort The HTTP port
201209
*
202210
* @api
203211
*/
204212
public function setHttpPort($httpPort)
205213
{
206-
$this->httpPort = $httpPort;
214+
$this->httpPort = (int) $httpPort;
207215
}
208216

209217
/**
210218
* Gets the HTTPS port.
211219
*
212-
* @return string The HTTPS port
220+
* @return int The HTTPS port
213221
*/
214222
public function getHttpsPort()
215223
{
@@ -219,19 +227,19 @@ public function getHttpsPort()
219227
/**
220228
* Sets the HTTPS port.
221229
*
222-
* @param string $httpsPort The HTTPS port
230+
* @param int $httpsPort The HTTPS port
223231
*
224232
* @api
225233
*/
226234
public function setHttpsPort($httpsPort)
227235
{
228-
$this->httpsPort = $httpsPort;
236+
$this->httpsPort = (int) $httpsPort;
229237
}
230238

231239
/**
232240
* Gets the query string.
233241
*
234-
* @return string The query string
242+
* @return string The query string without the "?"
235243
*/
236244
public function getQueryString()
237245
{
@@ -241,13 +249,14 @@ public function getQueryString()
241249
/**
242250
* Sets the query string.
243251
*
244-
* @param string $queryString The query string
252+
* @param string $queryString The query string (after "?")
245253
*
246254
* @api
247255
*/
248256
public function setQueryString($queryString)
249257
{
250-
$this->queryString = $queryString;
258+
// string cast to be fault-tolerant, accepting null
259+
F438 $this->queryString = (string) $queryString;
251260
}
252261

253262
/**
@@ -263,11 +272,9 @@ public function getParameters()
263272
/**
264273
* Sets the parameters.
265274
*
266-
* This method implements a fluent interface.
267-
*
268275
* @param array $parameters The parameters
269276
*
270-
* @return Route The current Route instance
277+
* @return RequestContext The current instance, implementing a fluent interface
271278
*/
272279
public function setParameters(array $parameters)
273280
{
@@ -281,7 +288,7 @@ public function setParameters(array $parameters)
281288
*
282289
* @param string $name A parameter name
283290
*
284-
* @return mixed The parameter value
291+
* @return mixed The parameter value or null if nonexistent
285292
*/
286293
public function getParameter($name)
287294
{
@@ -293,7 +300,7 @@ public function getParameter($name)
293300
*
294301
* @param string $name A parameter name
295302
*
296-
* @return bool true if the parameter value is set, false otherwise
303+
* @return bool True if the parameter value is set, false otherwise
297304
*/
298305
public function hasParameter($name)
299306
{

src/Symfony/Component/Routing/Tests/RequestContextTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,46 @@ public function testSetParameter()
9898

9999
$this->assertEquals('bar', $requestContext->getParameter('foo'));
100100
}
101+
102+
public function testMethod()
103+
{
104+
$requestContext = new RequestContext();
105+
$requestContext->setMethod('post');
106+
107+
$this->assertSame('POST', $requestContext->getMethod());
108+
}
109+
110+
public function testScheme()
111+
{
112+
$requestContext = new RequestContext();
113+
$requestContext->setScheme('HTTPS');
114+
115+
$this->assertSame('https', $requestContext->getScheme());
116+
}
117+
118+
public function testHost()
119+
{
120+
$requestContext = new RequestContext();
121+
$requestContext->setHost('eXampLe.com');
122+
123+
$this->assertSame('example.com', $requestContext->getHost());
124+
}
125+
126+
public function testQueryString()
127+
{
128+
$requestContext = new RequestContext();
129+
$requestContext->setQueryString(null);
130+
131+
$this->assertSame('', $requestContext->getQueryString());
132+
}
133+
134+
public function testPort()
135+
{
136+
$requestContext = new RequestContext();
137+
$requestContext->setHttpPort('123');
138+
$requestContext->setHttpsPort('456');
139+
140+
$this->assertSame(123, $requestContext->getHttpPort());
141+
$this->assertSame(456, $requestContext->getHttpsPort());
142+
}
101143
}

0 commit comments

Comments
 (0)
0