Skip to content

Commit 31f4bc6

Browse files
committed
fixed PHPStan errors
1 parent 20be8d5 commit 31f4bc6

6 files changed

Lines changed: 52 additions & 18 deletions

File tree

phpstan.neon

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
parameters:
2-
level: 5
2+
level: 8
33

44
paths:
55
- src
66

77
excludePaths:
8-
- src/Bridges/CacheLatte/CacheMacro.php
8+
- src/compatibility.php
9+
10+
ignoreErrors:
11+
- # Intentional design pattern for derive() and factory methods
12+
identifier: new.static
13+
paths:
14+
- src/Caching/Cache.php
15+
- src/Bridges/CacheLatte/Nodes/CacheNode.php
16+
17+
- # Intentional by-reference parameter for dependency injection
18+
identifier: argument.byRef
19+
path: src/Caching/Cache.php
20+
21+
- # Runtime validation for untyped array input
22+
identifier: function.alreadyNarrowedType
23+
path: src/Caching/Cache.php
24+
25+
- # PHPStan cannot narrow callable to array shape after is_array() in closure
26+
identifier: offsetAccess.nonOffsetAccessible
27+
path: src/Caching/Cache.php
28+
29+
- # Intentional: API for invoking arbitrary user callbacks, a signature would only add false positives at call sites
30+
identifier: missingType.callable
31+
path: src/Caching/Cache.php
32+
33+
-
34+
identifier: method.childParameterType
35+
path: src/Bridges/Psr/PsrCacheAdapter.php

src/Bridges/CacheLatte/Runtime.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Nette;
1212
use Nette\Caching\Cache;
1313
use Nette\Caching\OutputHelper;
14-
use function array_intersect_key, array_key_exists, array_merge, array_pop, count, end, is_file, range;
14+
use function array_intersect_key, array_key_exists, array_merge, array_pop, count, end, is_file, is_string, range;
1515

1616

1717
/**
@@ -34,7 +34,7 @@ public function initialize(Latte\Runtime\Template $template): void
3434
{
3535
if ($this->stack) {
3636
$file = (new \ReflectionClass($template))->getFileName();
37-
if (@is_file($file)) { // @ - may trigger error
37+
if (is_string($file) && @is_file($file)) { // @ - may trigger error
3838
end($this->stack)->dependencies[Cache::Files][] = $file;
3939
}
4040
}

src/Caching/Storages/FileStorage.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Nette;
1111
use Nette\Caching\Cache;
12-
use function dirname, fclose, filemtime, flock, fopen, fseek, ftruncate, fwrite, is_dir, is_string, microtime, mkdir, mt_getrandmax, mt_rand, rmdir, serialize, str_pad, str_repeat, stream_get_contents, strlen, strrpos, substr_replace, time, touch, unlink, unserialize, urlencode;
12+
use function dirname, fclose, filemtime, flock, fopen, fseek, ftruncate, fwrite, is_array, is_dir, is_string, microtime, mkdir, mt_getrandmax, mt_rand, rmdir, serialize, str_pad, str_repeat, stream_get_contents, strlen, strrpos, substr_replace, time, touch, unlink, unserialize, urlencode;
1313
use const LOCK_EX, LOCK_SH, LOCK_UN, STR_PAD_LEFT;
1414

1515

@@ -286,7 +286,7 @@ public function clean(array $conditions): void
286286

287287
// cleaning using journal
288288
if ($this->journal) {
289-
foreach ($this->journal->clean($conditions) as $file) {
289+
foreach ($this->journal->clean($conditions) ?? [] as $file) {
290290
$this->delete($file);
291291
}
292292
}
@@ -295,6 +295,7 @@ public function clean(array $conditions): void
295295

296296
/**
297297
* Opens and locks a cache file and reads its metadata.
298+
* @param int<0, 7> $lock
298299
* @return ?array<string, mixed> meta data with 'file' and 'handle' keys added, or null if not found
299300
*/
300301
protected function readMetaAndLock(string $file, int $lock): ?array
@@ -309,10 +310,13 @@ protected function readMetaAndLock(string $file, int $lock): ?array
309310
$size = (int) stream_get_contents($handle, self::MetaHeaderLen);
310311
if ($size) {
311312
$meta = stream_get_contents($handle, $size, self::MetaHeaderLen);
312-
$meta = unserialize($meta);
313-
$meta[self::File] = $file;
314-
$meta[self::Handle] = $handle;
315-
return $meta;
313+
if ($meta !== false) {
314+
$meta = unserialize($meta);
315+
assert(is_array($meta));
316+
$meta[self::File] = $file;
317+
$meta[self::Handle] = $handle;
318+
return $meta;
319+
}
316320
}
317321

318322
flock($handle, LOCK_UN);
@@ -330,8 +334,11 @@ protected function readData(array $meta): mixed
330334
$data = stream_get_contents($meta[self::Handle]);
331335
flock($meta[self::Handle], LOCK_UN);
332336
fclose($meta[self::Handle]);
333-
334-
return empty($meta[self::MetaSerialized]) ? $data : unserialize($data);
337+
return match (true) {
338+
$data === false => null,
339+
empty($meta[self::MetaSerialized]) => $data,
340+
default => unserialize($data),
341+
};
335342
}
336343

337344

src/Caching/Storages/MemcachedStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function addServer(string $host = 'localhost', int $port = 11211): void
5555
{
5656
if (@$this->memcached->addServer($host, $port, 1) === false) { // @ is escalated to exception
5757
$error = error_get_last();
58-
throw new Nette\InvalidStateException("Memcached::addServer(): $error[message].");
58+
throw new Nette\InvalidStateException('Memcached::addServer(): ' . ($error['message'] ?? 'unknown error') . '.');
5959
}
6060
}
6161

@@ -216,7 +216,7 @@ public function clean(array $conditions): void
216216
$this->memcached->flush();
217217

218218
} elseif ($this->journal) {
219-
foreach ($this->journal->clean($conditions) as $entry) {
219+
foreach ($this->journal->clean($conditions) ?? [] as $entry) {
220220
$this->memcached->delete($entry, 0);
221221
}
222222
}

src/Caching/Storages/SQLiteJournal.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Nette;
1111
use Nette\Caching\Cache;
12-
use function count, extension_loaded, implode, is_file, str_repeat, touch;
12+
use function array_values, count, extension_loaded, implode, is_file, str_repeat, touch;
1313

1414

1515
/**
@@ -73,7 +73,7 @@ public function write(string $key, array $dependencies): void
7373
$arr[] = $tag;
7474
}
7575

76-
$this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', count($arr) / 2 - 1))
76+
$this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', intdiv(count($arr), 2) - 1))
7777
->execute($arr);
7878
}
7979

@@ -136,6 +136,6 @@ public function clean(array $conditions): ?array
136136
$this->pdo->prepare("DELETE FROM priorities WHERE key IN ($unionSql)")->execute($args);
137137
$this->pdo->exec('COMMIT');
138138

139-
return $keys;
139+
return array_values($keys);
140140
}
141141
}

src/Caching/Storages/SQLiteStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function write(string $key, mixed $data, array $dependencies): void
111111
$arr[] = $tag;
112112
}
113113

114-
$this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', count($arr) / 2 - 1))
114+
$this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', intdiv(count($arr), 2) - 1))
115115
->execute($arr);
116116
}
117117

0 commit comments

Comments
 (0)