Skip to content

Commit

Permalink
cs static lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 3, 2024
1 parent 05f05c0 commit 8872ec1
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 109 deletions.
1 change: 0 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
'php_unit_data_provider_static' => false,
'php_unit_strict' => false,
'phpdoc_to_comment' => false,
'static_lambda' => false,
'strict_comparison' => false,
])
->setFinder($finder)
Expand Down
8 changes: 4 additions & 4 deletions tests/mutex/CASMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testExceedTimeout()
$this->expectException(LockAcquireException::class);

$mutex = new CASMutex(1);
$mutex->synchronized(function (): void {
$mutex->synchronized(static function (): void {
sleep(2);
});
}
Expand All @@ -48,7 +48,7 @@ public function testExceptionStopsIteration()
$this->expectException(\DomainException::class);

$mutex = new CASMutex();
$mutex->synchronized(function () {
$mutex->synchronized(static function () {
throw new \DomainException();
});
}
Expand All @@ -60,7 +60,7 @@ public function testNotify()
{
$i = 0;
$mutex = new CASMutex();
$mutex->synchronized(function () use ($mutex, &$i) {
$mutex->synchronized(static function () use ($mutex, &$i) {
$i++;
$mutex->notify();
});
Expand All @@ -74,7 +74,7 @@ public function testIteration()
{
$i = 0;
$mutex = new CASMutex();
$mutex->synchronized(function () use ($mutex, &$i): void {
$mutex->synchronized(static function () use ($mutex, &$i): void {
$i++;
if ($i > 1) {
$mutex->notify();
Expand Down
6 changes: 3 additions & 3 deletions tests/mutex/FlockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testCodeExecutedOutsideLockIsNotThrown(int $strategy)
{
$this->mutex->strategy = $strategy; // @phpstan-ignore-line

self::assertTrue($this->mutex->synchronized(function (): bool {
self::assertTrue($this->mutex->synchronized(static function (): bool {
usleep(1100 * 1000);

return true;
Expand All @@ -64,7 +64,7 @@ public function testTimeoutOccurs(int $strategy)

try {
$this->mutex->synchronized(
function () {
static function () {
self::fail('Did not expect code to be executed');
}
);
Expand Down Expand Up @@ -92,7 +92,7 @@ public function testNoTimeoutWaitsForever()

$timebox = new PcntlTimeout(1);
$timebox->timeBoxed(function () {
$this->mutex->synchronized(function (): void {
$this->mutex->synchronized(static function (): void {
self::fail('Did not expect code execution.');
});
});
Expand Down
14 changes: 7 additions & 7 deletions tests/mutex/LockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testLockFails()
->method('lock')
->willThrowException(new LockAcquireException());

$this->mutex->synchronized(function (): void {
$this->mutex->synchronized(static function (): void {
self::fail('Should not execute code.');
});
}
Expand All @@ -48,7 +48,7 @@ public function testUnlockAfterCode()
$this->mutex->expects(self::once())
->method('unlock');

$this->mutex->synchronized(function (): void {});
$this->mutex->synchronized(static function (): void {});
}

/**
Expand All @@ -60,7 +60,7 @@ public function testUnlockAfterException()
->method('unlock');

$this->expectException(\DomainException::class);
$this->mutex->synchronized(function () {
$this->mutex->synchronized(static function () {
throw new \DomainException();
});
}
Expand All @@ -76,7 +76,7 @@ public function testUnlockFailsAfterCode()
->method('unlock')
->willThrowException(new LockReleaseException());

$this->mutex->synchronized(function () {});
$this->mutex->synchronized(static function () {});
}

/**
Expand All @@ -90,7 +90,7 @@ public function testUnlockFailsAfterException()
->method('unlock')
->willThrowException(new LockReleaseException());

$this->mutex->synchronized(function () {
$this->mutex->synchronized(static function () {
throw new \DomainException();
});
}
Expand All @@ -105,7 +105,7 @@ public function testCodeResultAvailableAfterFailedUnlock()
->willThrowException(new LockReleaseException());

try {
$this->mutex->synchronized(function () {
$this->mutex->synchronized(static function () {
return 'result';
});
} catch (LockReleaseException $exception) {
Expand All @@ -124,7 +124,7 @@ public function testCodeExceptionAvailableAfterFailedUnlock()
->willThrowException(new LockReleaseException());

try {
$this->mutex->synchronized(function () {
$this->mutex->synchronized(static function () {
throw new \DomainException('Domain exception');
});
} catch (LockReleaseException $exception) {
Expand Down
4 changes: 2 additions & 2 deletions tests/mutex/MemcachedMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testFailAcquireLock()
->with('lock_test', true, 2)
->willReturn(false);

$this->mutex->synchronized(function (): void {
$this->mutex->synchronized(static function (): void {
self::fail('execution is not expected');
});
}
Expand All @@ -66,6 +66,6 @@ public function testFailReleasingLock()
->with('lock_test')
->willReturn(false);

$this->mutex->synchronized(function (): void {});
$this->mutex->synchronized(static function (): void {});
}
}
34 changes: 17 additions & 17 deletions tests/mutex/MutexConcurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public function testHighContention(callable $code, callable $mutexFactory)
$iterations = 1000 / $concurrency;
$timeout = $concurrency * 20;

$this->fork($concurrency, function () use ($mutexFactory, $timeout, $iterations, $code): void {
$this->fork($concurrency, static function () use ($mutexFactory, $timeout, $iterations, $code): void {
/** @var Mutex $mutex */
$mutex = $mutexFactory($timeout);
for ($i = 0; $i < $iterations; $i++) {
$mutex->synchronized(function () use ($code): void {
$mutex->synchronized(static function () use ($code): void {
$code(1);
});
}
Expand All @@ -110,15 +110,15 @@ public function testHighContention(callable $code, callable $mutexFactory)
*/
public function provideHighContentionCases(): iterable
{
$cases = array_map(function (array $mutexFactory): array {
$cases = array_map(static function (array $mutexFactory): array {
$filename = tempnam(sys_get_temp_dir(), 'php-lock-high-contention');

static::$temporaryFiles[] = $filename;

file_put_contents($filename, '0');

return [
function (int $increment) use ($filename): int {
static function (int $increment) use ($filename): int {
$counter = file_get_contents($filename);
$counter += $increment;

Expand Down Expand Up @@ -202,10 +202,10 @@ public function testExecutionIsSerializedWhenLocked(callable $mutexFactory)
{
$time = \microtime(true);

$this->fork(6, function () use ($mutexFactory): void {
$this->fork(6, static function () use ($mutexFactory): void {
/** @var Mutex $mutex */
$mutex = $mutexFactory();
$mutex->synchronized(function (): void {
$mutex->synchronized(static function (): void {
\usleep(200 * 1000);
});
});
Expand All @@ -226,29 +226,29 @@ public static function provideExecutionIsSerializedWhenLockedCases(): iterable
self::$temporaryFiles[] = $filename;

$cases = [
'flock' => [function ($timeout = 3) use ($filename): Mutex {
'flock' => [static function ($timeout = 3) use ($filename): Mutex {
$file = fopen($filename, 'w');

return new FlockMutex($file);
}],

'flockWithTimoutPcntl' => [function ($timeout = 3) use ($filename): Mutex {
'flockWithTimoutPcntl' => [static function ($timeout = 3) use ($filename): Mutex {
$file = fopen($filename, 'w');
$lock = Liberator::liberate(new FlockMutex($file, $timeout));
$lock->strategy = FlockMutex::STRATEGY_PCNTL; // @phpstan-ignore-line

return $lock->popsValue();
}],

'flockWithTimoutBusy' => [function ($timeout = 3) use ($filename): Mutex {
'flockWithTimoutBusy' => [static function ($timeout = 3) use ($filename): Mutex {
$file = fopen($filename, 'w');
$lock = Liberator::liberate(new FlockMutex($file, $timeout));
$lock->strategy = FlockMutex::STRATEGY_BUSY; // @phpstan-ignore-line

return $lock->popsValue();
}],

'semaphore' => [function ($timeout = 3) use ($filename): Mutex {
'semaphore' => [static function ($timeout = 3) use ($filename): Mutex {
$semaphore = sem_get(ftok($filename, 'b'));
self::assertThat(
$semaphore,
Expand All @@ -263,7 +263,7 @@ public static function provideExecutionIsSerializedWhenLockedCases(): iterable
];

if (getenv('MEMCACHE_HOST')) {
$cases['memcached'] = [function ($timeout = 3): Mutex {
$cases['memcached'] = [static function ($timeout = 3): Mutex {
$memcached = new \Memcached();
$memcached->addServer(getenv('MEMCACHE_HOST'), 11211);

Expand All @@ -274,9 +274,9 @@ public static function provideExecutionIsSerializedWhenLockedCases(): iterable
$uris = getenv('REDIS_URIS') !== false ? explode(',', getenv('REDIS_URIS')) : false;

if ($uris) {
$cases['PredisMutex'] = [function ($timeout = 3) use ($uris): Mutex {
$cases['PredisMutex'] = [static function ($timeout = 3) use ($uris): Mutex {
$clients = array_map(
function ($uri) {
static function ($uri) {
return new Client($uri);
},
$uris
Expand All @@ -287,10 +287,10 @@ function ($uri) {

if (class_exists(\Redis::class)) {
$cases['PHPRedisMutex'] = [
function ($timeout = 3) use ($uris): Mutex {
static function ($timeout = 3) use ($uris): Mutex {
/** @var \Redis[] $apis */
$apis = array_map(
function (string $uri): \Redis {
static function (string $uri): \Redis {
$redis = new \Redis();

$uri = parse_url($uri);
Expand All @@ -315,7 +315,7 @@ function (string $uri): \Redis {
}

if (getenv('MYSQL_DSN')) {
$cases['MySQLMutex'] = [function ($timeout = 3): Mutex {
$cases['MySQLMutex'] = [static function ($timeout = 3): Mutex {
$pdo = new \PDO(getenv('MYSQL_DSN'), getenv('MYSQL_USER'), getenv('MYSQL_USER'));
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

Expand All @@ -324,7 +324,7 @@ function (string $uri): \Redis {
}

if (getenv('PGSQL_DSN')) {
$cases['PgAdvisoryLockMutex'] = [function (): Mutex {
$cases['PgAdvisoryLockMutex'] = [static function (): Mutex {
$pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'), getenv('PGSQL_PASSWORD'));
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

Expand Down
Loading

0 comments on commit 8872ec1

Please sign in to comment.