Skip to content

Commit dbd81eb

Browse files
committed
[Lock] Add types to private properties
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent 3b31540 commit dbd81eb

12 files changed

+53
-61
lines changed

Key.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
*/
2121
final class Key
2222
{
23-
private $resource;
24-
private $expiringTime;
25-
private $state = [];
26-
private $serializable = true;
23+
private string $resource;
24+
private ?float $expiringTime = null;
25+
private array $state = [];
26+
private bool $serializable = true;
2727

2828
public function __construct(string $resource)
2929
{

Lock.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ final class Lock implements SharedLockInterface, LoggerAwareInterface
2929
{
3030
use LoggerAwareTrait;
3131

32-
private $store;
33-
private $key;
34-
private $ttl;
35-
private $autoRelease;
36-
private $dirty = false;
32+
private PersistingStoreInterface $store;
33+
private Key $key;
34+
private ?float $ttl;
35+
private bool $autoRelease;
36+
private bool $dirty = false;
3737

3838
/**
3939
* @param float|null $ttl Maximum expected lock duration in seconds

LockFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LockFactory implements LoggerAwareInterface
2525
{
2626
use LoggerAwareTrait;
2727

28-
private $store;
28+
private PersistingStoreInterface $store;
2929

3030
public function __construct(PersistingStoreInterface $store)
3131
{

Store/CombinedStore.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ class CombinedStore implements SharedLockStoreInterface, LoggerAwareInterface
3232
use LoggerAwareTrait;
3333

3434
/** @var PersistingStoreInterface[] */
35-
private $stores;
36-
/** @var StrategyInterface */
37-
private $strategy;
35+
private array $stores;
36+
private StrategyInterface $strategy;
3837
/** @var SharedLockStoreInterface[] */
39-
private $sharedLockStores;
38+
private array $sharedLockStores;
4039

4140
/**
4241
* @param PersistingStoreInterface[] $stores The list of synchronized stores

Store/FlockStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
class FlockStore implements BlockingStoreInterface, SharedLockStoreInterface
3232
{
33-
private $lockPath;
33+
private ?string $lockPath;
3434

3535
/**
3636
* @param string|null $lockPath the directory to store the lock, defaults to the system's temporary directory

Store/InMemoryStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
class InMemoryStore implements SharedLockStoreInterface
2525
{
26-
private $locks = [];
27-
private $readLocks = [];
26+
private array $locks = [];
27+
private array $readLocks = [];
2828

2929
public function save(Key $key)
3030
{

Store/MemcachedStore.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ class MemcachedStore implements PersistingStoreInterface
2626
{
2727
use ExpiringStoreTrait;
2828

29-
private $memcached;
30-
private $initialTtl;
31-
/** @var bool */
32-
private $useExtendedReturn;
29+
private \Memcached $memcached;
30+
private int $initialTtl;
31+
private bool $useExtendedReturn;
3332

3433
public static function isSupported()
3534
{
@@ -151,11 +150,7 @@ private function getUniqueToken(Key $key): string
151150

152151
private function getValueAndCas(Key $key): array
153152
{
154-
if (null === $this->useExtendedReturn) {
155-
$this->useExtendedReturn = version_compare(phpversion('memcached'), '2.9.9', '>');
156-
}
157-
158-
if ($this->useExtendedReturn) {
153+
if ($this->useExtendedReturn ??= version_compare(phpversion('memcached'), '2.9.9', '>')) {
159154
$extendedReturn = $this->memcached->get((string) $key, null, \Memcached::GET_EXTENDED);
160155
if (\Memcached::GET_ERROR_RETURN_VALUE === $extendedReturn) {
161156
return [$extendedReturn, 0.0];

Store/MongoDbStore.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class MongoDbStore implements PersistingStoreInterface
4949
{
5050
use ExpiringStoreTrait;
5151

52-
private $collection;
53-
private $client;
54-
private $uri;
55-
private $options;
56-
private $initialTtl;
52+
private Collection $collection;
53+
private Client $client;
54+
private string $uri;
55+
private array $options;
56+
private float $initialTtl;
5757

5858
/**
5959
* @param Collection|Client|string $mongo An instance of a Collection or Client or URI @see https://docs.mongodb.com/manual/reference/connection-string/
@@ -322,13 +322,11 @@ private function isDuplicateKeyException(WriteException $e): bool
322322

323323
private function getCollection(): Collection
324324
{
325-
if (null !== $this->collection) {
325+
if (isset($this->collection)) {
326326
return $this->collection;
327327
}
328328

329-
if (null === $this->client) {
330-
$this->client = new Client($this->uri, $this->options['uriOptions'], $this->options['driverOptions']);
331-
}
329+
$this->client ??= new Client($this->uri, $this->options['uriOptions'], $this->options['driverOptions']);
332330

333331
$this->collection = $this->client->selectCollection(
334332
$this->options['database'],

Store/PdoStore.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ class PdoStore implements PersistingStoreInterface
4040
{
4141
use ExpiringStoreTrait;
4242

43-
private $conn;
44-
private $dsn;
45-
private $driver;
46-
private $table = 'lock_keys';
47-
private $idCol = 'key_id';
48-
private $tokenCol = 'key_token';
49-
private $expirationCol = 'key_expiration';
50-
private $username = '';
51-
private $password = '';
52-
private $connectionOptions = [];
53-
private $gcProbability;
54-
private $initialTtl;
43+
private \PDO|Connection $conn;
44+
private string $dsn;
45+
private string $driver;
46+
private string $table = 'lock_keys';
47+
private string $idCol = 'key_id';
48+
private string $tokenCol = 'key_token';
49+
private string $expirationCol = 'key_expiration';
50+
private string $username = '';
51+
private string $password = '';
52+
private array $connectionOptions = [];
53+
private float $gcProbability;
54+
private int $initialTtl;
5555

5656
/**
5757
* You can either pass an existing database connection as PDO instance or
@@ -232,7 +232,7 @@ private function getUniqueToken(Key $key): string
232232

233233
private function getConnection(): \PDO|Connection
234234
{
235-
if (null === $this->conn) {
235+
if (!isset($this->conn)) {
236236
if (strpos($this->dsn, '://')) {
237237
if (!class_exists(DriverManager::class)) {
238238
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
@@ -336,7 +336,7 @@ private function prune(): void
336336

337337
private function getDriver(): string
338338
{
339-
if (null !== $this->driver) {
339+
if (isset($this->driver)) {
340340
return $this->driver;
341341
}
342342

Store/PostgreSqlStore.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
*/
2929
class PostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStoreInterface
3030
{
31-
private $conn;
32-
private $dsn;
33-
private $username = '';
34-
private $password = '';
35-
private $connectionOptions = [];
36-
private static $storeRegistry = [];
31+
private \PDO|Connection $conn;
32+
private string $dsn;
33+
private string $username = '';
34+
private string $password = '';
35+
private array $connectionOptions = [];
36+
private static array $storeRegistry = [];
3737

3838
/**
3939
* You can either pass an existing database connection as PDO instance or
@@ -234,7 +234,7 @@ private function unlockShared(Key $key): void
234234

235235
private function getConnection(): \PDO|Connection
236236
{
237-
if (null === $this->conn) {
237+
if (!isset($this->conn)) {
238238
if (strpos($this->dsn, '://')) {
239239
if (!class_exists(DriverManager::class)) {
240240
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));

Store/RedisStore.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class RedisStore implements SharedLockStoreInterface
3131
{
3232
use ExpiringStoreTrait;
3333

34-
private $redis;
35-
private $initialTtl;
36-
private $supportTime;
34+
private \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis;
35+
private float $initialTtl;
36+
private bool $supportTime;
3737

3838
/**
3939
* @param float $initialTtl The expiration delay of locks in seconds
@@ -293,7 +293,7 @@ private function getUniqueToken(Key $key): string
293293

294294
private function getNowCode(): string
295295
{
296-
if (null === $this->supportTime) {
296+
if (!isset($this->supportTime)) {
297297
// Redis < 5.0 does not support TIME (not deterministic) in script.
298298
// https://redis.io/commands/eval#replicating-commands-instead-of-scripts
299299
// This code asserts TIME can be use, otherwise will fallback to a timestamp generated by the PHP process.

Store/ZookeeperStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ZookeeperStore implements PersistingStoreInterface
2727
{
2828
use ExpiringStoreTrait;
2929

30-
private $zookeeper;
30+
private \Zookeeper $zookeeper;
3131

3232
public function __construct(\Zookeeper $zookeeper)
3333
{

0 commit comments

Comments
 (0)