Skip to content

Commit

Permalink
Upgrade PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 3, 2024
1 parent 35526ae commit a11ae4e
Show file tree
Hide file tree
Showing 38 changed files with 310 additions and 346 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
/coverage
/vendor
/composer.lock
.idea
nbproject
.vscode
.DS_Store

*.local
*.cache

/phpunit.xml
/.phpunit.result.cache
62 changes: 30 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
**[Requirements](#requirements)** |
**[Installation](#installation)** |
**[Usage](#usage)** |
**[License and authors](#license-and-authors)** |
**[License](#license)** |

# php-lock/lock

Expand Down Expand Up @@ -119,7 +119,7 @@ $newBalance = $mutex->check(function () use ($bankAccount, $amount): bool {
return $balance;
});

if (false === $newBalance) {
if ($newBalance === false) {
if ($balance < 0) {
throw new \DomainException('You have no credit.');
}
Expand All @@ -143,11 +143,11 @@ try {
throw new \DomainException();
}

return "result";
return 'result';
});
} catch (LockReleaseException $unlockException) {
if ($unlockException->getCodeException() !== null) {
$codeException = $unlockException->getCodeException()
$codeException = $unlockException->getCodeException();
// do something with the code exception
} else {
$code_result = $unlockException->getCodeResult();
Expand Down Expand Up @@ -190,9 +190,9 @@ Example:
```php
$mutex = new CASMutex();
$mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
$balance = $memcached->get("balance", null, $casToken);
$balance = $memcached->get('balance', null, $casToken);
$balance -= $amount;
if (!$memcached->cas($casToken, "balance", $balance)) {
if (!$memcached->cas($casToken, 'balance', $balance)) {
return;
}
$mutex->notify();
Expand All @@ -206,12 +206,12 @@ The **FlockMutex** is a lock implementation based on

Example:
```php
$mutex = new FlockMutex(fopen(__FILE__, "r"));
$mutex = new FlockMutex(fopen(__FILE__, 'r'));
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -228,14 +228,14 @@ The **MemcachedMutex** is a spinlock implementation which uses the
Example:
```php
$memcache = new \Memcached();
$memcache->addServer("localhost", 11211);
$memcache->addServer('localhost', 11211);

$mutex = new MemcachedMutex("balance", $memcache);
$mutex = new MemcachedMutex('balance', $memcache);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -255,14 +255,14 @@ continue to function as long as a majority of the servers still works.
Example:
```php
$redis = new Redis();
$redis->connect("localhost");
$redis->connect('localhost');

$mutex = new PHPRedisMutex([$redis], "balance");
$mutex = new PHPRedisMutex([$redis], 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -276,14 +276,14 @@ The **PredisMutex** is the distributed lock implementation of

Example:
```php
$redis = new Client("redis://localhost");
$redis = new Client('redis://localhost');

$mutex = new PredisMutex([$redis], "balance");
$mutex = new PredisMutex([$redis], 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -296,13 +296,13 @@ The **SemaphoreMutex** is a lock implementation based on

Example:
```php
$semaphore = sem_get(ftok(__FILE__, "a"));
$semaphore = sem_get(ftok(__FILE__, 'a'));
$mutex = new SemaphoreMutex($semaphore);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -324,16 +324,16 @@ Example:
$mutex = new TransactionalMutex($pdo);
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
$select = $pdo->prepare(
"SELECT balance FROM account WHERE id = ? FOR UPDATE"
'SELECT balance FROM account WHERE id = ? FOR UPDATE'
);
$select->execute([$accountId]);
$balance = $select->fetchColumn();

$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$pdo->prepare("UPDATE account SET balance = ? WHERE id = ?")
$pdo->prepare('UPDATE account SET balance = ? WHERE id = ?')
->execute([$balance, $accountId]);
});
```
Expand All @@ -355,14 +355,14 @@ Also note that `GET_LOCK` function is server wide and the MySQL manual suggests
you to namespace your locks like `dbname.lockname`.

```php
$pdo = new PDO("mysql:host=localhost;dbname=test", "username");
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username');

$mutex = new MySQLMutex($pdo, "balance", 15);
$mutex = new MySQLMutex($pdo, 'balance', 15);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -381,24 +381,22 @@ No time outs are supported. If the connection to the database server is lost or
interrupted, the lock is automatically released.

```php
$pdo = new PDO("pgsql:host=localhost;dbname=test;", "username");
$pdo = new PDO('pgsql:host=localhost;dbname=test;', 'username');

$mutex = new PgAdvisoryLockMutex($pdo, "balance");
$mutex = new PgAdvisoryLockMutex($pdo, 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
```

## License and authors

This project is free and under the MIT.
## License

Responsible for this project is Willem Stuursma-Ruwen <[email protected]>.
This project is free and is licensed under the MIT.

[1]: http://semver.org
[2]: https://github.com/nrk/predis
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"homepage": "https://github.com/malkusch/lock",
"require": {
"php": ">=7.3 <8.4",
"psr/log": "^1 || ^2 || ^3"
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"ext-memcached": "*",
Expand All @@ -44,14 +44,17 @@
"ext-pdo_mysql": "*",
"ext-pdo_sqlite": "*",
"ext-sysvsem": "*",
"eloquent/liberator": "^2.0",
"eloquent/liberator": "^2.0 || ^3.0",
"ergebnis/composer-normalize": "^2.13",
"friendsofphp/php-cs-fixer": "^3.0",
"mikey179/vfsstream": "^1.6.7",
"mikey179/vfsstream": "^1.6.11",
"php-mock/php-mock-phpunit": "^2.1",
"phpstan/phpstan": "^0.12.58",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^9.4",
"predis/predis": "^1.1",
"predis/predis": "^1.1.8",
"spatie/async": "^1.5"
},
"suggest": {
Expand Down
36 changes: 30 additions & 6 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
level: 5
level: 6
checkMissingOverrideMethodAttribute: true
paths:
- ./
excludes_analyse:
- vendor/
- .
excludePaths:
- vendor

# TODO review once we drop PHP 7.x support
treatPhpDocTypesAsCertain: false
ignoreErrors:
# TODO
-
path: '*'
identifier: equal.notAllowed
message: '~^Loose comparison via "==" is not allowed\.$~'
count: 4
-
path: 'src/mutex/RedisMutex.php'
identifier: if.condNotBoolean
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
count: 1
-
path: 'src/mutex/TransactionalMutex.php'
identifier: if.condNotBoolean
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
count: 1
-
path: 'tests/mutex/*Test.php'
identifier: empty.notAllowed
message: '~^Construct empty\(\) is not allowed\. Use more strict comparison\.$~'
count: 6
3 changes: 0 additions & 3 deletions src/exception/DeadlineException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@

namespace malkusch\lock\exception;

/**
* Deadline exception.
*/
class DeadlineException extends \RuntimeException implements PhpLockException {}
4 changes: 1 addition & 3 deletions src/exception/MutexException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
class MutexException extends \RuntimeException implements PhpLockException
{
/**
* @var int not enough redis servers
*/
/** @var int not enough redis servers */
public const REDIS_NOT_ENOUGH_SERVERS = 1;
}
16 changes: 8 additions & 8 deletions src/exception/PhpLockException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
/**
* Common php-lock/lock exception interface.
*
* @method string getMessage()
* @method int getCode()
* @method string getFile()
* @method int getLine()
* @method array getTrace()
* @method string getTraceAsString()
* @method \Throwable|null getPrevious()
* @method string __toString()
* @method string getMessage()
* @method int getCode()
* @method string getFile()
* @method int getLine()
* @method list<array<string, mixed>> getTrace()
* @method string getTraceAsString()
* @method \Throwable|null getPrevious()
* @method string __toString()
*/
interface PhpLockException {}
12 changes: 2 additions & 10 deletions src/mutex/CASMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
*/
class CASMutex extends Mutex
{
/**
* @var Loop the loop
*/
/** @var Loop the loop */
private $loop;

/**
Expand Down Expand Up @@ -63,21 +61,15 @@ public function notify(): void
* $balance -= $amount;
* if (!$memcached->cas($casToken, 'balance', $balance)) {
* return;
*
* }
* $mutex->notify();
* });
* </code>
*
* @template T
*
* @param callable(): T $code the synchronized execution block
*
* @return T the return value of the execution block
*
* @throws \Exception the execution block threw an exception
* @throws TimeoutException the timeout was reached
*/
#[\Override]
public function synchronized(callable $code)
{
return $this->loop->execute($code);
Expand Down
Loading

0 comments on commit a11ae4e

Please sign in to comment.