Skip to content

Commit afb452d

Browse files
committed
test with empty result cache (#482)
1 parent 0804a38 commit afb452d

19 files changed

+51
-20017
lines changed

.github/workflows/tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ jobs:
5656
reflector: "pdo-mysql"
5757
mode: "replay-and-recording"
5858

59+
- php-version: "8.1"
60+
db-image: 'mysql:8.0'
61+
reflector: "pdo-mysql"
62+
mode: "empty-recording"
63+
- php-version: "8.1"
64+
db-image: 'mysql:8.0'
65+
reflector: "pdo-mysql"
66+
mode: "empty-replay-and-recording"
67+
5968
env:
6069
DBA_REFLECTOR: ${{ matrix.reflector }}
6170
DBA_MODE: ${{ matrix.mode }}

src/QueryReflection/RecordingQueryReflector.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public function validateQueryString(string $queryString): ?Error
3434
{
3535
$error = $this->reflector->validateQueryString($queryString);
3636

37-
$this->reflectionCache->putValidationError(
38-
$queryString,
39-
$error
40-
);
37+
if (null !== $error) {
38+
$this->reflectionCache->putValidationError(
39+
$queryString,
40+
$error
41+
);
42+
}
4143

4244
return $error;
4345
}
@@ -46,11 +48,13 @@ public function getResultType(string $queryString, int $fetchType): ?Type
4648
{
4749
$resultType = $this->reflector->getResultType($queryString, $fetchType);
4850

49-
$this->reflectionCache->putResultType(
50-
$queryString,
51-
$fetchType,
52-
$resultType
53-
);
51+
if (null !== $resultType) {
52+
$this->reflectionCache->putResultType(
53+
$queryString,
54+
$fetchType,
55+
$resultType
56+
);
57+
}
5458

5559
return $resultType;
5660
}

src/QueryReflection/ReflectionCache.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
final class ReflectionCache
1515
{
16-
public const SCHEMA_VERSION = 'v9-put-null-when-valid';
16+
public const SCHEMA_VERSION = 'v10-rework-replay';
1717

1818
/**
1919
* @var string
@@ -174,11 +174,6 @@ public function persist(): void
174174
return;
175175
}
176176

177-
if ($this->isPHPStormSingleFileRun()) {
178-
// don't overwrite reflection cache, when analyzing only a single file
179-
return;
180-
}
181-
182177
try {
183178
flock(self::$lockHandle, LOCK_EX);
184179

@@ -253,7 +248,7 @@ public function getValidationError(string $queryString): ?Error
253248
return $cacheEntry['error'];
254249
}
255250

256-
public function putValidationError(string $queryString, ?Error $error): void
251+
public function putValidationError(string $queryString, Error $error): void
257252
{
258253
$records = $this->lazyReadRecords();
259254

@@ -266,6 +261,8 @@ public function putValidationError(string $queryString, ?Error $error): void
266261
$this->changes[$queryString]['error'] = $this->records[$queryString]['error'] = $error;
267262
$this->cacheIsDirty = true;
268263
}
264+
265+
unset($this->records[$queryString]['result']);
269266
}
270267

271268
/**
@@ -315,7 +312,7 @@ public function getResultType(string $queryString, int $fetchType): ?Type
315312
/**
316313
* @param QueryReflector::FETCH_TYPE* $fetchType
317314
*/
318-
public function putResultType(string $queryString, int $fetchType, ?Type $resultType): void
315+
public function putResultType(string $queryString, int $fetchType, Type $resultType): void
319316
{
320317
$records = $this->lazyReadRecords();
321318

@@ -334,14 +331,7 @@ public function putResultType(string $queryString, int $fetchType, ?Type $result
334331
$this->changes[$queryString]['result'][$fetchType] = $this->records[$queryString]['result'][$fetchType] = $resultType;
335332
$this->cacheIsDirty = true;
336333
}
337-
}
338-
339-
private function isPHPStormSingleFileRun(): bool
340-
{
341-
if (!\array_key_exists('__CFBundleIdentifier', $_SERVER)) {
342-
return false;
343-
}
344334

345-
return 'com.jetbrains.PhpStorm' === $_SERVER['__CFBundleIdentifier'];
335+
unset($this->records[$queryString]['error']);
346336
}
347337
}

src/QueryReflection/ReplayAndRecordingQueryReflector.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace staabm\PHPStanDba\QueryReflection;
66

77
use PHPStan\Type\Type;
8-
use staabm\PHPStanDba\CacheNotPopulatedException;
98
use staabm\PHPStanDba\DbSchema\SchemaHasherMysql;
109
use staabm\PHPStanDba\Error;
1110

@@ -66,11 +65,12 @@ public function validateQueryString(string $queryString): ?Error
6665
return $this->createRecordingReflector()->validateQueryString($queryString);
6766
}
6867

69-
try {
70-
return $this->replayReflector->validateQueryString($queryString);
71-
} catch (CacheNotPopulatedException $e) {
72-
return $this->createRecordingReflector()->validateQueryString($queryString);
68+
$error = $this->replayReflector->validateQueryString($queryString);
69+
if (null !== $error) {
70+
return $error;
7371
}
72+
73+
return $this->createRecordingReflector()->validateQueryString($queryString);
7474
}
7575

7676
public function getResultType(string $queryString, int $fetchType): ?Type
@@ -79,11 +79,12 @@ public function getResultType(string $queryString, int $fetchType): ?Type
7979
return $this->createRecordingReflector()->getResultType($queryString, $fetchType);
8080
}
8181

82-
try {
83-
return $this->replayReflector->getResultType($queryString, $fetchType);
84-
} catch (CacheNotPopulatedException $e) {
85-
return $this->createRecordingReflector()->getResultType($queryString, $fetchType);
82+
$resultType = $this->replayReflector->getResultType($queryString, $fetchType);
83+
if (null !== $resultType) {
84+
return $resultType;
8685
}
86+
87+
return $this->createRecordingReflector()->getResultType($queryString, $fetchType);
8788
}
8889

8990
public function getDatasource()

src/QueryReflection/ReplayQueryReflector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public function __construct(ReflectionCache $cache)
2121

2222
public function validateQueryString(string $queryString): ?Error
2323
{
24+
if (!$this->reflectionCache->hasValidationError($queryString)) {
25+
return null;
26+
}
27+
2428
return $this->reflectionCache->getValidationError($queryString);
2529
}
2630

2731
public function getResultType(string $queryString, int $fetchType): ?Type
2832
{
29-
// queries with errors don't have a cached result type
30-
if (false === $this->reflectionCache->hasResultType($queryString, $fetchType)) {
33+
if (!$this->reflectionCache->hasResultType($queryString, $fetchType)) {
3134
return null;
3235
}
3336

tests/ReflectorFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public static function create(string $cacheDir): QueryReflector
5050
$cacheFile = $cacheDir.'/.phpstan-dba-'.$reflector.'.cache';
5151
}
5252

53+
if (str_starts_with($mode, 'empty-')) {
54+
file_put_contents($cacheFile, ''); // clear cache
55+
$mode = substr($mode, 6);
56+
}
57+
5358
$reflectionCache = ReflectionCache::create(
5459
$cacheFile
5560
);

0 commit comments

Comments
 (0)