Skip to content

Commit f5b5945

Browse files
committed
[HttpFoundation] Add types to private properties
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent d94107b commit f5b5945

37 files changed

+143
-248
lines changed

AcceptHeader.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ class AcceptHeader
2727
/**
2828
* @var AcceptHeaderItem[]
2929
*/
30-
private $items = [];
30+
private array $items = [];
3131

32-
/**
33-
* @var bool
34-
*/
35-
private $sorted = true;
32+
private bool $sorted = true;
3633

3734
/**
3835
* @param AcceptHeaderItem[] $items

AcceptHeaderItem.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
class AcceptHeaderItem
2020
{
21-
private $value;
22-
private $quality = 1.0;
23-
private $index = 0;
24-
private $attributes = [];
21+
private string $value;
22+
private float $quality = 1.0;
23+
private int $index = 0;
24+
private array $attributes = [];
2525

2626
public function __construct(string $value, array $attributes = [])
2727
{

Cookie.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class Cookie
3030
protected $secure;
3131
protected $httpOnly;
3232

33-
private $raw;
34-
private $sameSite;
35-
private $secureDefault = false;
33+
private bool $raw;
34+
private ?string $sameSite = null;
35+
private bool $secureDefault = false;
3636

37-
private static $reservedCharsList = "=,; \t\r\n\v\f";
37+
private const RESERVED_CHARS_LIST = "=,; \t\r\n\v\f";
3838
private const RESERVED_CHARS_FROM = ['=', ',', ';', ' ', "\t", "\r", "\n", "\v", "\f"];
3939
private const RESERVED_CHARS_TO = ['%3D', '%2C', '%3B', '%20', '%09', '%0D', '%0A', '%0B', '%0C'];
4040

@@ -90,7 +90,7 @@ public static function create(string $name, string $value = null, int|string|\Da
9090
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')
9191
{
9292
// from PHP source code
93-
if ($raw && false !== strpbrk($name, self::$reservedCharsList)) {
93+
if ($raw && false !== strpbrk($name, self::RESERVED_CHARS_LIST)) {
9494
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
9595
}
9696

@@ -199,7 +199,7 @@ public function withHttpOnly(bool $httpOnly = true): static
199199
*/
200200
public function withRaw(bool $raw = true): static
201201
{
202-
if ($raw && false !== strpbrk($this->name, self::$reservedCharsList)) {
202+
if ($raw && false !== strpbrk($this->name, self::RESERVED_CHARS_LIST)) {
203203
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $this->name));
204204
}
205205

