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

Skip to content

Commit cece78c

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

File tree

6 files changed

+46
-97
lines changed

6 files changed

+46
-97
lines changed

src/Symfony/Component/Routing/RequestContext.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getBaseUrl()
8181
*
8282
* @return $this
8383
*/
84-
public function setBaseUrl($baseUrl)
84+
public function setBaseUrl(string $baseUrl)
8585
{
8686
$this->baseUrl = $baseUrl;
8787

@@ -105,7 +105,7 @@ public function getPathInfo()
105105
*
106106
* @return $this
107107
*/
108-
public function setPathInfo($pathInfo)
108+
public function setPathInfo(string $pathInfo)
109109
{
110110
$this->pathInfo = $pathInfo;
111111

@@ -131,7 +131,7 @@ public function getMethod()
131131
*
132132
* @return $this
133133
*/
134-
public function setMethod($method)
134+
public function setMethod(string $method)
135135
{
136136
$this->method = strtoupper($method);
137137

@@ -157,7 +157,7 @@ public function getHost()
157157
*
158158
* @return $this
159159
*/
160-
public function setHost($host)
160+
public function setHost(string $host)
161161
{
162162
$this->host = strtolower($host);
163163

@@ -181,7 +181,7 @@ public function getScheme()
181181
*
182182
* @return $this
183183
*/
184-
public function setScheme($scheme)
184+
public function setScheme(string $scheme)
185185
{
186186
$this->scheme = strtolower($scheme);
187187

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
}

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function count()
7272
* @param string $name The route name
7373
* @param Route $route A Route instance
7474
*/
75-
public function add($name, Route $route)
75+
public function add(string $name, Route $route)
7676
{
7777
unset($this->routes[$name]);
7878

@@ -96,7 +96,7 @@ public function all()
9696
*
9797
* @return Route|null A Route instance or null when not found
9898
*/
99-
public function get($name)
99+
public function get(string $name)
100100
{
101101
return isset($this->routes[$name]) ? $this->routes[$name] : null;
102102
}
@@ -134,11 +134,11 @@ public function addCollection(self $collection)
134134
/**
135135
* Adds a prefix to the path of all child routes.
136136
*
137-
* @param string $prefix An optional prefix to add before each pattern of the route collection
138-
* @param array $defaults An array of default values
139-
* @param array $requirements An array of requirements
137+
* @param string|null $prefix An optional prefix to add before each pattern of the route collection
138+
* @param array $defaults An array of default values
139+
* @param array $requirements An array of requirements
140140
*/
141-
public function addPrefix($prefix, array $defaults = [], array $requirements = [])
141+
public function addPrefix(?string $prefix, array $defaults = [], array $requirements = [])
142142
{
143143
$prefix = trim(trim($prefix), '/');
144144

@@ -173,11 +173,11 @@ public function addNamePrefix(string $prefix)
173173
/**
174174
* Sets the host pattern on all routes.
175175
*
176-
* @param string $pattern The pattern
177-
* @param array $defaults An array of default values
178-
* @param array $requirements An array of requirements
176+
* @param string|null $pattern The pattern
177+
* @param array $defaults An array of default values
178+
* @param array $requirements An array of requirements
179179
*/
180-
public function setHost($pattern, array $defaults = [], array $requirements = [])
180+
public function setHost(?string $pattern, array $defaults = [], array $requirements = [])
181181
{
182182
foreach ($this->routes as $route) {
183183
$route->setHost($pattern);
@@ -191,9 +191,9 @@ public function setHost($pattern, array $defaults = [], array $requirements = []
191191
*
192192
* Existing conditions will be overridden.
193193
*
194-
* @param string $condition The condition
194+
* @param string|null $condition The condition
195195
*/
196-
public function setCondition($condition)
196+
public function setCondition(?string $condition)
197197
{
198198
foreach ($this->routes as $route) {
199199
$route->setCondition($condition);

0 commit comments

Comments
 (0)
0