Skip to content

Commit 3dc1509

Browse files
PHP 7.1 changes.
- Add argument and return type hints to all methods and anonymous functions - Remove usage of call_user_func() where possible - Update risky tests - Remove deprecated MemcacheMutex class - Change constructor of DoubleCheckedLocking to prevent initialization errors
1 parent 6ab5f6d commit 3dc1509

36 files changed

+367
-401
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.idea
12
/vendor
23
/composer.lock
34
/phpunit.xml

classes/mutex/CASMutex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ class CASMutex extends Mutex
3333
* @param int $timeout The timeout in seconds.
3434
* @throws \LengthException The timeout must be greater than 0.
3535
*/
36-
public function __construct($timeout = 3)
36+
public function __construct(int $timeout = 3)
3737
{
3838
$this->loop = new Loop($timeout);
3939
}
4040

4141
/**
4242
* Notifies the Mutex about a successful CAS operation.
4343
*/
44-
public function notify()
44+
public function notify(): void
4545
{
4646
$this->loop->end();
4747
}

classes/mutex/FlockMutex.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class FlockMutex extends LockMutex
5858
* @param resource $fileHandle The file handle.
5959
* @param int $timeout
6060
*/
61-
public function __construct($fileHandle, $timeout = self::INFINITE_TIMEOUT)
61+
public function __construct($fileHandle, int $timeout = self::INFINITE_TIMEOUT)
6262
{
6363
if (!is_resource($fileHandle)) {
6464
throw new \InvalidArgumentException("The file handle is not a valid resource.");
@@ -85,7 +85,7 @@ private function determineLockingStrategy()
8585
/**
8686
* @throws LockAcquireException
8787
*/
88-
private function lockBlocking()
88+
private function lockBlocking(): void
8989
{
9090
if (!flock($this->fileHandle, LOCK_EX)) {
9191
throw new LockAcquireException("Failed to lock the file.");
@@ -96,13 +96,13 @@ private function lockBlocking()
9696
* @throws LockAcquireException
9797
* @throws TimeoutException
9898
*/
99-
private function lockPcntl()
99+
private function lockPcntl(): void
100100
{
101101
$timebox = new PcntlTimeout($this->timeout);
102102

103103
try {
104104
$timebox->timeBoxed(
105-
function () {
105+
function (): void {
106106
$this->lockBlocking();
107107
}
108108
);
@@ -118,7 +118,7 @@ function () {
118118
private function lockBusy()
119119
{
120120
$loop = new Loop($this->timeout);
121-
$loop->execute(function () use ($loop) {
121+
$loop->execute(function () use ($loop): void {
122122
if ($this->acquireNonBlockingLock()) {
123123
$loop->end();
124124
}
@@ -129,7 +129,7 @@ private function lockBusy()
129129
* @return bool
130130
* @throws LockAcquireException
131131
*/
132-
private function acquireNonBlockingLock()
132+
private function acquireNonBlockingLock(): bool
133133
{
134134
if (!flock($this->fileHandle, LOCK_EX | LOCK_NB, $wouldBlock)) {
135135
if ($wouldBlock) {
@@ -147,7 +147,7 @@ private function acquireNonBlockingLock()
147147
* @throws LockAcquireException
148148
* @throws TimeoutException
149149
*/
150-
protected function lock()
150+
protected function lock(): void
151151
{
152152
switch ($this->strategy) {
153153
case self::STRATEGY_BLOCK:
@@ -167,7 +167,7 @@ protected function lock()
167167
/**
168168
* @throws LockReleaseException
169169
*/
170-
protected function unlock()
170+
protected function unlock(): void
171171
{
172172
if (!flock($this->fileHandle, LOCK_UN)) {
173173
throw new LockReleaseException("Failed to unlock the file.");

classes/mutex/LockMutex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ abstract class LockMutex extends Mutex
2323
*
2424
* @throws LockAcquireException The lock could not be acquired.
2525
*/
26-
abstract protected function lock();
26+
abstract protected function lock(): void;
2727

2828
/**
2929
* Releases the lock.
3030
*
3131
* @throws LockReleaseException The lock could not be released.
3232
*/
33-
abstract protected function unlock();
33+
abstract protected function unlock(): void;
3434

3535
public function synchronized(callable $code)
3636
{
3737
$this->lock();
3838
try {
39-
return call_user_func($code);
39+
return $code();
4040
} finally {
4141
$this->unlock();
4242
}

classes/mutex/MemcacheMutex.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

classes/mutex/MemcachedMutex.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,19 @@ class MemcachedMutex extends SpinlockMutex
3737
*
3838
* @throws \LengthException The timeout must be greater than 0.
3939
*/
40-
public function __construct($name, Memcached $memcache, $timeout = 3)
40+
public function __construct(string $name, Memcached $memcache, int $timeout = 3)
4141
{
4242
parent::__construct($name, $timeout);
4343

4444
$this->memcache = $memcache;
4545
}
4646

47-
/**
48-
* @internal
49-
*/
50-
protected function acquire($key, $expire)
47+
protected function acquire(string $key, int $expire): bool
5148
{
5249
return $this->memcache->add($key, true, $expire);
5350
}
5451

55-
/**
56-
* @internal
57-
*/
58-
protected function release($key)
52+
protected function release(string $key): bool
5953
{
6054
return $this->memcache->delete($key);
6155
}

classes/mutex/Mutex.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ abstract public function synchronized(callable $code);
5454
*
5555
* @return DoubleCheckedLocking The double-checked locking pattern.
5656
*/
57-
public function check(callable $check)
57+
public function check(callable $check): DoubleCheckedLocking
5858
{
59-
$doubleCheckedLocking = new DoubleCheckedLocking($this);
60-
$doubleCheckedLocking->setCheck($check);
61-
return $doubleCheckedLocking;
59+
return new DoubleCheckedLocking($this, $check);
6260
}
6361
}

classes/mutex/MySQLMutex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MySQLMutex extends LockMutex
2121
*/
2222
private $timeout;
2323

24-
public function __construct(\PDO $PDO, $name, $timeout = 0)
24+
public function __construct(\PDO $PDO, string $name, int $timeout = 0)
2525
{
2626
$this->pdo = $PDO;
2727

@@ -36,7 +36,7 @@ public function __construct(\PDO $PDO, $name, $timeout = 0)
3636
/**
3737
* @throws LockAcquireException
3838
*/
39-
public function lock()
39+
public function lock(): void
4040
{
4141
$statement = $this->pdo->prepare("SELECT GET_LOCK(?,?)");
4242

@@ -65,7 +65,7 @@ public function lock()
6565
throw TimeoutException::create($this->timeout);
6666
}
6767

68-
public function unlock()
68+
public function unlock(): void
6969
{
7070
$statement = $this->pdo->prepare("DO RELEASE_LOCK(?)");
7171
$statement->execute([

classes/mutex/NoMutex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
*/
1515
class NoMutex extends Mutex
1616
{
17-
17+
1818
public function synchronized(callable $code)
1919
{
20-
return call_user_func($code);
20+
return $code();
2121
}
2222
}

classes/mutex/PHPRedisMutex.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@ class PHPRedisMutex extends RedisMutex
3232
*
3333
* @throws \LengthException The timeout must be greater than 0.
3434
*/
35-
public function __construct(array $redisAPIs, $name, $timeout = 3)
35+
public function __construct(array $redisAPIs, string $name, int $timeout = 3)
3636
{
3737
parent::__construct($redisAPIs, $name, $timeout);
3838
}
3939

40-
/**
41-
* @internal
42-
*/
43-
protected function add($redis, $key, $value, $expire)
40+
protected function add($redis, string $key, string $value, int $expire): bool
4441
{
4542
/** @var Redis $redis */
4643
try {
@@ -56,10 +53,7 @@ protected function add($redis, $key, $value, $expire)
5653
}
5754
}
5855

59-
/**
60-
* @internal
61-
*/
62-
protected function evalScript($redis, $script, $numkeys, array $arguments)
56+
protected function evalScript($redis, string $script, int $numkeys, array $arguments)
6357
{
6458
/** @var Redis $redis */
6559

0 commit comments

Comments
 (0)