Skip to content

Commit 35526ae

Browse files
committed
Improve CS
1 parent 22d4b42 commit 35526ae

34 files changed

+252
-280
lines changed

.php-cs-fixer.dist.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232
'equal' => false,
3333
'identical' => false,
3434
],
35-
'native_constant_invocation' => true,
35+
'native_constant_invocation' => [
36+
'include' => [
37+
// https://github.com/php/php-src/blob/php-8.4.0/ext/pcntl/pcntl.stub.php#L201
38+
'SIGALRM',
39+
],
40+
],
3641
'native_function_invocation' => false,
3742
'void_return' => false,
3843
'blank_line_before_statement' => [
@@ -57,18 +62,12 @@
5762
// also prevent bounding of unwanted variables for GC
5863
'use_arrow_functions' => false,
5964

60-
// TODO disable too strict rules for now - remove once the CS is updated
65+
// TODO disable too strict rules for now
6166
'declare_strict_types' => false,
62-
'fully_qualified_strict_types' => false,
6367
'general_phpdoc_annotation_remove' => false,
64-
'global_namespace_import' => false,
65-
'increment_style' => false,
66-
'native_constant_invocation' => false,
67-
'no_useless_else' => false,
6868
'php_unit_data_provider_static' => false,
6969
'php_unit_strict' => false,
7070
'phpdoc_to_comment' => false,
71-
'static_lambda' => false,
7271
'strict_comparison' => false,
7372
])
7473
->setFinder($finder)

src/exception/DeadlineException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace malkusch\lock\exception;
66

7-
use RuntimeException;
8-
97
/**
108
* Deadline exception.
119
*/
12-
class DeadlineException extends RuntimeException implements PhpLockException {}
10+
class DeadlineException extends \RuntimeException implements PhpLockException {}

