Skip to content

Commit

Permalink
minor #32181 [Routing] Add type-hints RequestContext and Route classe…
Browse files Browse the repository at this point in the history
…s. (derrabus)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[Routing] Add type-hints RequestContext and Route classes.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32179
| License       | MIT
| Doc PR        | N/A

This PR adds type-hints to `RequestContext` and the Route classes to give them a more php7-like feeling. Adding these type-hints on master should not be a breaking change because we require php 7.2 there, which allows downstream classes/interfaces to remove a type-hint from a method signature.

Commits
-------

614e824 [Routing] Add type-hints RequestContext and Route classes.
  • Loading branch information
Tobion committed Jun 28, 2019
2 parents cf8cfeb + 614e824 commit 7b3ef19
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 112 deletions.
49 changes: 14 additions & 35 deletions src/Symfony/Component/Routing/RequestContext.php
Expand Up @@ -77,11 +77,9 @@ public function getBaseUrl()
/**
* Sets the base URL.
*
* @param string $baseUrl The base URL
*
* @return $this
*/
public function setBaseUrl($baseUrl)
public function setBaseUrl(string $baseUrl)
{
$this->baseUrl = $baseUrl;

Expand All @@ -101,11 +99,9 @@ public function getPathInfo()
/**
* Sets the path info.
*
* @param string $pathInfo The path info
*
* @return $this
*/
public function setPathInfo($pathInfo)
public function setPathInfo(string $pathInfo)
{
$this->pathInfo = $pathInfo;

Expand All @@ -127,11 +123,9 @@ public function getMethod()
/**
* Sets the HTTP method.
*
* @param string $method The HTTP method
*
* @return $this
*/
public function setMethod($method)
public function setMethod(string $method)
{
$this->method = strtoupper($method);

Expand All @@ -153,11 +147,9 @@ public function getHost()
/**
* Sets the HTTP host.
*
* @param string $host The HTTP host
*
* @return $this
*/
public function setHost($host)
public function setHost(string $host)
{
$this->host = strtolower($host);

Expand All @@ -177,11 +169,9 @@ public function getScheme()
/**
* Sets the HTTP scheme.
*
* @param string $scheme The HTTP scheme
*
* @return $this
*/
public function setScheme($scheme)
public function setScheme(string $scheme)
{
$this->scheme = strtolower($scheme);

Expand All @@ -201,13 +191,11 @@ public function getHttpPort()
/**
* Sets the HTTP port.
*
* @param int $httpPort The HTTP port
*
* @return $this
*/
public function setHttpPort($httpPort)
public function setHttpPort(int $httpPort)
{
$this->httpPort = (int) $httpPort;
$this->httpPort = $httpPort;

return $this;
}
Expand All @@ -225,13 +213,11 @@ public function getHttpsPort()
/**
* Sets the HTTPS port.
*
* @param int $httpsPort The HTTPS port
*
* @return $this
*/
public function setHttpsPort($httpsPort)
public function setHttpsPort(int $httpsPort)
{
$this->httpsPort = (int) $httpsPort;
$this->httpsPort = $httpsPort;

return $this;
}
Expand All @@ -249,11 +235,9 @@ public function getQueryString()
/**
* Sets the query string.
*
* @param string $queryString The query string (after "?")
*
* @return $this
*/
public function setQueryString($queryString)
public function setQueryString(?string $queryString)
{
// string cast to be fault-tolerant, accepting null
$this->queryString = (string) $queryString;
Expand Down Expand Up @@ -288,36 +272,31 @@ public function setParameters(array $parameters)
/**
* Gets a parameter value.
*
* @param string $name A parameter name
*
* @return mixed The parameter value or null if nonexistent
*/
public function getParameter($name)
public function getParameter(string $name)
{
return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
}

/**
* Checks if a parameter value is set for the given parameter.
*
* @param string $name A parameter name
*
* @return bool True if the parameter value is set, false otherwise
*/
public function hasParameter($name)
public function hasParameter(string $name)
{
return \array_key_exists($name, $this->parameters);
}

/**
* Sets a parameter value.
*
* @param string $name A parameter name
* @param mixed $parameter The parameter value
* @param mixed $parameter The parameter value
*
* @return $this
*/
public function setParameter($name, $parameter)
public function setParameter(string $name, $parameter)
{
$this->parameters[$name] = $parameter;

Expand Down
61 changes: 16 additions & 45 deletions src/Symfony/Component/Routing/Route.php
Expand Up @@ -126,11 +126,9 @@ public function getPath()
*
* This method implements a fluent interface.
*
* @param string $pattern The path pattern
*
* @return $this
*/
public function setPath($pattern)
public function setPath(string $pattern)
{
if (false !== strpbrk($pattern, '?<')) {
$pattern = preg_replace_callback('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
Expand Down Expand Up @@ -168,11 +166,9 @@ public function getHost()
*
* This method implements a fluent interface.
*
* @param string $pattern The host pattern
*
* @return $this
*/
public function setHost($pattern)
public function setHost(?string $pattern)
{
$this->host = (string) $pattern;
$this->compiled = null;
Expand Down Expand Up @@ -212,11 +208,9 @@ public function setSchemes($schemes)
/**
* Checks if a scheme requirement has been set.
*
* @param string $scheme
*
* @return bool true if the scheme requirement exists, otherwise false
*/
public function hasScheme($scheme)
public function hasScheme(string $scheme)
{
return \in_array(strtolower($scheme), $this->schemes, true);
}
Expand Down Expand Up @@ -302,12 +296,11 @@ public function addOptions(array $options)
*
* This method implements a fluent interface.
*
* @param string $name An option name
* @param mixed $value The option value
* @param mixed $value The option value
*
* @return $this
*/
public function setOption($name, $value)
public function setOption(string $name, $value)
{
$this->options[$name] = $value;
$this->compiled = null;
Expand All @@ -318,23 +311,19 @@ public function setOption($name, $value)
/**
* Get an option value.
*
* @param string $name An option name
*
* @return mixed The option value or null when not given
*/
public function getOption($name)
public function getOption(string $name)
{
return isset($this->options[$name]) ? $this->options[$name] : null;
}

/**
* Checks if an option has been set.
*
* @param string $name An option name
*
* @return bool true if the option is set, false otherwise
*/
public function hasOption($name)
public function hasOption(string $name)
{
return \array_key_exists($name, $this->options);
}
Expand Down Expand Up @@ -387,36 +376,31 @@ public function addDefaults(array $defaults)
/**
* Gets a default value.
*
* @param string $name A variable name
*
* @return mixed The default value or null when not given
*/
public function getDefault($name)
public function getDefault(string $name)
{
return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
}

/**
* Checks if a default value is set for the given variable.
*
* @param string $name A variable name
*
* @return bool true if the default value is set, false otherwise
*/
public function hasDefault($name)
public function hasDefault(string $name)
{
return \array_key_exists($name, $this->defaults);
}

/**
* Sets a default value.
*
* @param string $name A variable name
* @param mixed $default The default value
* @param mixed $default The default value
*
* @return $this
*/
public function setDefault($name, $default)
public function setDefault(string $name, $default)
{
$this->defaults[$name] = $default;
$this->compiled = null;
Expand Down Expand Up @@ -472,36 +456,29 @@ public function addRequirements(array $requirements)
/**
* Returns the requirement for the given key.
*
* @param string $key The key
*
* @return string|null The regex or null when not given
*/
public function getRequirement($key)
public function getRequirement(string $key)
{
return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
}

/**
* Checks if a requirement is set for the given key.
*
* @param string $key A variable name
*
* @return bool true if a requirement is specified, false otherwise
*/
public function hasRequirement($key)
public function hasRequirement(string $key)
{
return \array_key_exists($key, $this->requirements);
}

/**
* Sets a requirement for the given key.
*
* @param string $key The key
* @param string $regex The regex
*
* @return $this
*/
public function setRequirement($key, $regex)
public function setRequirement(string $key, string $regex)
{
$this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
$this->compiled = null;
Expand All @@ -524,11 +501,9 @@ public function getCondition()
*
* This method implements a fluent interface.
*
* @param string $condition The condition
*
* @return $this
*/
public function setCondition($condition)
public function setCondition(?string $condition)
{
$this->condition = (string) $condition;
$this->compiled = null;
Expand Down Expand Up @@ -557,12 +532,8 @@ public function compile()
return $this->compiled = $class::compile($this);
}

private function sanitizeRequirement($key, $regex)
private function sanitizeRequirement(string $key, string $regex)
{
if (!\is_string($regex)) {
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
}

if ('' !== $regex && '^' === $regex[0]) {
$regex = (string) substr($regex, 1); // returns false for a single character
}
Expand Down

0 comments on commit 7b3ef19

Please sign in to comment.