Skip to content

Commit b9a5574

Browse files
staabmclxmstaab
andauthored
make RecordingQueryReflector work, without the need to pass --debug (#95)
Co-authored-by: Markus Staab <[email protected]>
1 parent caa9824 commit b9a5574

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ If not configured otherwise, the following defaults are used:
165165
composer require --dev staabm/phpstan-dba
166166
```
167167

168-
## Caveats
169-
170-
- running `RecordingQueryReflector` requires running PHPStan with `--debug` (otherwise we see concurrency issues while building the cache).
171-
172168
## Todos
173169

174170
- support [more mysql to PHPStan type mappings](https://github.com/staabm/phpstan-dba/blob/b868f40c80afcecd3de408df3801b5a24e220dd8/src/QueryReflection/MysqliQueryReflector.php#L111)

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
],
3434
"phpstan-dba-baseline": [
3535
"phpunit",
36-
"phpstan analyse -c phpstan.neon.dist --debug"
36+
"phpstan analyse -c phpstan.neon.dist"
3737
],
3838
"phpstan": [
39-
"phpstan analyse -c phpstan.neon.dist --debug"
39+
"phpstan analyse -c phpstan.neon.dist"
4040
],
4141
"phpunit": [
4242
"phpunit"

src/QueryReflection/ReflectionCache.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@ final class ReflectionCache
2323
*/
2424
private $records = [];
2525

26+
/**
27+
* @var bool
28+
*/
29+
private $changed = false;
30+
2631
private function __construct(string $cacheFile)
2732
{
2833
$this->cacheFile = $cacheFile;
34+
35+
if (!is_file($cacheFile)) {
36+
// enforce file creation
37+
$this->changed = true;
38+
}
2939
}
3040

3141
public static function create(string $cacheFile): self
@@ -51,6 +61,10 @@ public static function load(string $cacheFile): self
5161

5262
public function persist(): void
5363
{
64+
if (!$this->changed) {
65+
return;
66+
}
67+
5468
$records = $this->records;
5569

5670
// sort records to prevent unnecessary cache invalidation caused by different order of queries
@@ -99,6 +113,7 @@ public function putValidationError(string $queryString, ?Error $error): void
99113

100114
$cacheEntry = &$this->records[$queryString];
101115
$cacheEntry['error'] = $error;
116+
$this->changed = true;
102117
}
103118

104119
/**
@@ -154,5 +169,6 @@ public function putResultType(string $queryString, int $fetchType, ?Type $result
154169
}
155170

156171
$cacheEntry['result'][$fetchType] = $resultType;
172+
$this->changed = true;
157173
}
158174
}

0 commit comments

Comments
 (0)