8000 [HttpFoundation] add union types · symfony/symfony@cc86cb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc86cb8

Browse files
[HttpFoundation] add union types
1 parent f4d1e4f commit cc86cb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+131
-195
lines changed

src/Symfony/Component/HttpFoundation/AcceptHeaderItem.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,9 @@ public function hasAttribute(string $name)
138138
/**
139139
* Returns an attribute by its name.
140140
*
141-
* @param mixed $default
142-
*
143141
* @return mixed
144142
*/
145-
public function getAttribute(string $name, $default = null)
143+
public function getAttribute(string $name, mixed $default = null)
146144
{
147145
return $this->attributes[$name] ?? $default;
148146
}

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,14 @@ class BinaryFileResponse extends Response
3636
protected $deleteFileAfterSend = false;
3737

3838
/**
39-
* @param \SplFileInfo|string $file The file to stream
40-
* @param int $status The response status code
41-
* @param array $headers An array of response headers
42-
* @param bool $public Files are public by default
43-
* @param string|null $contentDisposition The type of Content-Disposition to set automatically with the filename
44-
* @param bool $autoEtag Whether the ETag header should be automatically set
45-
* @param bool $autoLastModified Whether the Last-Modified header should be automatically set
39+
* @param int $status The response status code
40+
* @param array $headers An array of response headers
41+
* @param bool $public Files are public by default
42+
* @param string|null $contentDisposition The type of Content-Disposition to set automatically with the filename
43+
* @param bool $autoEtag Whether the ETag header should be automatically set
44+
* @param bool $autoLastModified Whether the Last-Modified header should be automatically set
4645
*/
47-
public function __construct($file, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
46+
public function __construct(\SplFileInfo|string $file, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
4847
{
4948
parent::__construct(null, $status, $headers);
5049

@@ -58,13 +57,11 @@ public function __construct($file, int $status = 200, array $headers = [], bool
5857
/**
5958
* Sets the file to stream.
6059
*
61-
* @param \SplFileInfo|string $file The file to stream
62-
*
6360
* @return $this
6461
*
6562
* @throws FileException
6663
*/
67-
public function setFile($file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
64+
public function setFile(\SplFileInfo|string $file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
6865
{
6966
if (!$file instanceof File) {
7067
if ($file instanceof \SplFileInfo) {

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function fromString(string $cookie, bool $decode = false)
7171
return new static($name, $value, $data['expires'], $data['path'], $data['domain'], $data['secure'], $data['httponly'], $data['raw'], $data['samesite']);
7272
}
7373

74-
public static function create(string $name, string $value = null, $expire = 0, ?st F438 ring $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = self::SAMESITE_LAX): self
74+
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
7575
{
7676
return new self($name, $value, $expire, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
7777
}
@@ -89,7 +89,7 @@ public static function create(string $name, string $value = null, $expire = 0, ?
8989
*
9090
* @throws \InvalidArgumentException
9191
*/
92-
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')
92+
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')
9393
{
9494
// from PHP source code
9595
if ($raw && false !== strpbrk($name, self::$reservedCharsList)) {
@@ -140,11 +140,9 @@ public function withDomain(?string $domain): self
140140
/**
141141
* Creates a cookie copy with a new time the cookie expires.
142142
*
143-
* @param int|string|\DateTimeInterface $expire
144-
*
145143
* @return static
146144
*/
147-
public function withExpires($expire = 0): self
145+
public function withExpires(int|string|\DateTimeInterface $expire = 0): self
148146
{
149147
$cookie = clone $this;
150148
$cookie->expire = self::expiresTimestamp($expire);
@@ -154,12 +152,8 @@ public function withExpires($expire = 0): self
154152

155153
/**
156154
* Converts expires formats to a unix timestamp.
157-
*
158-
* @param int|string|\DateTimeInterface $expire
159-
*
160-
* @return int
161155
*/
162-
private static function expiresTimestamp($expire = 0)
156+
private static function expiresTimestamp(int|string|\DateTimeInterface $expire = 0): int
163157
{
164158
// convert expiration time to a Unix timestamp
165159
if ($expire instanceof \DateTimeInterface) {

src/Symfony/Component/HttpFoundation/Exception/SessionNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class SessionNotFoundException extends \LogicException implements RequestExceptionInterface
2222
{
23-
public function __construct($message = 'There is currently no session available.', $code = 0, \Throwable $previous = null)
23+
public function __construct(string $message = 'There is currently no session available.', int $code = 0, \Throwable $previous = null)
2424
{
2525
parent::__construct($message, $code, $previous);
2626
}

src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpFoundation;
1313

14+
use Symfony\Component\ExpressionLanguage\Expression;
1415
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1516

1617
/**
@@ -23,7 +24,7 @@ class ExpressionRequestMatcher extends RequestMatcher
2324
private $language;
2425
private $expression;
2526

26-
public function setExpression(ExpressionLanguage $language, $expression)
27+
public function setExpression(ExpressionLanguage $language, Expression|string $expression)
2728
{
2829
$this->language = $language;
2930
$this->expression = $expression;

src/Symfony/Component/HttpFoundation/File/Exception/UnexpectedTypeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class UnexpectedTypeException extends FileException
1515
{
16-
public function __construct($value, string $expectedType)
16+
public function __construct(mixed $value, string $expectedType)
1717
{
1818
parent::__construct(sprintf('Expected argument of type %s, %s given', $expectedType, get_debug_type($value)));
1919
}

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public static function getMaxFilesize()
231231
*
232232
* @return int|float Returns float if size > PHP_INT_MAX
233233
*/
234-
private static function parseFilesize($size)
234+
private static function parseFilesize(string $size)
235235
{
236236
if ('' === $size) {
237237
return 0;

src/Symfony/Component/HttpFoundation/FileBag.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function replace(array $files = [])
4343
/**
4444
* {@inheritdoc}
4545
*/
46-
public function set(string $key, $value)
46+
public function set(string $key, mixed $value)
4747
{
4848
if (!\is_array($value) && !$value instanceof UploadedFile) {
4949
throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
@@ -65,32 +65,28 @@ public function add(array $files = [])
6565
/**
6666
* Converts uploaded files to UploadedFile instances.
6767
*
68-
* @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
69-
*
7068
* @return UploadedFile[]|UploadedFile|null A (multi-dimensional) array of UploadedFile instances
7169
*/
72-
protected function convertFileInformation($file)
70+
protected function convertFileInformation(mixed $file)
7371
{
74-
if ($file instanceof UploadedFile) {
72+
if (!\is_array($file)) {
7573
return $file;
7674
}
7775

78-
if (\is_array($file)) {
79-
$file = $this->fixPhpFilesArray($file);
80-
$keys = array_keys($file);
81-
sort($keys);
82-
83-
if (self::FILE_KEYS == $keys) {
84-
if (\UPLOAD_ERR_NO_FILE == $file['error']) {
85-
$file = null;
86-
} else {
87-
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false);
88-
}
76+
$file = $this->fixPhpFilesArray($file);
77+
$keys = array_keys($file);
78+
sort($keys);
79+
80+
if (self::FILE_KEYS == $keys) {
81+
if (\UPLOAD_ERR_NO_FILE == $file['error']) {
82+
$file = null;
8983
} else {
90-
$file = array_map([$this, 'convertFileInformation'], $file);
91-
if (array_keys($keys) === $keys) {
92-
$file = array_filter($file);
93-
}
84+
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false);
85+
}
86+
} else {
87+
$file = array_map([$this, 'convertFileInformation'], $file);
88+
if (array_keys($keys) === $keys) {
89+
$file = array_filter($file);
9490
}
9591
}
9692

@@ -109,11 +105,9 @@ protected function convertFileInformation($file)
109105
* It's safe to pass an already converted array, in which case this method
110106
* just returns the original array unmodified.
111107
*
112-
* @param array $data
113-
*
114108
* @return array
115109
*/
116-
protected function fixPhpFilesArray($data)
110+
protected function fixPhpFilesArray(array $data)
117111
{
118112
$keys = array_keys($data);
119113
sort($keys);

src/Symfony/Component/HttpFoundation/HeaderBag.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function get(string $key, string $default = null)
124124
* @param string|string[] $values The value or an array of values
125125
* @param bool $replace Whether to replace the actual value or not (true by default)
126126
*/
127-
public function set(string $key, $values, bool $replace = true)
127+
public function set(string $key, string|array|null $values, bool $replace = true)
128128
{
129129
$key = strtr($key, self::UPPER, self::LOWER);
130130

@@ -190,7 +190,7 @@ public function remove(string $key)
190190
*
191191
* @throws \RuntimeException When the HTTP header is not parseable
192192
*/
193-
public function getDate(string $key, \DateTime $default = null)
193+
public function getDate(string $key, \DateTimeInterface $default = null)
194194
{
195195
if (null === $value = $this->get($key)) {
196196
return $default;
@@ -205,10 +205,8 @@ public function getDate(string $key, \DateTime $default = null)
205205

206206
/**
207207
* Adds a custom Cache-Control directive.
208-
*
209-
* @param mixed $value The Cache-Control directive value
210208
*/
211-
public function addCacheControlDirective(string $key, $value = true)
209+
public function addCacheControlDirective(string $key, bool|string $value = true)
212210
{
213211
$this->cacheControl[$key] = $value;
214212

@@ -222,17 +220,17 @@ public function addCacheControlDirective(string $key, $value = true)
222220
*/
223221
public function hasCacheControlDirective(string $key)
224222
{
225-
return \array_key_exists($key, $this->cacheControl);
223+
return isset($this->cacheControl[$key]);
226224
}
227225

228226
/**
229227
* Returns a Cache-Control directive value by name.
230228
*
231-
* @return mixed The directive value if defined, null otherwise
229+
* @return bool|string|null The directive value if defined, null otherwise
232230
*/
233231
public function getCacheControlDirective(string $key)
234232
{
235-
return \array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
233+
return $this->cacheControl[$key] ?? null;
236234
}
237235

238236
/**

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ final class InputBag extends ParameterBag
2424
* Returns a scalar input value by name.
2525
*
2626
* @param string|int|float|bool|null $default The default value if the input key does not exist
27-
*
28-
* @return string|int|float|bool|null
2927
*/
30-
public function get(string $key, $default = null)
28+
public function get(string $key, mixed $default = null): string|int|float|bool|null
3129
{
3230
if (null !== $default && !is_scalar($default) && !$default instanceof \Stringable) {
3331
throw new \InvalidArgumentException(sprintf('Excepted a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
@@ -74,7 +72,7 @@ public function add(array $inputs = [])
7472
*
7573
* @param string|int|float|bool|array|null $value
7674
*/
77-
public function set(string $key, $value)
75+
public function set(string $key, mixed $value)
7876
{
7977
if (null !== $value && !is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
8078
throw new \InvalidArgumentException(sprintf('Excepted a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
@@ -86,7 +84,7 @@ public function set(string $key, $value)
8684
/**
8785
* {@inheritdoc}
8886
*/
89-
public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
87+
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = [])
9088
{
9189
$value = $this->has($key) ? $this->all()[$key] : $default;
9290

src/Symfony/Component/HttpFoundation/IpUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private function __construct()
3434
*
3535
* @return bool Whether the IP is valid
3636
*/
37-
public static function checkIp(?string $requestIp, $ips)
37+
public static function checkIp(?string $requestIp, string|array $ips)
3838
{
3939
if (!\is_array($ips)) {
4040
$ips = [$ips];

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ class JsonResponse extends Response
3434
protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS;
3535

3636
/**
37-
* @param mixed $data The response data
38-
* @param int $status The response status code
39-
* @param array $headers An array of response headers
40-
* @param bool $json If the data is already a JSON string
37+
* @param bool $json If the data is already a JSON string
4138
*/
42-
public function __construct($data = null, int $status = 200, array $headers = [], bool $json = false)
39+
public function __construct(mixed $data = null, int $status = 200, array $headers = [], bool $json = false)
4340
{
4441
parent::__construct('', $status, $headers);
4542

@@ -123,13 +120,11 @@ public function setJson(string $json)
123120
/**
124121
* Sets the data to be sent as JSON.
125122
*
126-
* @param mixed $data
127-
*
128123
* @return $this
129124
*
130125
* @throws \InvalidArgumentException
131126
*/
132-
public function setData($data = [])
127+
public function setData(mixed $data = [])
133128
{
134129
try {
135130
$data = json_encode($data, $this->encodingOptions);

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,14 @@ public function add(array $parameters = [])
7979
}
8080

8181
/**
82-
* Returns a parameter by name.
83-
*
84-
* @param mixed $default The default value if the parameter key does not exist
85-
*
8682
* @return mixed
8783
*/
88-
public function get(string $key, $default = null)
84+
public function get(string $key, mixed $default = null)
8985
{
9086
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
9187
}
9288

93-
/**
94-
* Sets a parameter by name.
95-
*
96-
* @param mixed $value The value
97-
*/
98-
public function set(string $key, $value)
89+
public function set(string $key, mixed $value)
9990
{
10091
$this->parameters[$key] = $value;
10192
}
@@ -172,15 +163,13 @@ public function getBoolean(string $key, bool $default = false)
172163
/**
173164
* Filter key.
174165
*
175-
* @param mixed $default Default = null
176-
* @param int $filter FILTER_* constant
177-
* @param mixed $options Filter options
166+
* @param int $filter FILTER_* constant
178167
*
179168
* @see https://php.net/filter-var
180169
*
181170
* @return mixed
182171
*/
183-
public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
172+
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = [])
184173
{
185174
$value = $this->get($key, $default);
186175

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,9 @@ public static function getHttpMethodParameterOverride()
693693
*
694694
* Order of precedence: PATH (routing placeholders or custom attributes), GET, POST
695695
*
696-
* @param mixed $default The default value if the parameter key does not exist
697-
*
698696
* @return mixed
699697
*/
700-
public function get(string $key, $default = null)
698+
public function get(string $key, mixed $default = null)
701699
{
702700
if ($this !== $result = $this->attributes->get($key, $this)) {
703701
return $result;
@@ -1351,7 +1349,7 @@ public function getFormat(?string $mimeType)
13511349
*
13521350
* @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
13531351
*/
1354-
public function setFormat(?string $format, $mimeTypes)
1352+
public function setFormat(?string $format, string|array $mimeTypes)
13551353
{
13561354
if (null === static::$formats) {
13571355
static::initializeFormats();

0 commit comments

Comments
 (0)
0