ExpressionRequestMatcher.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*/
2222
class ExpressionRequestMatcher extends RequestMatcher
2323
{
24-
private $language;
25-
private $expression;
24+
private ExpressionLanguage $language;
25+
private Expression|string $expression;
2626

2727
public function setExpression(ExpressionLanguage $language, Expression|string $expression)
2828
{
@@ -32,7 +32,7 @@ public function setExpression(ExpressionLanguage $language, Expression|string $e
3232

3333
public function matches(Request $request): bool
3434
{
35-
if (!$this->language) {
35+
if (!isset($this->language)) {
3636
throw new \LogicException('Unable to match the request as the expression language is not available.');
3737
}
3838

File/UploadedFile.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
*/
3232
class UploadedFile extends File
3333
{
34-
private $test;
35-
private $originalName;
36-
private $mimeType;
37-
private $error;
34+
private bool $test;
35+
private string $originalName;
36+
private string $mimeType;
37+
private int $error;
3838

3939
/**
4040
* Accepts the information of the uploaded file as provided by the PHP global $_FILES.

IpUtils.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class IpUtils
2020
{
21-
private static $checkedIps = [];
21+
private static array $checkedIps = [];
2222

2323
/**
2424
* This class should not be instantiated.

Request.php

+8-19
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,12 @@ class Request
205205

206206
protected static $requestFactory;
207207

208-
/**
209-
* @var string|null
210-
*/
211-
private $preferredFormat;
212-
private $isHostValid = true;
213-
private $isForwardedValid = true;
214-
215-
/**
216-
* @var bool|null
217-
*/
218-
private $isSafeContentPreferred;
208+
private ?string $preferredFormat = null;
209+
private bool $isHostValid = true;
210+
private bool $isForwardedValid = true;
211+
private bool $isSafeContentPreferred;
219212

220-
private static $trustedHeaderSet = -1;
213+
private static int $trustedHeaderSet = -1;
221214

222215
private const FORWARDED_PARAMS = [
223216
self::HEADER_X_FORWARDED_FOR => 'for',
@@ -1665,20 +1658,16 @@ public function isXmlHttpRequest(): bool
16651658
*/
16661659
public function preferSafeContent(): bool
16671660
{
1668-
if (null !== $this->isSafeContentPreferred) {
1661+
if (isset($this->isSafeContentPreferred)) {
16691662
return $this->isSafeContentPreferred;
16701663
}
16711664

16721665
if (!$this->isSecure()) {
16731666
// see https://tools.ietf.org/html/rfc8674#section-3
1674-
$this->isSafeContentPreferred = false;
1675-
1676-
return $this->isSafeContentPreferred;
1667+
return $this->isSafeContentPreferred = false;
16771668
}
16781669

1679-
$this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');
1680-
1681-
return $this->isSafeContentPreferred;
1670+
return $this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');
16821671
}
16831672

16841673
/*

RequestMatcher.php

+8-19
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,29 @@
1818
*/
1919
class RequestMatcher implements RequestMatcherInterface
2020
{
21-
/**
22-
* @var string|null
23-
*/
24-
private $path;
25-
26-
/**
27-
* @var string|null
28-
*/
29-
private $host;
30-
31-
/**
32-
* @var int|null
33-
*/
34-
private $port;
21+
private ?string $path = null;
22+
private ?string $host = null;
23+
private ?int $port = null;
3524

3625
/**
3726
* @var string[]
3827
*/
39-
private $methods = [];
28+
private array $methods = [];
4029

4130
/**
4231
* @var string[]
4332
*/
44-
private $ips = [];
33+
private array $ips = [];
4534

4635
/**
47-
* @var array
36+
* @var string[]
4837
*/
49-
private $attributes = [];
38+
private array $attributes = [];
5039

5140
/**
5241
* @var string[]
5342
*/
54-
private $schemes = [];
43+
private array $schemes = [];
5544

5645
/**
5746
* @param string|string[]|null $methods

RequestStack.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RequestStack
2424
/**
2525
* @var Request[]
2626
*/
27-
private $requests = [];
27+
private array $requests = [];
2828

2929
/**
3030
* Pushes a Request on the stack.

Session/Attribute/AttributeBag.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable
1818
{
19-
private $name = 'attributes';
20-
private $storageKey;
19+
private string $name = 'attributes';
20+
private string $storageKey;
2121

2222
protected $attributes = [];
2323

Session/Flash/AutoExpireFlashBag.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
class AutoExpireFlashBag implements FlashBagInterface
2020
{
21-
private $name = 'flashes';
22-
private $flashes = ['display' => [], 'new' => []];
23-
private $storageKey;
21+
private string $name = 'flashes';
22+
private array $flashes = ['display' => [], 'new' => []];
23+
private string $storageKey;
2424

2525
/**
2626
* @param string $storageKey The key used to store flashes in the session

Session/Flash/FlashBag.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
class FlashBag implements FlashBagInterface
2020
{
21-
private $name = 'flashes';
22-
private $flashes = [];
23-
private $storageKey;
21+
private string $name = 'flashes';
22+
private array $flashes = [];
23+
private string $storageKey;
2424

2525
/**
2626
* @param string $storageKey The key used to store flashes in the session

Session/Session.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
3232
{
3333
protected $storage;
3434

35-
private $flashName;
36-
private $attributeName;
37-
private $data = [];
38-
private $usageIndex = 0;
39-
private $usageReporter;
35+
private string $flashName;
36+
private string $attributeName;
37+
private array $data = [];
38+
private int $usageIndex = 0;
39+
private ?\Closure $usageReporter;
4040

4141
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, callable $usageReporter = null)
4242
{
4343
$this->storage = $storage ?? new NativeSessionStorage();
44-
$this->usageReporter = $usageReporter;
44+
$this->usageReporter = $usageReporter instanceof \Closure || !\is_callable($usageReporter) ? $usageReporter : \Closure::fromCallable($usageReporter);
4545

4646
$attributes = $attributes ?? new AttributeBag();
4747
$this->attributeName = $attributes->getName();

Session/SessionBagProxy.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
*/
1919
final class SessionBagProxy implements SessionBagInterface
2020
{
21-
private $bag;
22-
private $data;
23-
private $usageIndex;
24-
private $usageReporter;
21+
private SessionBagInterface $bag;
22+
private array $data;
23+
private ?int $usageIndex;
24+
private ?\Closure $usageReporter;
2525

2626
public function __construct(SessionBagInterface $bag, array &$data, ?int &$usageIndex, ?callable $usageReporter)
2727
{
2828
$this->bag = $bag;
2929
$this->data = &$data;
3030
$this->usageIndex = &$usageIndex;
31-
$this->usageReporter = $usageReporter;
31+
$this->usageReporter = $usageReporter instanceof \Closure || !\is_callable($usageReporter) ? $usageReporter : \Closure::fromCallable($usageReporter);
3232
}
3333

3434
public function getBag(): SessionBagInterface

Session/SessionFactory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class_exists(Session::class);
2222
*/
2323
class SessionFactory implements SessionFactoryInterface
2424
{
25-
private $requestStack;
26-
private $storageFactory;
27-
private $usageReporter;
25+
private RequestStack $requestStack;
26+
private SessionStorageFactoryInterface $storageFactory;
27+
private ?\Closure $usageReporter;
2828

2929
public function __construct(RequestStack $requestStack, SessionStorageFactoryInterface $storageFactory, callable $usageReporter = null)
3030
{
3131
$this->requestStack = $requestStack;
3232
$this->storageFactory = $storageFactory;
33-
$this->usageReporter = $usageReporter;
33+
$this->usageReporter = $usageReporter instanceof \Closure || !\is_callable($usageReporter) ? $usageReporter : \Closure::fromCallable($usageReporter);
3434
}
3535

3636
public function createSession(): SessionInterface

Session/Storage/Handler/AbstractSessionHandler.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
*/
2323
abstract class AbstractSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
2424
{
25-
private $sessionName;
26-
private $prefetchId;
27-
private $prefetchData;
28-
private $newSessionId;
29-
private $igbinaryEmptyData;
25+
private string $sessionName;
26+
private string $prefetchId;
27+
private string $prefetchData;
28+
private ?string $newSessionId = null;
29+
private string $igbinaryEmptyData;
3030

3131
public function open(string $savePath, string $sessionName): bool
3232
{
@@ -54,10 +54,10 @@ public function validateId(string $sessionId): bool
5454

5555
public function read(string $sessionId): string
5656
{
57-
if (null !== $this->prefetchId) {
57+
if (isset($this->prefetchId)) {
5858
$prefetchId = $this->prefetchId;
5959
$prefetchData = $this->prefetchData;
60-
$this->prefetchId = $this->prefetchData = null;
60+
unset($this->prefetchId, $this->prefetchData);
6161

6262
if ($prefetchId === $sessionId || '' === $prefetchData) {
6363
$this->newSessionId = '' === $prefetchData ? $sessionId : null;
@@ -74,10 +74,8 @@ public function read(string $sessionId): string
7474

7575
public function write(string $sessionId, string $data): bool
7676
{
77-
if (null === $this->igbinaryEmptyData) {
78-
// see https://github.com/igbinary/igbinary/issues/146
79-
$this->igbinaryEmptyData = \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
80-
}
77+
// see https://github.com/igbinary/igbinary/issues/146
78+
$this->igbinaryEmptyData ??= \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
8179
if ('' === $data || $this->igbinaryEmptyData === $data) {
8280
return $this->destroy($sessionId);
8381
}
@@ -89,7 +87,7 @@ public function write(string $sessionId, string $data): bool
8987
public function destroy(string $sessionId): bool
9088
{
9189
if (!headers_sent() && filter_var(ini_get('session.use_cookies'), \FILTER_VALIDATE_BOOLEAN)) {
92-
if (!$this->sessionName) {
90+
if (!isset($this->sessionName)) {
9391
throw new \LogicException(sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', static::class));
9492
}
9593
$cookie = SessionUtils::popSessionCookie($this->sessionName, $sessionId);

Session/Storage/Handler/MarshallingSessionHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
class MarshallingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
2020
{
21-
private $handler;
22-
private $marshaller;
21+
private AbstractSessionHandler $handler;
22+
private MarshallerInterface $marshaller;
2323

2424
public function __construct(AbstractSessionHandler $handler, MarshallerInterface $marshaller)
2525
{

0 commit comments

Comments
 (0)