Skip to content

Commit 836e1d1

Browse files
Add types to private and internal properties
1 parent 67419c6 commit 836e1d1

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

Lock.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\LoggerAwareInterface;
1515
use Psr\Log\LoggerAwareTrait;
16-
use Psr\Log\NullLogger;
1716
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1817
use Symfony\Component\Lock\Exception\LockAcquiringException;
1918
use Symfony\Component\Lock\Exception\LockConflictedException;
@@ -29,24 +28,18 @@ final class Lock implements SharedLockInterface, LoggerAwareInterface
2928
{
3029
use LoggerAwareTrait;
3130

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

3833
/**
3934
* @param float|null $ttl Maximum expected lock duration in seconds
4035
* @param bool $autoRelease Whether to automatically release the lock or not when the lock instance is destroyed
4136
*/
42-
public function __construct(Key $key, PersistingStoreInterface $store, float $ttl = null, bool $autoRelease = true)
43-
{
44-
$this->store = $store;
45-
$this->key = $key;
46-
$this->ttl = $ttl;
47-
$this->autoRelease = $autoRelease;
48-
49-
$this->logger = new NullLogger();
37+
public function __construct(
38+
private Key $key,
39+
private PersistingStoreInterface $store,
40+
private ?float $ttl = null,
41+
private bool $autoRelease = true,
42+
) {
5043
}
5144

5245
public function __sleep(): array
@@ -93,7 +86,7 @@ public function acquire(bool $blocking = false): bool
9386
}
9487

9588
$this->dirty = true;
96-
$this->logger->debug('Successfully acquired the "{resource}" lock.', ['resource' => $this->key]);
89+
$this->logger?->debug('Successfully acquired the "{resource}" lock.', ['resource' => $this->key]);
9790

9891
if ($this->ttl) {
9992
$this->refresh();
@@ -111,15 +104,15 @@ public function acquire(bool $blocking = false): bool
111104
return true;
112105
} catch (LockConflictedException $e) {
113106
$this->dirty = false;
114-
$this->logger->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
107+
$this->logger?->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
115108

116109
if ($blocking) {
117110
throw $e;
118111
}
119112

120113
return false;
121114
} catch (\Exception $e) {
122-
$this->logger->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
115+
$this->logger?->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
123116
throw new LockAcquiringException(sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
124117
}
125118
}
@@ -129,7 +122,7 @@ public function acquireRead(bool $blocking = false): bool
129122
$this->key->resetLifetime();
130123
try {
131124
if (!$this->store instanceof SharedLockStoreInterface) {
132-
$this->logger->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);
125+
$this->logger?->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);
133126

134127
return $this->acquire($blocking);
135128
}
@@ -151,7 +144,7 @@ public function acquireRead(bool $blocking = false): bool
151144
}
152145

153146
$this->dirty = true;
154-
$this->logger->debug('Successfully acquired the "{resource}" lock for reading.', ['resource' => $this->key]);
147+
$this->logger?->debug('Successfully acquired the "{resource}" lock for reading.', ['resource' => $this->key]);
155148

156149
if ($this->ttl) {
157150
$this->refresh();
@@ -169,15 +162,15 @@ public function acquireRead(bool $blocking = false): bool
169162
return true;
170163
} catch (LockConflictedException $e) {
171164
$this->dirty = false;
172-
$this->logger->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
165+
$this->logger?->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
173166

174167
if ($blocking) {
175168
throw $e;
176169
}
177170

178171
return false;
179172
} catch (\Exception $e) {
180-
$this->logger->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
173+
$this->logger?->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
181174
throw new LockAcquiringException(sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
182175
}
183176
}
@@ -202,13 +195,13 @@ public function refresh(float $ttl = null): void
202195
throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $this->key));
203196
}
204197

205-
$this->logger->debug('Expiration defined for "{resource}" lock for "{ttl}" seconds.', ['resource' => $this->key, 'ttl' => $ttl]);
198+
$this->logger?->debug('Expiration defined for "{resource}" lock for "{ttl}" seconds.', ['resource' => $this->key, 'ttl' => $ttl]);
206199
} catch (LockConflictedException $e) {
207200
$this->dirty = false;
208-
$this->logger->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', ['resource' => $this->key]);
201+
$this->logger?->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', ['resource' => $this->key]);
209202
throw $e;
210203
} catch (\Exception $e) {
211-
$this->logger->notice('Failed to define an expiration for the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
204+
$this->logger?->notice('Failed to define an expiration for the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
212205
throw new LockAcquiringException(sprintf('Failed to define an expiration for the "%s" lock.', $this->key), 0, $e);
213206
}
214207
}
@@ -234,9 +227,9 @@ public function release(): void
234227
throw new LockReleasingException(sprintf('Failed to release the "%s" lock, the resource is still locked.', $this->key));
235228
}
236229