src/exception/ExecutionOutsideLockException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class ExecutionOutsideLockException extends LockReleaseException
2121
/**
2222
* Creates a new instance of the ExecutionOutsideLockException class.
2323
*
24-
* @param float $elapsedTime total elapsed time of the synchronized code
25-
* callback execution
24+
* @param float $elapsedTime total elapsed time of the synchronized code callback execution
2625
* @param float $timeout the lock timeout in seconds
2726
*
2827
* @return self execution outside lock exception

src/exception/LockReleaseException.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace malkusch\lock\exception;
66

7-
use Throwable;
8-
97
/**
108
* Lock release exception.
119
*
@@ -59,10 +57,9 @@ public function setCodeResult($codeResult): self
5957
* Gets the exception that has happened during the synchronized code
6058
* execution.
6159
*
62-
* @return \Throwable|null the exception thrown by the code block or null
63-
* when there has been no exception
60+
* @return \Throwable|null the exception thrown by the code block or null when there has been no exception
6461
*/
65-
public function getCodeException(): ?Throwable
62+
public function getCodeException(): ?\Throwable
6663
{
6764
return $this->codeException;
6865
}
@@ -75,7 +72,7 @@ public function getCodeException(): ?Throwable
7572
*
7673
* @return self current lock release exception instance
7774
*/
78-
public function setCodeException(Throwable $codeException): self
75+
public function setCodeException(\Throwable $codeException): self
7976
{
8077
$this->codeException = $codeException;
8178

src/exception/MutexException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
namespace malkusch\lock\exception;
66

7-
use RuntimeException;
8-
97
/**
108
* Mutex exception.
119
*
1210
* Generic exception for any other not covered reason. Usually extended by
1311
* child classes.
1412
*/
15-
class MutexException extends RuntimeException implements PhpLockException
13+
class MutexException extends \RuntimeException implements PhpLockException
1614
{
1715
/**
1816
* @var int not enough redis servers

src/mutex/CASMutex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public function notify(): void
5858
* Example:
5959
* <code>
6060
* $mutex = new CASMutex();
61-
* $mutex->synchronized(function () use ($memcached, $mutex, $amount) {
62-
* $balance = $memcached->get("balance", null, $casToken);
61+
* $mutex->synchronized(static function () use ($memcached, $mutex, $amount) {
62+
* $balance = $memcached->get('balance', null, $casToken);
6363
* $balance -= $amount;
64-
* if (!$memcached->cas($casToken, "balance", $balance)) {
64+
* if (!$memcached->cas($casToken, 'balance', $balance)) {
6565
* return;
6666
*
6767
* }

src/mutex/FlockMutex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function determineLockingStrategy(): int
8585
*/
8686
private function lockBlocking(): void
8787
{
88-
if (!flock($this->fileHandle, LOCK_EX)) {
88+
if (!flock($this->fileHandle, \LOCK_EX)) {
8989
throw new LockAcquireException('Failed to lock the file.');
9090
}
9191
}
@@ -130,7 +130,7 @@ private function lockBusy()
130130
*/
131131
private function acquireNonBlockingLock(): bool
132132
{
133-
if (!flock($this->fileHandle, LOCK_EX | LOCK_NB, $wouldBlock)) {
133+
if (!flock($this->fileHandle, \LOCK_EX | \LOCK_NB, $wouldBlock)) {
134134
if ($wouldBlock) {
135135
// Another process holds the lock.
136136
return false;
@@ -171,7 +171,7 @@ protected function lock(): void
171171
*/
172172
protected function unlock(): void
173173
{
174-
if (!flock($this->fileHandle, LOCK_UN)) {
174+
if (!flock($this->fileHandle, \LOCK_UN)) {
175175
throw new LockReleaseException('Failed to unlock the file.');
176176
}
177177
}

src/mutex/LockMutex.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace malkusch\lock\mutex;
66

7+
use malkusch\lock\exception\LockAcquireException;
78
use malkusch\lock\exception\LockReleaseException;
8-
use Throwable;
99

1010
/**
1111
* Locking mutex.
@@ -19,16 +19,14 @@ abstract class LockMutex extends Mutex
1919
*
2020
* This method blocks until the lock was acquired.
2121
*
22-
* @throws \malkusch\lock\exception\LockAcquireException the lock could not
23-
* be acquired
22+
* @throws LockAcquireException the lock could not be acquired
2423
*/
2524
abstract protected function lock(): void;
2625

2726
/**
2827
* Releases the lock.
2928
*
30-
* @throws \malkusch\lock\exception\LockReleaseException the lock could not
31-
* be released
29+
* @throws LockReleaseException the lock could not be released
3230
*/
3331
abstract protected function unlock(): void;
3432

@@ -40,7 +38,7 @@ public function synchronized(callable $code)
4038
$codeException = null;
4139
try {
4240
$codeResult = $code();
43-
} catch (Throwable $exception) {
41+
} catch (\Throwable $exception) {
4442
$codeException = $exception;
4543

4644
throw $exception;

src/mutex/MemcachedMutex.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class MemcachedMutex extends SpinlockMutex
1313
{
1414
/**
15-
* @var Memcached the connected Memcached API
15+
* @var \Memcached the connected Memcached API
1616
*/
1717
private $memcache;
1818

@@ -22,13 +22,13 @@ class MemcachedMutex extends SpinlockMutex
2222
* The Memcached API needs to have at least one server in its pool. I.e.
2323
* it has to be added with Memcached::addServer().
2424
*
25-
* @param string $name the lock name
26-
* @param Memcached $memcache the connected Memcached API
27-
* @param float $timeout the time in seconds a lock expires, default is 3
25+
* @param string $name the lock name
26+
* @param \Memcached $memcache the connected Memcached API
27+
* @param float $timeout the time in seconds a lock expires, default is 3
2828
*
2929
* @throws \LengthException the timeout must be greater than 0
3030
*/
31-
public function __construct(string $name, Memcached $memcache, float $timeout = 3)
31+
public function __construct(string $name, \Memcached $memcache, float $timeout = 3)
3232
{
3333
parent::__construct($name, $timeout);
3434

src/mutex/Mutex.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace malkusch\lock\mutex;
66

7+
use malkusch\lock\exception\ExecutionOutsideLockException;
8+
use malkusch\lock\exception\LockAcquireException;
9+
use malkusch\lock\exception\LockReleaseException;
710
use malkusch\lock\util\DoubleCheckedLocking;
811

912
/**
@@ -27,13 +30,10 @@ abstract class Mutex
2730
*
2831
* @return T the return value of the execution callback
2932
*
30-
* @throws \Exception the execution callback threw an exception
31-
* @throws \malkusch\lock\exception\LockAcquireException the mutex could not
32-
* be acquired, no further side effects
33-
* @throws \malkusch\lock\exception\LockReleaseException the mutex could not
34-
* be released, the code was already executed
35-
* @throws \malkusch\lock\exception\ExecutionOutsideLockException some code
36-
* has been executed outside of the lock
33+
* @throws \Exception the execution callback threw an exception
34+
* @throws LockAcquireException the mutex could not be acquired, no further side effects
35+
* @throws LockReleaseException the mutex could not be released, the code was already executed
36+
* @throws ExecutionOutsideLockException some code has been executed outside of the lock
3737
*/
3838
abstract public function synchronized(callable $code);
3939

@@ -45,19 +45,17 @@ abstract public function synchronized(callable $code);
4545
*
4646
* Example:
4747
* <code>
48-
* $result = $mutex->check(function () use ($bankAccount, $amount) {
48+
* $result = $mutex->check(static function () use ($bankAccount, $amount) {
4949
* return $bankAccount->getBalance() >= $amount;
50-
* })->then(function () use ($bankAccount, $amount) {
50+
* })->then(static function () use ($bankAccount, $amount) {
5151
* return $bankAccount->withdraw($amount);
5252
* });
5353
* </code>
5454
*
55-
* @param callable(): bool $check callback that decides if the lock should be
56-
* acquired and if the synchronized callback should be executed after
57-
* acquiring the lock
55+
* @param callable(): bool $check callback that decides if the lock should be acquired and if the synchronized
56+
* callback should be executed after acquiring the lock
5857
*
59-
* @return \malkusch\lock\util\DoubleCheckedLocking the double-checked
60-
* locking pattern
58+
* @return DoubleCheckedLocking the double-checked locking pattern
6159
*/
6260
public function check(callable $check): DoubleCheckedLocking
6361
{

0 commit comments

Comments
 (0)