Skip to content

Commit

Permalink
Improve CS
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 3, 2024
1 parent 22d4b42 commit 35526ae
Show file tree
Hide file tree
Showing 34 changed files with 252 additions and 280 deletions.
15 changes: 7 additions & 8 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
'equal' => false,
'identical' => false,
],
'native_constant_invocation' => true,
'native_constant_invocation' => [
'include' => [
// https://github.com/php/php-src/blob/php-8.4.0/ext/pcntl/pcntl.stub.php#L201
'SIGALRM',
],
],
'native_function_invocation' => false,
'void_return' => false,
'blank_line_before_statement' => [
Expand All @@ -57,18 +62,12 @@
// also prevent bounding of unwanted variables for GC
'use_arrow_functions' => false,

// TODO disable too strict rules for now - remove once the CS is updated
// TODO disable too strict rules for now
'declare_strict_types' => false,
'fully_qualified_strict_types' => false,
'general_phpdoc_annotation_remove' => false,
'global_namespace_import' => false,
'increment_style' => false,
'native_constant_invocation' => false,
'no_useless_else' => false,
'php_unit_data_provider_static' => false,
'php_unit_strict' => false,
'phpdoc_to_comment' => false,
'static_lambda' => false,
'strict_comparison' => false,
])
->setFinder($finder)
Expand Down
4 changes: 1 addition & 3 deletions src/exception/DeadlineException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace malkusch\lock\exception;

use RuntimeException;

