From 18e3c751080b55cbe4bed8bd376f0b7b20eab618 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 2 Oct 2023 15:52:10 +0200 Subject: [PATCH] [HttpFoundation] Fix type of properties in Request class --- .../Config/Definition/Builder/TreeBuilder.php | 9 +- .../HttpClient/Response/MockResponse.php | 6 +- .../Component/HttpFoundation/Request.php | 84 +++++++++---------- 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php b/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php index 3e79eb4da514e..cdee55772bf11 100644 --- a/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php +++ b/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php @@ -20,7 +20,14 @@ */ class TreeBuilder implements NodeParentInterface { + /** + * @var NodeInterface|null + */ protected $tree; + + /** + * @var NodeDefinition + */ protected $root; public function __construct(string $name, string $type = 'array', NodeBuilder $builder = null) @@ -53,7 +60,7 @@ public function buildTree(): NodeInterface public function setPathSeparator(string $separator) { // unset last built as changing path separator changes all nodes - unset($this->tree); + $this->tree = null; $this->root->setPathSeparator($separator); } diff --git a/src/Symfony/Component/HttpClient/Response/MockResponse.php b/src/Symfony/Component/HttpClient/Response/MockResponse.php index 4c21eba91e6b0..dba6307f2b5d9 100644 --- a/src/Symfony/Component/HttpClient/Response/MockResponse.php +++ b/src/Symfony/Component/HttpClient/Response/MockResponse.php @@ -28,7 +28,7 @@ class MockResponse implements ResponseInterface, StreamableInterface use CommonResponseTrait; use TransportResponseTrait; - private string|iterable $body; + private string|iterable|null $body; private array $requestOptions = []; private string $requestUrl; private string $requestMethod; @@ -98,7 +98,7 @@ public function cancel(): void $this->info['canceled'] = true; $this->info['error'] = 'Response has been canceled.'; try { - unset($this->body); + $this->body = null; } catch (TransportException $e) { // ignore errors when canceling } @@ -172,7 +172,7 @@ protected static function perform(ClientState $multi, array &$responses): void foreach ($responses as $response) { $id = $response->id; - if (!isset($response->body)) { + if (null === $response->body) { // Canceled response $response->body = []; } elseif ([] === $response->body) { diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index e19d5c584cd79..9356e1ff161d5 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -136,57 +136,57 @@ class Request protected $content; /** - * @var string[] + * @var string[]|null */ protected $languages; /** - * @var string[] + * @var string[]|null */ protected $charsets; /** - * @var string[] + * @var string[]|null */ protected $encodings; /** - * @var string[] + * @var string[]|null */ protected $acceptableContentTypes; /** - * @var string + * @var string|null */ protected $pathInfo; /** - * @var string + * @var string|null */ protected $requestUri; /** - * @var string + * @var string|null */ protected $baseUrl; /** - * @var string + * @var string|null */ protected $basePath; /** - * @var string + * @var string|null */ protected $method; /** - * @var string + * @var string|null */ protected $format; /** - * @var SessionInterface|callable(): SessionInterface + * @var SessionInterface|callable():SessionInterface|null */ protected $session; @@ -282,16 +282,16 @@ public function initialize(array $query = [], array $request = [], array $attrib $this->headers = new HeaderBag($this->server->getHeaders()); $this->content = $content; - unset($this->languages); - unset($this->charsets); - unset($this->encodings); - unset($this->acceptableContentTypes); - unset($this->pathInfo); - unset($this->requestUri); - unset($this->baseUrl); - unset($this->basePath); - unset($this->method); - unset($this->format); + $this->languages = null; + $this->charsets = null; + $this->encodings = null; + $this->acceptableContentTypes = null; + $this->pathInfo = null; + $this->requestUri = null; + $this->baseUrl = null; + $this->basePath = null; + $this->method = null; + $this->format = null; } /** @@ -468,16 +468,16 @@ public function duplicate(array $query = null, array $request = null, array $att $dup->server = new ServerBag($server); $dup->headers = new HeaderBag($dup->server->getHeaders()); } - unset($dup->languages); - unset($dup->charsets); - unset($dup->encodings); - unset($dup->acceptableContentTypes); - unset($dup->pathInfo); - unset($dup->requestUri); - unset($dup->baseUrl); - unset($dup->basePath); - unset($dup->method); - unset($dup->format); + $dup->languages = null; + $dup->charsets = null; + $dup->encodings = null; + $dup->acceptableContentTypes = null; + $dup->pathInfo = null; + $dup->requestUri = null; + $dup->baseUrl = null; + $dup->basePath = null; + $dup->method = null; + $dup->format = null; if (!$dup->get('_format') && $this->get('_format')) { $dup->attributes->set('_format', $this->get('_format')); @@ -1182,7 +1182,7 @@ public function getHost(): string */ public function setMethod(string $method) { - unset($this->method); + $this->method = null; $this->server->set('REQUEST_METHOD', $method); } @@ -1201,7 +1201,7 @@ public function setMethod(string $method) */ public function getMethod(): string { - if (isset($this->method)) { + if (null !== $this->method) { return $this->method; } @@ -1249,7 +1249,7 @@ public function getRealMethod(): string */ public function getMimeType(string $format): ?string { - if (!isset(static::$formats)) { + if (null === static::$formats) { static::initializeFormats(); } @@ -1263,7 +1263,7 @@ public function getMimeType(string $format): ?string */ public static function getMimeTypes(string $format): array { - if (!isset(static::$formats)) { + if (null === static::$formats) { static::initializeFormats(); } @@ -1280,7 +1280,7 @@ public function getFormat(?string $mimeType): ?string $canonicalMimeType = trim(substr($mimeType, 0, $pos)); } - if (!isset(static::$formats)) { + if (null === static::$formats) { static::initializeFormats(); } @@ -1305,7 +1305,7 @@ public function getFormat(?string $mimeType): ?string */ public function setFormat(?string $format, string|array $mimeTypes) { - if (!isset(static::$formats)) { + if (null === static::$formats) { static::initializeFormats(); } @@ -1586,13 +1586,13 @@ public function isNoCache(): bool */ public function getPreferredFormat(?string $default = 'html'): ?string { - if (isset($this->preferredFormat) || null !== $preferredFormat = $this->getRequestFormat(null)) { - return $this->preferredFormat ??= $preferredFormat; + if ($this->preferredFormat ??= $this->getRequestFormat(null)) { + return $this->preferredFormat; } foreach ($this->getAcceptableContentTypes() as $mimeType) { - if ($preferredFormat = $this->getFormat($mimeType)) { - return $this->preferredFormat = $preferredFormat; + if ($this->preferredFormat = $this->getFormat($mimeType)) { + return $this->preferredFormat; } } @@ -1639,7 +1639,7 @@ public function getPreferredLanguage(array $locales = null): ?string */ public function getLanguages(): array { - if (isset($this->languages)) { + if (null !== $this->languages) { return $this->languages; }