Skip to content

Commit a11ae4e

Browse files
committed
Upgrade PHPStan
1 parent 35526ae commit a11ae4e

38 files changed

+310
-346
lines changed

.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
/coverage
12
/vendor
23
/composer.lock
4+
.idea
5+
nbproject
6+
.vscode
7+
.DS_Store
8+
9+
*.local
10+
*.cache
11+
312
/phpunit.xml
4-
/.phpunit.result.cache

README.md

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
**[Requirements](#requirements)** |
22
**[Installation](#installation)** |
33
**[Usage](#usage)** |
4-
**[License and authors](#license-and-authors)** |
4+
**[License](#license)** |
55

66
# php-lock/lock
77

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

122-
if (false === $newBalance) {
122+
if ($newBalance === false) {
123123
if ($balance < 0) {
124124
throw new \DomainException('You have no credit.');
125125
}
@@ -143,11 +143,11 @@ try {
143143
throw new \DomainException();
144144
}
145145

146-
return "result";
146+
return 'result';
147147
});
148148
} catch (LockReleaseException $unlockException) {
149149
if ($unlockException->getCodeException() !== null) {
150-
$codeException = $unlockException->getCodeException()
150+
$codeException = $unlockException->getCodeException();
151151
// do something with the code exception
152152
} else {
153153
$code_result = $unlockException->getCodeResult();
@@ -190,9 +190,9 @@ Example:
190190
```php
191191
$mutex = new CASMutex();
192192
$mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
193-
$balance = $memcached->get("balance", null, $casToken);
193+
$balance = $memcached->get('balance', null, $casToken);
194194
$balance -= $amount;
195-
if (!$memcached->cas($casToken, "balance", $balance)) {
195+
if (!$memcached->cas($casToken, 'balance', $balance)) {
196196
return;
197197
}
198198
$mutex->notify();
@@ -206,12 +206,12 @@ The **FlockMutex** is a lock implementation based on
206206

207207
Example:
208208
```php
209-
$mutex = new FlockMutex(fopen(__FILE__, "r"));
209+
$mutex = new FlockMutex(fopen(__FILE__, 'r'));
210210
$mutex->synchronized(function () use ($bankAccount, $amount) {
211211
$balance = $bankAccount->getBalance();
212212
$balance -= $amount;
213213
if ($balance < 0) {
214-
throw new \DomainException("You have no credit.");
214+
throw new \DomainException('You have no credit.');
215215
}
216216
$bankAccount->setBalance($balance);
217217
});
@@ -228,14 +228,14 @@ The **MemcachedMutex** is a spinlock implementation which uses the
228228
Example:
229229
```php
230230
$memcache = new \Memcached();
231-
$memcache->addServer("localhost", 11211);
231+
$memcache->addServer('localhost', 11211);
232232

233-
$mutex = new MemcachedMutex("balance", $memcache);
233+
$mutex = new MemcachedMutex('balance', $memcache);
234234
$mutex->synchronized(function () use ($bankAccount, $amount) {
235235
$balance = $bankAccount->getBalance();
236236
$balance -= $amount;
237237
if ($balance < 0) {
238-
throw new \DomainException("You have no credit.");
238+
throw new \DomainException('You have no credit.');
239239
}
240240
$bankAccount->setBalance($balance);
241241
});
@@ -255,14 +255,14 @@ continue to function as long as a majority of the servers still works.
255255
Example:
256256
```php
257257
$redis = new Redis();
258-
$redis->connect("localhost");
258+
$redis->connect('localhost');
259259

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

277277
Example:
278278
```php
279-
$redis = new Client("redis://localhost");
279+
$redis = new Client('redis://localhost');
280280

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

297297
Example:
298298
```php
299-
$semaphore = sem_get(ftok(__FILE__, "a"));
299+
$semaphore = sem_get(ftok(__FILE__, 'a'));
300300
$mutex = new SemaphoreMutex($semaphore);
301301
$mutex->synchronized(function () use ($bankAccount, $amount) {
302302
$balance = $bankAccount->getBalance();
303303
$balance -= $amount;
304304
if ($balance < 0) {
305-
throw new \DomainException("You have no credit.");
305+
throw new \DomainException('You have no credit.');
306306
}
307307
$bankAccount->setBalance($balance);
308308
});
@@ -324,16 +324,16 @@ Example:
324324
$mutex = new TransactionalMutex($pdo);
325325
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
326326
$select = $pdo->prepare(
327-
"SELECT balance FROM account WHERE id = ? FOR UPDATE"
327+
'SELECT balance FROM account WHERE id = ? FOR UPDATE'
328328
);
329329
$select->execute([$accountId]);
330330
$balance = $select->fetchColumn();
331331

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

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

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

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

386-
$mutex = new PgAdvisoryLockMutex($pdo, "balance");
386+
$mutex = new PgAdvisoryLockMutex($pdo, 'balance');
387387
$mutex->synchronized(function () use ($bankAccount, $amount) {
388388
$balance = $bankAccount->getBalance();
389389
$balance -= $amount;
390390
if ($balance < 0) {
391-
throw new \DomainException("You have no credit.");
391+
throw new \DomainException('You have no credit.');
392392
}
393393
$bankAccount->setBalance($balance);
394394
});
395395
```
396396

397-
## License and authors
398-
399-
This project is free and under the MIT.
397+
## License
400398

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

403401
[1]: http://semver.org
404402
[2]: https://github.com/nrk/predis

composer.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"homepage": "https://github.com/malkusch/lock",
3636
"require": {
3737
"php": ">=7.3 <8.4",
38-
"psr/log": "^1 || ^2 || ^3"
38+
"psr/log": "^1.0 || ^2.0 || ^3.0"
3939
},
4040
"require-dev": {
4141
"ext-memcached": "*",
@@ -44,14 +44,17 @@
4444
"ext-pdo_mysql": "*",
4545
"ext-pdo_sqlite": "*",
4646
"ext-sysvsem": "*",
47-
"eloquent/liberator": "^2.0",
47+
"eloquent/liberator": "^2.0 || ^3.0",
4848
"ergebnis/composer-normalize": "^2.13",
4949
"friendsofphp/php-cs-fixer": "^3.0",
50-
"mikey179/vfsstream": "^1.6.7",
50+
"mikey179/vfsstream": "^1.6.11",
5151
"php-mock/php-mock-phpunit": "^2.1",
52-
"phpstan/phpstan": "^0.12.58",
52+
"phpstan/extension-installer": "^1.1",
53+
"phpstan/phpstan": "^2.0",
54+
"phpstan/phpstan-deprecation-rules": "^2.0",
55+
"phpstan/phpstan-strict-rules": "^2.0",
5356
"phpunit/phpunit": "^9.4",
54-
"predis/predis": "^1.1",
57+
"predis/predis": "^1.1.8",
5558
"spatie/async": "^1.5"
5659
},
5760
"suggest": {

phpstan.neon.dist

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1+
includes:
2+
- phar://phpstan.phar/conf/bleedingEdge.neon
3+
14
parameters:
2-
level: 5
5+
level: 6
6+
checkMissingOverrideMethodAttribute: true
37
paths:
4-
- ./
5-
excludes_analyse:
6-
- vendor/
8+
- .
9+
excludePaths:
10+
- vendor
711

8-
# TODO review once we drop PHP 7.x support
9-
treatPhpDocTypesAsCertain: false
12+
ignoreErrors:
13+
# TODO
14+
-
15+
path: '*'
16+
identifier: equal.notAllowed
17+
message: '~^Loose comparison via "==" is not allowed\.$~'
18+
count: 4
19+
-
20+
path: 'src/mutex/RedisMutex.php'
21+
identifier: if.condNotBoolean
22+
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
23+
count: 1
24+
-
25+
path: 'src/mutex/TransactionalMutex.php'
26+
identifier: if.condNotBoolean
27+
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
28+
count: 1
29+
-
30+
path: 'tests/mutex/*Test.php'
31+
identifier: empty.notAllowed
32+
message: '~^Construct empty\(\) is not allowed\. Use more strict comparison\.$~'
33+
count: 6

src/exception/DeadlineException.php

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

55
namespace malkusch\lock\exception;
66

7-
/**
8-
* Deadline exception.
9-
*/
107
class DeadlineException extends \RuntimeException implements PhpLockException {}

src/exception/MutexException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*/
1313
class MutexException extends \RuntimeException implements PhpLockException
1414
{
15-
/**
16-
* @var int not enough redis servers
17-
*/
15+
/** @var int not enough redis servers */
1816
public const REDIS_NOT_ENOUGH_SERVERS = 1;
1917
}

src/exception/PhpLockException.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
/**
88
* Common php-lock/lock exception interface.
99
*
10-
* @method string getMessage()
11-
* @method int getCode()
12-
* @method string getFile()
13-
* @method int getLine()
14-
* @method array getTrace()
15-
* @method string getTraceAsString()
16-
* @method \Throwable|null getPrevious()
17-
* @method string __toString()
10+
* @method string getMessage()
11+
* @method int getCode()
12+
* @method string getFile()
13+
* @method int getLine()
14+
* @method list<array<string, mixed>> getTrace()
15+
* @method string getTraceAsString()
16+
* @method \Throwable|null getPrevious()
17+
* @method string __toString()
1818
*/
1919
interface PhpLockException {}

src/mutex/CASMutex.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
*/
1818
class CASMutex extends Mutex
1919
{
20-
/**
21-
* @var Loop the loop
22-
*/
20+
/** @var Loop the loop */
2321
private $loop;
2422

2523
/**
@@ -63,21 +61,15 @@ public function notify(): void
6361
* $balance -= $amount;
6462
* if (!$memcached->cas($casToken, 'balance', $balance)) {
6563
* return;
66-
*
6764
* }
6865
* $mutex->notify();
6966
* });
7067
* </code>
7168
*
72-
* @template T
73-
*
74-
* @param callable(): T $code the synchronized execution block
75-
*
76-
* @return T the return value of the execution block
77-
*
7869
* @throws \Exception the execution block threw an exception
7970
* @throws TimeoutException the timeout was reached
8071
*/
72+
#[\Override]
8173
public function synchronized(callable $code)
8274
{
8375
return $this->loop->execute($code);

0 commit comments

Comments
 (0)