8000 [Routing] Add type-hints RequestContext and Route classes. · symfony/symfony@bb0725f · GitHub
[go: up one dir, main page]

Skip to content

Commit bb0725f

Browse files
committed
[Routing] Add type-hints RequestContext and Route classes.
1 parent cf8cfeb commit bb0725f

File tree

5 files changed

+36
-108
lines changed

5 files changed

+36
-108
lines changed

src/Symfony/Component/Routing/RequestContext.php

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ public function getBaseUrl()
7777
/**
7878
* Sets the base URL.
7979
*
80-
* @param string $baseUrl The base URL
81-
*
8280
* @return $this
8381
*/
84-
public function setBaseUrl($baseUrl)
82+
public function setBase 8000 Url(string $baseUrl)
8583
{
8684
$this->baseUrl = $baseUrl;
8785

@@ -101,11 +99,9 @@ public function getPathInfo()
10199
/**
102100
* Sets the path info.
103101
*
104-
* @param string $pathInfo The path info
105-
*
106102
* @return $this
107103
*/
108-
public function setPathInfo($pathInfo)
104+
public function setPathInfo(string $pathInfo)
109105
{
110106
$this->pathInfo = $pathInfo;
111107

@@ -127,11 +123,9 @@ public function getMethod()
127123
/**
128124
* Sets the HTTP method.
129125
*
130-
* @param string $method The HTTP method
131-
*
132126
* @return $this
133127
*/
134-
public function setMethod($method)
128+
public function setMethod(string $method)
135129
{
136130
$this->method = strtoupper($method);
137131

@@ -153,11 +147,9 @@ public function getHost()
153147
/**
154148
* Sets the HTTP host.
155149
*
156-
* @param string $host The HTTP host
157-
*
158150
* @return $this
159151
*/
160-
public function setHost($host)
152+
public function setHost(string $host)
161153
{
162154
$this->host = strtolower($host);
163155

@@ -177,11 +169,9 @@ public function getScheme()
177169
/**
178170
* Sets the HTTP scheme.
179171
*
180-
* @param string $scheme The HTTP scheme
181-
*
182172
* @return $this
183173
*/
184-
public function setScheme($scheme)
174+
public function setScheme(string $scheme)
185175
{
186176
$this->scheme = strtolower($scheme);
187177

@@ -201,13 +191,11 @@ public function getHttpPort()
201191
/**
202192
* Sets the HTTP port.
203193
*
204-
* @param int $httpPort The HTTP port
205-
*
206194
* @return $this
207195
*/
208-
public function setHttpPort($httpPort)
196+
public function setHttpPort(int $httpPort)
209197
{
210-
$this->httpPort = (int) $httpPort;
198+
$this->httpPort = $httpPort;
211199

212200
return $this;
213201
}
@@ -225,13 +213,11 @@ public function getHttpsPort()
225213
/**
226214
* Sets the HTTPS port.
227215
*
228-
* @param int $httpsPort The HTTPS port
229-
*
230216
* @return $this
231217
*/
232-
public function setHttpsPort($httpsPort)
218+
public function setHttpsPort(int $httpsPort)
233219
{
234-
$this->httpsPort = (int) $httpsPort;
220+
$this->httpsPort = $httpsPort;
235221

236222
return $this;
237223
}
@@ -249,11 +235,9 @@ public function getQueryString()
249235
/**
250236
* Sets the query string.
251237
*
252-
* @param string $queryString The query string (after "?")
253-
*
254238
* @return $this
255239
*/
256-
public function setQueryString($queryString)
240+
public function setQueryString(?string $queryString)
257241
{
258242
// string cast to be fault-tolerant, accepting null
259243
$this->queryString = (string) $queryString;
@@ -288,36 +272,31 @@ public function setParameters(array $parameters)
288272
/**
289273
* Gets a parameter value.
290274
*
291-
* @param string $name A parameter name
292-
*
293275
* @return mixed The parameter value or null if nonexistent
294276
*/
295-
public function getParameter($name)
277+
public function getParameter(string $name)
296278
{
297279
return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
298280
}
299281

300282
/**
301283
* Checks if a parameter value is set for the given parameter.
302284
*
303-
* @param string $name A parameter name
304-
*
305285
* @return bool True if the parameter value is set, false otherwise
306286
*/
307-
public function hasParameter($name)
287+
public function hasParameter(string $name)
308288
{
309289
return \array_key_exists($name, $this->parameters);
310290
}
311291

312292
/**
313293
* Sets a parameter value.
314294
*
315-
* @param string $name A parameter name
316-
* @param mixed $parameter The parameter value
295+
* @param mixed $parameter The parameter value
317296
*
318297
* @return $this
319298
*/
320-
public function setParameter($name, $parameter)
299+
public function setParameter(string $name, $parameter)
321300
{
322301
$this->parameters[$name] = $parameter;
323302

src/Symfony/Component/Routing/Route.php

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ public function getPath()
126126
*
127127
* This method implements a fluent interface.
128128
*
129-
* @param string $pattern The path pattern
130-
*
131129
* @return $this
132130
*/
133-
public function setPath($pattern)
131+
public function setPath(string $pattern)
134132
{
135133
if (false !== strpbrk($pattern, '?<')) {
136134
$pattern = preg_replace_callback('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
@@ -168,11 +166,9 @@ public function getHost()
168166
*
169167
* This method implements a fluent interface.
170168
*
171-
* @param string $pattern The host pattern
172-
*
173169
* @return $this
174170
*/
175-
public function setHost($pattern)
171+
public function setHost(?string $pattern)
176172
{
177173
$this->host = (string) $pattern;
178174
$this->compiled = null;
@@ -212,11 +208,9 @@ public function setSchemes($schemes)
212208
/**
213209
* Checks if a scheme requirement has been set.
214210
*
215-
* @param string $scheme
216-
*
217211
* @return bool true if the scheme requirement exists, otherwise false
218212
*/
219-
public function hasScheme($scheme)
213+
public function hasScheme(string $scheme)
220214
{
221215
return \in_array(strtolower($scheme), $this->schemes, true);
222216
}
@@ -302,12 +296,11 @@ public function addOptions(array $options)
302296
*
303297
* This method implements a fluent interface.
304298
*
305-
* @param string $name An option name
306-
* @param mixed $value The option value
299+
* @param mixed $value The option value
307300
*
308301
* @return $this
309302
*/
310-
public function setOption($name, $value)
303+
public function setOption(string $name, $value)
311304
{
312305
$this->options[$name] = $value;
313306
$this->compiled = null;
@@ -318,23 +311,19 @@ public function setOption($name, $value)
318311
/**
319312
* Get an option value.
320313
*
321-
* @param string $name An option name
322-
*
323314
* @return mixed The option value or null when not given
324315
*/
325-
public function getOption($name)
316+
public function getOption(string $name)
326317
{
327318
return isset($this->options[$name]) ? $this->options[$name] : null;
328319
}
329320

330321
/**
331322
* Checks if an option has been set.
332323
*
333-
* @param string $name An option name
334-
*
335324
* @return bool true if the option is set, false otherwise
336325
*/
337-
public function hasOption($name)
326+
public function hasOption(string $name)
338327
{
339328
return \array_key_exists($name, $this->options);
340329
}
@@ -387,36 +376,31 @@ public function addDefaults(array $defaults)
387376
/**
388377
* Gets a default value.
389378
*
390-
* @param string $name A variable name
391-
*
392379
* @return mixed The default value or null when not given
393380
*/
394-
public function getDefault($name)
381+
public function getDefault(string $name)
395382
{
396383
return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
397384
}
398385

399386
/**
400387
* Checks if a default value is set for the given variable.
401388
*
402-
* @param string $name A variable name
403-
*
404389
* @return bool true if the default value is set, false otherwise
405390
*/
406-
public function hasDefault($name)
391+
public function hasDefault(string $name)
407392
{
408393
return \array_key_exists($name, $this->defaults);
409394
}
410395

411396
/**
412397
* Sets a default value.
413398
*
414-
* @param string $name A variable name
415-
* @param mixed $default The default value
399+
* @param mixed $default The default value
416400
*
417401
* @return $this
418402
*/
419-
public function setDefault($name, $default)
403+
public function setDefault(string $name, $default)
420404
{
421405
$this->defaults[$name] = $default;
422406
$this->compiled = null;
@@ -472,36 +456,29 @@ public function addRequirements(array $requirements)
472456
/**
473457
* Returns the requirement for the given key.
474458
*
475-
* @param string $key The key
476-
*
477459
* @return string|null The regex or null when not given
478460
*/
479-
public function getRequirement($key)
461+
public function getRequirement(string $key)
480462
{
481463
return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
482464
}
483465

484466
/**
485467
* Checks if a requirement is set for the given key.
486468
*
487-
* @param string $key A variable name
488-
*
489469
* @return bool true if a requirement is specified, false otherwise
490470
*/
491-
public function hasRequirement($key)
471+
public function hasRequirement(string $key)
492472
{
493473
return \array_key_exists($key, $this->requirements);
494474
}
495475

496476
/**
497477
* Sets a requirement for the given key.
498478
*
499-
* @param string $key The key
500-
* @param string $regex The regex
501-
*
502479
* @return $this
503480
*/
504-
public function setRequirement($key, $regex)
481+
public function setRequirement(string $key, string $regex)
505482
{
506483
$this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
507484
$this->compiled = null;
@@ -524,11 +501,9 @@ public function getCondition()
524501
*
525502
* This method implements a fluent interface.
526503
*
527-
* @param string $condition The condition
528-
*
529504
* @return $this
530505
*/
531-
public function setCondition($condition)
506+
public function setCondition(?string $condition)
532507
{
533508
$this->condition = (string) $condition;
534509
$this->compiled = null;
@@ -557,12 +532,8 @@ public function compile()
557532
return $this->compiled = $class::compile($this);
558533
}
559534

560-
private function sanitizeRequirement($key, $regex)
535+
private function sanitizeRequirement(string $key, string $regex)
561536
{
562-
if (!\is_string($regex)) {
563-
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
564-
}
565-
566537
if ('' !== $regex && '^' === $regex[0]) {
567538
$regex = (string) substr($regex, 1); // returns false for a single character
568539
}

0 commit comments

Comments
 (0)
0