Skip to content

Commit

Permalink
minor #41903 [HttpFoundation] add union types (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.0 branch.

Discussion
----------

[HttpFoundation] add union types

| Q             | A
| ------------- | ---
| Branch?       | 6.0
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Part of #41424
| License       | MIT
| Doc PR        | -

Commits
-------

9f4a5fb [HttpFoundation] add union types
  • Loading branch information
nicolas-grekas committed Jun 30, 2021
2 parents b6aaf51 + 9f4a5fb commit ca39553
Show file tree
Hide file tree
Showing 41 changed files with 125 additions and 188 deletions.
4 changes: 1 addition & 3 deletions src/Symfony/Component/HttpFoundation/AcceptHeaderItem.php
Expand Up @@ -138,11 +138,9 @@ public function hasAttribute(string $name)
/**
* Returns an attribute by its name.
*
* @param mixed $default
*
* @return mixed
*/
public function getAttribute(string $name, $default = null)
public function getAttribute(string $name, mixed $default = null)
{
return $this->attributes[$name] ?? $default;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Expand Up @@ -44,7 +44,7 @@ class BinaryFileResponse extends Response
* @param bool $autoEtag Whether the ETag header should be automatically set
* @param bool $autoLastModified Whether the Last-Modified header should be automatically set
*/
public function __construct($file, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
public function __construct(\SplFileInfo|string $file, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
{
parent::__construct(null, $status, $headers);

Expand All @@ -58,13 +58,11 @@ public function __construct($file, int $status = 200, array $headers = [], bool
/**
* Sets the file to stream.
*
* @param \SplFileInfo|string $file The file to stream
*
* @return $this
*
* @throws FileException
*/
public function setFile($file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
public function setFile(\SplFileInfo|string $file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
{
if (!$file instanceof File) {
if ($file instanceof \SplFileInfo) {
Expand Down
14 changes: 4 additions & 10 deletions src/Symfony/Component/HttpFoundation/Cookie.php
Expand Up @@ -71,7 +71,7 @@ public static function fromString(string $cookie, bool $decode = false)
return new static($name, $value, $data['expires'], $data['path'], $data['domain'], $data['secure'], $data['httponly'], $data['raw'], $data['samesite']);
}

public static function create(string $name, string $value = null, $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = self::SAMESITE_LAX): self
public static function create(string $name, string $value = null, int|string|\DateTimeInterface $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = self::SAMESITE_LAX): self
{
return new self($name, $value, $expire, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
}
Expand All @@ -89,7 +89,7 @@ public static function create(string $name, string $value = null, $expire = 0, ?
*
* @throws \InvalidArgumentException
*/
public function __construct(string $name, string $value = null, $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = 'lax')
public function __construct(string $name, string $value = null, int|string|\DateTimeInterface $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = 'lax')
{
// from PHP source code
if ($raw && false !== strpbrk($name, self::$reservedCharsList)) {
Expand Down Expand Up @@ -140,11 +140,9 @@ public function withDomain(?string $domain): self
/**
* Creates a cookie copy with a new time the cookie expires.
*
* @param int|string|\DateTimeInterface $expire
*
* @return static
*/
public function withExpires($expire = 0): self
public function withExpires(int|string|\DateTimeInterface $expire = 0): self
{
$cookie = clone $this;
$cookie->expire = self::expiresTimestamp($expire);
Expand All @@ -154,12 +152,8 @@ public function withExpires($expire = 0): self

/**
* Converts expires formats to a unix timestamp.
*
* @param int|string|\DateTimeInterface $expire
*
* @return int
*/
private static function expiresTimestamp($expire = 0)
private static function expiresTimestamp(int|string|\DateTimeInterface $expire = 0): int
{
// convert expiration time to a Unix timestamp
if ($expire instanceof \DateTimeInterface) {
Expand Down
Expand Up @@ -20,7 +20,7 @@
*/
class SessionNotFoundException extends \LogicException implements RequestExceptionInterface
{
public function __construct($message = 'There is currently no session available.', $code = 0, \Throwable $previous = null)
public function __construct(string $message = 'There is currently no session available.', int $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpFoundation;

use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

/**
Expand All @@ -23,7 +24,7 @@ class ExpressionRequestMatcher extends RequestMatcher
private $language;
private $expression;

public function setExpression(ExpressionLanguage $language, $expression)
public function setExpression(ExpressionLanguage $language, Expression|string $expression)
{
$this->language = $language;
$this->expression = $expression;
Expand Down
Expand Up @@ -13,7 +13,7 @@

class UnexpectedTypeException extends FileException
{
public function __construct($value, string $expectedType)
public function __construct(mixed $value, string $expectedType)
{
parent::__construct(sprintf('Expected argument of type %s, %s given', $expectedType, get_debug_type($value)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Expand Up @@ -231,7 +231,7 @@ public static function getMaxFilesize()
*
* @return int|float Returns float if size > PHP_INT_MAX
*/
private static function parseFilesize($size)
private static function parseFilesize(string $size)
{
if ('' === $size) {
return 0;
Expand Down
38 changes: 16 additions & 22 deletions src/Symfony/Component/HttpFoundation/FileBag.php
Expand Up @@ -43,7 +43,7 @@ public function replace(array $files = [])
/**
* {@inheritdoc}
*/
public function set(string $key, $value)
public function set(string $key, mixed $value)
{
if (!\is_array($value) && !$value instanceof UploadedFile) {
throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
Expand All @@ -65,32 +65,28 @@ public function add(array $files = [])
/**
* Converts uploaded files to UploadedFile instances.
*
* @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
*
* @return UploadedFile[]|UploadedFile|null A (multi-dimensional) array of UploadedFile instances
*/
protected function convertFileInformation($file)
protected function convertFileInformation(array|UploadedFile $file)
{
if ($file instanceof UploadedFile) {
return $file;
}

if (\is_array($file)) {
$file = $this->fixPhpFilesArray($file);
$keys = array_keys($file);
sort($keys);

if (self::FILE_KEYS == $keys) {
if (\UPLOAD_ERR_NO_FILE == $file['error']) {
$file = null;
} else {
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false);
}
$file = $this->fixPhpFilesArray($file);
$keys = array_keys($file);
sort($keys);

if (self::FILE_KEYS == $keys) {
if (\UPLOAD_ERR_NO_FILE == $file['error']) {
$file = null;
} else {
$file = array_map([$this, 'convertFileInformation'], $file);
if (array_keys($keys) === $keys) {
$file = array_filter($file);
}
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false);
}
} else {
$file = array_map(function ($v) { return $v instanceof UploadedFile || \is_array($v) ? $this->convertFileInformation($v) : $v; }, $file);
if (array_keys($keys) === $keys) {
$file = array_filter($file);
}
}

Expand All @@ -109,11 +105,9 @@ protected function convertFileInformation($file)
* It's safe to pass an already converted array, in which case this method
* just returns the original array unmodified.
*
* @param array $data
*
* @return array
*/
protected function fixPhpFilesArray($data)
protected function fixPhpFilesArray(array $data)
{
$keys = array_keys($data);
sort($keys);
Expand Down
16 changes: 7 additions & 9 deletions src/Symfony/Component/HttpFoundation/HeaderBag.php
Expand Up @@ -121,10 +121,10 @@ public function get(string $key, string $default = null)
/**
* Sets a header by name.
*
* @param string|string[] $values The value or an array of values
* @param bool $replace Whether to replace the actual value or not (true by default)
* @param string|string[]|null $values The value or an array of values
* @param bool $replace Whether to replace the actual value or not (true by default)
*/
public function set(string $key, $values, bool $replace = true)
public function set(string $key, string|array|null $values, bool $replace = true)
{
$key = strtr($key, self::UPPER, self::LOWER);

Expand Down Expand Up @@ -186,7 +186,7 @@ public function remove(string $key)
/**
* Returns the HTTP header value converted to a date.
*
* @return \DateTimeInterface|null The parsed DateTime or the default value if the header does not exist
* @return \DateTime|null The parsed DateTime or the default value if the header does not exist
*
* @throws \RuntimeException When the HTTP header is not parseable
*/
Expand All @@ -205,10 +205,8 @@ public function getDate(string $key, \DateTime $default = null)

/**
* Adds a custom Cache-Control directive.
*
* @param mixed $value The Cache-Control directive value
*/
public function addCacheControlDirective(string $key, $value = true)
public function addCacheControlDirective(string $key, bool|string $value = true)
{
$this->cacheControl[$key] = $value;

Expand All @@ -228,11 +226,11 @@ public function hasCacheControlDirective(string $key)
/**
* Returns a Cache-Control directive value by name.
*
* @return mixed The directive value if defined, null otherwise
* @return bool|string|null The directive value if defined, null otherwise
*/
public function getCacheControlDirective(string $key)
{
return \array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
return $this->cacheControl[$key] ?? null;
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/HttpFoundation/InputBag.php
Expand Up @@ -24,10 +24,8 @@ final class InputBag extends ParameterBag
* Returns a scalar input value by name.
*
* @param string|int|float|bool|null $default The default value if the input key does not exist
*
* @return string|int|float|bool|null
*/
public function get(string $key, $default = null)
public function get(string $key, mixed $default = null): string|int|float|bool|null
{
if (null !== $default && !is_scalar($default) && !$default instanceof \Stringable) {
throw new \InvalidArgumentException(sprintf('Excepted a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
Expand Down Expand Up @@ -74,7 +72,7 @@ public function add(array $inputs = [])
*
* @param string|int|float|bool|array|null $value
*/
public function set(string $key, $value)
public function set(string $key, mixed $value)
{
if (null !== $value && !is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
throw new \InvalidArgumentException(sprintf('Excepted a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
Expand All @@ -86,7 +84,7 @@ public function set(string $key, $value)
/**
* {@inheritdoc}
*/
public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = [])
{
$value = $this->has($key) ? $this->all()[$key] : $default;

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/IpUtils.php
Expand Up @@ -34,7 +34,7 @@ private function __construct()
*
* @return bool Whether the IP is valid
*/
public static function checkIp(?string $requestIp, $ips)
public static function checkIp(?string $requestIp, string|array $ips)
{
if (!\is_array($ips)) {
$ips = [$ips];
Expand Down
11 changes: 3 additions & 8 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Expand Up @@ -34,12 +34,9 @@ class JsonResponse extends Response
protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS;

/**
* @param mixed $data The response data
* @param int $status The response status code
* @param array $headers An array of response headers
* @param bool $json If the data is already a JSON string
* @param bool $json If the data is already a JSON string
*/
public function __construct($data = null, int $status = 200, array $headers = [], bool $json = false)
public function __construct(mixed $data = null, int $status = 200, array $headers = [], bool $json = false)
{
parent::__construct('', $status, $headers);

Expand Down Expand Up @@ -123,13 +120,11 @@ public function setJson(string $json)
/**
* Sets the data to be sent as JSON.
*
* @param mixed $data
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function setData($data = [])
public function setData(mixed $data = [])
{
try {
$data = json_encode($data, $this->encodingOptions);
Expand Down
19 changes: 4 additions & 15 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Expand Up @@ -79,23 +79,14 @@ public function add(array $parameters = [])
}

/**
* Returns a parameter by name.
*
* @param mixed $default The default value if the parameter key does not exist
*
* @return mixed
*/
public function get(string $key, $default = null)
public function get(string $key, mixed $default = null)
{
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

/**
* Sets a parameter by name.
*
* @param mixed $value The value
*/
public function set(string $key, $value)
public function set(string $key, mixed $value)
{
$this->parameters[$key] = $value;
}
Expand Down Expand Up @@ -172,15 +163,13 @@ public function getBoolean(string $key, bool $default = false)
/**
* Filter key.
*
* @param mixed $default Default = null
* @param int $filter FILTER_* constant
* @param mixed $options Filter options
* @param int $filter FILTER_* constant
*
* @see https://php.net/filter-var
*
* @return mixed
*/
public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = [])
{
$value = $this->get($key, $default);

Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -693,11 +693,9 @@ public static function getHttpMethodParameterOverride()
*
* Order of precedence: PATH (routing placeholders or custom attributes), GET, POST
*
* @param mixed $default The default value if the parameter key does not exist
*
* @return mixed
*/
public function get(string $key, $default = null)
public function get(string $key, mixed $default = null)
{
if ($this !== $result = $this->attributes->get($key, $this)) {
return $result;
Expand Down Expand Up @@ -1351,7 +1349,7 @@ public function getFormat(?string $mimeType)
*
* @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
*/
public function setFormat(?string $format, $mimeTypes)
public function setFormat(?string $format, string|array $mimeTypes)
{
if (null === static::$formats) {
static::initializeFormats();
Expand Down

0 comments on commit ca39553

Please sign in to comment.