/**
* Deadline exception.
*/
class DeadlineException extends RuntimeException implements PhpLockException {}
class DeadlineException extends \RuntimeException implements PhpLockException {}
3 changes: 1 addition & 2 deletions src/exception/ExecutionOutsideLockException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class ExecutionOutsideLockException extends LockReleaseException
/**
* Creates a new instance of the ExecutionOutsideLockException class.
*
* @param float $elapsedTime total elapsed time of the synchronized code
* callback execution
* @param float $elapsedTime total elapsed time of the synchronized code callback execution
* @param float $timeout the lock timeout in seconds
*
* @return self execution outside lock exception
Expand Down
9 changes: 3 additions & 6 deletions src/exception/LockReleaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace malkusch\lock\exception;

use Throwable;

/**
* Lock release exception.
*
Expand Down Expand Up @@ -59,10 +57,9 @@ public function setCodeResult($codeResult): self
* Gets the exception that has happened during the synchronized code
* execution.
*
* @return \Throwable|null the exception thrown by the code block or null
* when there has been no exception
* @return \Throwable|null the exception thrown by the code block or null when there has been no exception
*/
public function getCodeException(): ?Throwable
public function getCodeException(): ?\Throwable
{
return $this->codeException;
}
Expand All @@ -75,7 +72,7 @@ public function getCodeException(): ?Throwable
*
* @return self current lock release exception instance
*/
public function setCodeException(Throwable $codeException): self
public function setCodeException(\Throwable $codeException): self
{
$this->codeException = $codeException;

Expand Down
4 changes: 1 addition & 3 deletions src/exception/MutexException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace malkusch\lock\exception;

use RuntimeException;

/**
* Mutex exception.
*
* Generic exception for any other not covered reason. Usually extended by
* child classes.
*/
class MutexException extends RuntimeException implements PhpLockException
class MutexException extends \RuntimeException implements PhpLockException
{
/**
* @var int not enough redis servers
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/CASMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function notify(): void
* Example:
* <code>
* $mutex = new CASMutex();
* $mutex->synchronized(function () use ($memcached, $mutex, $amount) {
* $balance = $memcached->get("balance", null, $casToken);
* $mutex->synchronized(static function () use ($memcached, $mutex, $amount) {
* $balance = $memcached->get('balance', null, $casToken);
* $balance -= $amount;
* if (!$memcached->cas($casToken, "balance", $balance)) {
* if (!$memcached->cas($casToken, 'balance', $balance)) {
* return;
*
* }
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/FlockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function determineLockingStrategy(): int
*/
private function lockBlocking(): void
{
if (!flock($this->fileHandle, LOCK_EX)) {
if (!flock($this->fileHandle, \LOCK_EX)) {
throw new LockAcquireException('Failed to lock the file.');
}
}
Expand Down Expand Up @@ -130,7 +130,7 @@ private function lockBusy()
*/
private function acquireNonBlockingLock(): bool
{
if (!flock($this->fileHandle, LOCK_EX | LOCK_NB, $wouldBlock)) {
if (!flock($this->fileHandle, \LOCK_EX | \LOCK_NB, $wouldBlock)) {
if ($wouldBlock) {
// Another process holds the lock.
return false;
Expand Down Expand Up @@ -171,7 +171,7 @@ protected function lock(): void
*/
protected function unlock(): void
{
if (!flock($this->fileHandle, LOCK_UN)) {
if (!flock($this->fileHandle, \LOCK_UN)) {
throw new LockReleaseException('Failed to unlock the file.');
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/mutex/LockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace malkusch\lock\mutex;

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use Throwable;

/**
* Locking mutex.
Expand All @@ -19,16 +19,14 @@ abstract class LockMutex extends Mutex
*
* This method blocks until the lock was acquired.
*
* @throws \malkusch\lock\exception\LockAcquireException the lock could not
* be acquired
* @throws LockAcquireException the lock could not be acquired
*/
abstract protected function lock(): void;

/**
* Releases the lock.
*
* @throws \malkusch\lock\exception\LockReleaseException the lock could not
* be released
* @throws LockReleaseException the lock could not be released
*/
abstract protected function unlock(): void;

Expand All @@ -40,7 +38,7 @@ public function synchronized(callable $code)
$codeException = null;
try {
$codeResult = $code();
} catch (Throwable $exception) {
} catch (\Throwable $exception) {
$codeException = $exception;

throw $exception;
Expand Down
10 changes: 5 additions & 5 deletions src/mutex/MemcachedMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class MemcachedMutex extends SpinlockMutex
{
/**
* @var Memcached the connected Memcached API
* @var \Memcached the connected Memcached API
*/
private $memcache;

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

Expand Down
26 changes: 12 additions & 14 deletions src/mutex/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace malkusch\lock\mutex;

use malkusch\lock\exception\ExecutionOutsideLockException;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\util\DoubleCheckedLocking;

/**
Expand All @@ -27,13 +30,10 @@ abstract class Mutex
*
* @return T the return value of the execution callback
*
* @throws \Exception the execution callback threw an exception
* @throws \malkusch\lock\exception\LockAcquireException the mutex could not
* be acquired, no further side effects
* @throws \malkusch\lock\exception\LockReleaseException the mutex could not
* be released, the code was already executed
* @throws \malkusch\lock\exception\ExecutionOutsideLockException some code
* has been executed outside of the lock
* @throws \Exception the execution callback threw an exception
* @throws LockAcquireException the mutex could not be acquired, no further side effects
* @throws LockReleaseException the mutex could not be released, the code was already executed
* @throws ExecutionOutsideLockException some code has been executed outside of the lock
*/
abstract public function synchronized(callable $code);

Expand All @@ -45,19 +45,17 @@ abstract public function synchronized(callable $code);
*
* Example:
* <code>
* $result = $mutex->check(function () use ($bankAccount, $amount) {
* $result = $mutex->check(static function () use ($bankAccount, $amount) {
* return $bankAccount->getBalance() >= $amount;
* })->then(function () use ($bankAccount, $amount) {
* })->then(static function () use ($bankAccount, $amount) {
* return $bankAccount->withdraw($amount);
* });
* </code>
*
* @param callable(): bool $check callback that decides if the lock should be
* acquired and if the synchronized callback should be executed after
* acquiring the lock
* @param callable(): bool $check callback that decides if the lock should be acquired and if the synchronized
* callback should be executed after acquiring the lock
*
* @return \malkusch\lock\util\DoubleCheckedLocking the double-checked
* locking pattern
* @return DoubleCheckedLocking the double-checked locking pattern
*/
public function check(callable $check): DoubleCheckedLocking
{
Expand Down
3 changes: 1 addition & 2 deletions src/mutex/MySQLMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace malkusch\lock\mutex;

use InvalidArgumentException;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\TimeoutException;

Expand All @@ -29,7 +28,7 @@ public function __construct(\PDO $PDO, string $name, float $timeout = 0)
$this->pdo = $PDO;

if (\strlen($name) > 64) {
throw new InvalidArgumentException('The maximum length of the lock name is 64 characters.');
throw new \InvalidArgumentException('The maximum length of the lock name is 64 characters.');
}

$this->name = $name;
Expand Down
12 changes: 5 additions & 7 deletions src/mutex/PHPRedisMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use Redis;
use RedisException;

/**
* Mutex based on the Redlock algorithm using the phpredis extension.
Expand All @@ -29,8 +28,7 @@ class PHPRedisMutex extends RedisMutex
*
* @param array<\Redis|\RedisCluster> $redisAPIs the Redis connections
* @param string $name the lock name
* @param float $timeout The time in seconds a lock expires after. Default is
* 3 seconds.
* @param float $timeout The time in seconds a lock expires after. Default is 3 seconds.
*
* @throws \LengthException the timeout must be greater than 0
*/
Expand All @@ -52,7 +50,7 @@ protected function add($redisAPI, string $key, string $value, float $expire): bo
try {
// Will set the key, if it doesn't exist, with a ttl of $expire seconds
return $redisAPI->set($key, $value, ['nx', 'px' => $expireMillis]);
} catch (RedisException $e) {
} catch (\RedisException $e) {
$message = sprintf(
"Failed to acquire lock for key '%s'",
$key
Expand All @@ -69,7 +67,7 @@ protected function add($redisAPI, string $key, string $value, float $expire): bo
*/
protected function evalScript($redis, string $script, int $numkeys, array $arguments)
{
for ($i = $numkeys; $i < count($arguments); $i++) {
for ($i = $numkeys; $i < count($arguments); ++$i) {
/*
* If a serialization mode such as "php" or "igbinary" is enabled, the arguments must be
* serialized by us, because phpredis does not do this for the eval command.
Expand All @@ -89,7 +87,7 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum

try {
return $redis->eval($script, $arguments, $numkeys);
} catch (RedisException $e) {
} catch (\RedisException $e) {
throw new LockReleaseException('Failed to release lock', 0, $e);
}
}
Expand All @@ -107,6 +105,6 @@ private function hasLzfCompression($redis): bool
return false;
}

return $redis->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZF;
return $redis->getOption(\Redis::OPT_COMPRESSION) === \Redis::COMPRESSION_LZF;
}
}
4 changes: 1 addition & 3 deletions src/mutex/PgAdvisoryLockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace malkusch\lock\mutex;

use RuntimeException;

class PgAdvisoryLockMutex extends LockMutex
{
/**
Expand Down Expand Up @@ -33,7 +31,7 @@ public function __construct(\PDO $PDO, string $name)
$hashed_name = hash('sha256', $name, true);

if ($hashed_name === false) { // @phpstan-ignore-line
throw new RuntimeException('Unable to hash the key, sha256 algorithm is not supported.');
throw new \RuntimeException('Unable to hash the key, sha256 algorithm is not supported.');
}

[$bytes1, $bytes2] = str_split($hashed_name, 4);
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/RedisMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function acquire(string $key, float $expire): bool
foreach ($this->redisAPIs as $index => $redisAPI) {
try {
if ($this->add($redisAPI, $key, $this->token, $expire)) {
$acquired++;
++$acquired;
}
} catch (LockAcquireException $exception) {
// todo if there is only one redis server, throw immediately.
Expand All @@ -71,7 +71,7 @@ protected function acquire(string $key, float $expire): bool
];
$this->logger->warning('Could not set {key} = {token} at server #{index}.', $context);

$errored++;
++$errored;
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ protected function release(string $key): bool
foreach ($this->redisAPIs as $index => $redisAPI) {
try {
if ($this->evalScript($redisAPI, $script, 1, [$key, $this->token])) {
$released++;
++$released;
}
} catch (LockReleaseException $e) {
// todo throw if there is only one redis server
Expand Down
3 changes: 1 addition & 2 deletions src/mutex/SemaphoreMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace malkusch\lock\mutex;

use InvalidArgumentException;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;

Expand Down Expand Up @@ -36,7 +35,7 @@ class SemaphoreMutex extends LockMutex
public function __construct($semaphore)
{
if (!$semaphore instanceof \SysvSemaphore && !is_resource($semaphore)) {
throw new InvalidArgumentException('The semaphore id is not a valid resource.');
throw new \InvalidArgumentException('The semaphore id is not a valid resource.');
}
$this->semaphore = $semaphore;
}
Expand Down
Loading

0 comments on commit 35526ae

Please sign in to comment.