237-
$this->logger->debug('Successfully released the "{resource}" lock.', ['resource' => $this->key]);
230+
$this->logger?->debug('Successfully released the "{resource}" lock.', ['resource' => $this->key]);
238231
} catch (LockReleasingException $e) {
239-
$this->logger->notice('Failed to release the "{resource}" lock.', ['resource' => $this->key]);
232+
$this->logger?->notice('Failed to release the "{resource}" lock.', ['resource' => $this->key]);
240233
throw $e;
241234
}
242235
}

LockFactory.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@ class LockFactory implements LoggerAwareInterface
2525
{
2626
use LoggerAwareTrait;
2727

28-
private PersistingStoreInterface $store;
29-
30-
public function __construct(PersistingStoreInterface $store)
31-
{
32-
$this->store = $store;
33-
34-
$this->logger = new NullLogger();
28+
public function __construct(
29+
private PersistingStoreInterface $store,
30+
) {
3531
}
3632

3733
/**
@@ -60,7 +56,9 @@ public function createLock(string $resource, ?float $ttl = 300.0, bool $autoRele
6056
public function createLockFromKey(Key $key, ?float $ttl = 300.0, bool $autoRelease = true): LockInterface
6157
{
6258
$lock = new Lock($key, $this->store, $ttl, $autoRelease);
63-
$lock->setLogger($this->logger);
59+
if ($this->logger) {
60+
$lock->setLogger($this->logger);
61+
}
6462

6563
return $lock;
6664
}

Store/CombinedStore.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\LoggerAwareInterface;
1515
use Psr\Log\LoggerAwareTrait;
16-
use Psr\Log\NullLogger;
1716
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1817
use Symfony\Component\Lock\Exception\LockConflictedException;
1918
use Symfony\Component\Lock\Key;
@@ -50,7 +49,6 @@ public function __construct(array $stores, StrategyInterface $strategy)
5049

5150
$this->stores = $stores;
5251
$this->strategy = $strategy;
53-
$this->logger = new NullLogger();
5452
}
5553

5654
/**
@@ -67,7 +65,7 @@ public function save(Key $key)
6765
$store->save($key);
6866
++$successCount;
6967
} catch (\Exception $e) {
70-
$this->logger->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
68+
$this->logger?->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
7169
++$failureCount;
7270
}
7371

@@ -82,7 +80,7 @@ public function save(Key $key)
8280
return;
8381
}
8482

85-
$this->logger->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
83+
$this->logger?->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
8684

8785
// clean up potential locks
8886
$this->delete($key);
@@ -108,7 +106,7 @@ public function saveRead(Key $key)
108106
}
109107
++$successCount;
110108
} catch (\Exception $e) {
111-
$this->logger->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
109+
$this->logger?->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
112110
++$failureCount;
113111
}
114112

@@ -123,7 +121,7 @@ public function saveRead(Key $key)
123121
return;
124122
}
125123

126-
$this->logger->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
124+
$this->logger?->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
127125

128126
// clean up potential locks
129127
$this->delete($key);
@@ -144,15 +142,15 @@ public function putOffExpiration(Key $key, float $ttl)
144142
foreach ($this->stores as $store) {
145143
try {
146144
if (0.0 >= $adjustedTtl = $expireAt - microtime(true)) {
147-
$this->logger->debug('Stores took to long to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'ttl' => $ttl]);
145+
$this->logger?->debug('Stores took to long to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'ttl' => $ttl]);
148146
$key->reduceLifetime(0);
149147
break;
150148
}
151149

152150
$store->putOffExpiration($key, $adjustedTtl);
153151
++$successCount;
154152
} catch (\Exception $e) {
155-
$this->logger->debug('One store failed to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
153+
$this->logger?->debug('One store failed to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
156154
++$failureCount;
157155
}
158156

@@ -167,7 +165,7 @@ public function putOffExpiration(Key $key, float $ttl)
167165
return;
168166
}
169167

170-
$this->logger->notice('Failed to define the expiration for the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
168+
$this->logger?->notice('Failed to define the expiration for the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
171169

172170
// clean up potential locks
173171
$this->delete($key);
@@ -184,7 +182,7 @@ public function delete(Key $key)
184182
try {
185183
$store->delete($key);
186184
} catch (\Exception $e) {
187-
$this->logger->notice('One store failed to delete the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
185+
$this->logger?->notice('One store failed to delete the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
188186
}
189187
}
190188
}
@@ -203,7 +201,7 @@ public function exists(Key $key): bool
203201
++$failureCount;
204202
}
205203
} catch (\Exception $e) {
206-
$this->logger->debug('One store failed to check the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
204+
$this->logger?->debug('One store failed to check the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
207205
++$failureCount;
208206
}
209207

Store/DoctrineDbalPostgreSqlStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
class DoctrineDbalPostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStoreInterface
3434
{
3535
private Connection $conn;
36-
private static $storeRegistry = [];
36+
private static array $storeRegistry = [];
3737

3838
/**
3939
* You can either pass an existing database connection a Doctrine DBAL Connection

0 commit comments

Comments
 (0)