Skip to content

Commit 6c67529

Browse files
committed
Add PHPStan
1 parent 7e9419c commit 6c67529

14 files changed

+38
-27
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ before_script:
4848
script:
4949
- vendor/bin/phpunit
5050
- vendor/bin/phpcs --standard=PSR2 classes/ tests/
51-
51+
- vendor/bin/phpstan analyse

classes/mutex/PHPRedisMutex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PHPRedisMutex extends RedisMutex
3535
* @param array<\Redis|\RedisCluster> $redisAPIs The Redis connections.
3636
* @param string $name The lock name.
3737
* @param int $timeout The time in seconds a lock expires after. Default is
38-
* 3 seconds.
38+
* 3 seconds.
3939
* @throws \LengthException The timeout must be greater than 0.
4040
*/
4141
public function __construct(array $redisAPIs, string $name, int $timeout = 3)
@@ -44,7 +44,7 @@ public function __construct(array $redisAPIs, string $name, int $timeout = 3)
4444
}
4545

4646
/**
47-
* @param \Redis|\RedisCluster $redisApi The Redis or RedisCluster connection.
47+
* @param \Redis|\RedisCluster $redisAPI The Redis or RedisCluster connection.
4848
* @throws LockAcquireException
4949
*/
5050
protected function add($redisAPI, string $key, string $value, int $expire): bool

classes/mutex/PgAdvisoryLockMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(\PDO $PDO, string $name)
3232

3333
$hashed_name = hash('sha256', $name, true);
3434

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

classes/mutex/SemaphoreMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SemaphoreMutex extends LockMutex
3333
* $mutex = new SemaphoreMutex($semaphore);
3434
* </code>
3535
*
36-
* @param resource semaphore The semaphore id.
36+
* @param resource $semaphore The semaphore id.
3737
* @throws \InvalidArgumentException The semaphore id is not a valid resource.
3838
*/
3939
public function __construct($semaphore)

classes/mutex/TransactionalMutex.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ private static function checkAutocommit(\PDO $pdo): void
106106
* @throws LockAcquireException The transaction was not commited.
107107
* @return mixed The return value of the execution block.
108108
* @SuppressWarnings(PHPMD)
109-
*
110109
*/
111110
public function synchronized(callable $code)
112111
{
@@ -129,7 +128,7 @@ public function synchronized(callable $code)
129128
$this->rollBack($e);
130129

131130
if (self::hasPDOException($e)) {
132-
return; // Replay
131+
return null; // Replay
133132
} else {
134133
throw $e;
135134
}
@@ -140,10 +139,10 @@ public function synchronized(callable $code)
140139
/**
141140
* Checks if an exception or any of its previous exceptions is a PDOException.
142141
*
143-
* @param \Exception $exception The exception.
144-
* @return boolean True if there's a PDOException.
142+
* @param \Throwable $exception The exception.
143+
* @return bool True if there's a PDOException.
145144
*/
146-
private static function hasPDOException(\Exception $exception)
145+
private static function hasPDOException(\Throwable $exception)
147146
{
148147
if ($exception instanceof PDOException) {
149148
return true;

classes/util/Loop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function execute(callable $code)
9797
$result = null;
9898
for ($i = 0; $this->looping && microtime(true) < $deadline; ++$i) {
9999
$result = $code();
100-
if (!$this->looping) {
100+
if (!$this->looping) { // @phpstan-ignore-line
101101
break;
102102
}
103103

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"johnkary/phpunit-speedtrap": "^3.0",
5858
"mikey179/vfsstream": "^1.6",
5959
"php-mock/php-mock-phpunit": "^2.1",
60+
"phpstan/phpstan": "^0.12.58",
6061
"phpunit/phpunit": "^9.4",
6162
"predis/predis": "^1.1",
6263
"spatie/async": "^1.5",

phpstan.neon.dist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- ./
5+
excludes_analyse:
6+
- vendor/
7+
8+
# TODO review once we drop PHP 7.x support
9+
treatPhpDocTypesAsCertain: false

tests/mutex/FlockMutexTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FlockMutexTest extends TestCase
2222
private $mutex;
2323

2424
/**
25-
* @var resource
25+
* @var string
2626
*/
2727
private $file;
2828

@@ -31,7 +31,7 @@ protected function setUp(): void
3131
parent::setUp();
3232

3333
$this->file = tempnam(sys_get_temp_dir(), 'flock-');
34-
$this->mutex = Liberator::liberate(new FlockMutex(fopen($this->file, 'r'), 1));
34+
$this->mutex = Liberator::liberate(new FlockMutex(fopen($this->file, 'r'), 1)); // @phpstan-ignore-line
3535
}
3636

3737
protected function tearDown(): void
@@ -46,10 +46,10 @@ protected function tearDown(): void
4646
*/
4747
public function testCodeExecutedOutsideLockIsNotThrown(int $strategy)
4848
{
49-
$this->mutex->strategy = $strategy;
49+
$this->mutex->strategy = $strategy; // @phpstan-ignore-line
5050

5151
$this->assertTrue($this->mutex->synchronized(function (): bool {
52-
usleep(1.1e6);
52+
usleep(1100 * 1000);
5353

5454
return true;
5555
}));
@@ -66,7 +66,7 @@ public function testTimeoutOccurs(int $strategy)
6666
$another_resource = fopen($this->file, 'r');
6767
flock($another_resource, LOCK_EX);
6868

69-
$this->mutex->strategy = $strategy;
69+
$this->mutex->strategy = $strategy; // @phpstan-ignore-line
7070

7171
try {
7272
$this->mutex->synchronized(
@@ -94,7 +94,7 @@ public function testNoTimeoutWaitsForever()
9494
$another_resource = fopen($this->file, 'r');
9595
flock($another_resource, LOCK_EX);
9696

97-
$this->mutex->strategy = FlockMutex::STRATEGY_BLOCK;
97+
$this->mutex->strategy = FlockMutex::STRATEGY_BLOCK; // @phpstan-ignore-line
9898

9999
$timebox = new PcntlTimeout(1);
100100
$timebox->timeBoxed(function () {

tests/mutex/LockMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class LockMutexTest extends TestCase
1818
{
1919
/**
20-
* @var \PHPUnit\Framework\MockObject\MockObject The SUT
20+
* @var \PHPUnit\Framework\MockObject\MockObject|LockMutex The SUT
2121
*/
2222
private $mutex;
2323

0 commit comments

Comments
 (0)