Skip to content

Commit a33689a

Browse files
authored
Define stub for cache object shape (#724)
* Define stub for cache object shape Fixes #270 * Keep PHP 7.4 support
1 parent 8f92120 commit a33689a

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Drupal\Core\Cache;
4+
5+
/**
6+
* @phpstan-type CacheObject object{data: mixed, created: int, tags: string[], valid: bool, expire: int, checksum: string, serialized: int}
7+
*/
8+
interface CacheBackendInterface {
9+
10+
/**
11+
* @return CacheObject|false
12+
*/
13+
public function get(string $cid, bool $allow_invalid = FALSE);
14+
15+
/**
16+
* @param string[] $cids
17+
* @return CacheObject[]
18+
*/
19+
public function getMultiple(array &$cids, bool $allow_invalid = FALSE): array;
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace mglaman\PHPStanDrupal\Tests\Type;
6+
7+
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
8+
use PHPStan\Testing\TypeInferenceTestCase;
9+
10+
final class CacheBackendInterfaceStubTest extends TypeInferenceTestCase
11+
{
12+
use AdditionalConfigFilesTrait;
13+
14+
public function dataFileAsserts(): iterable
15+
{
16+
yield from self::gatherAssertTypes(__DIR__ . '/data/cache-backend-interface-stub.php');
17+
}
18+
19+
/**
20+
* @dataProvider dataFileAsserts
21+
* @param string $assertType
22+
* @param string $file
23+
* @param mixed ...$args
24+
*/
25+
public function testFileAsserts(
26+
string $assertType,
27+
string $file,
28+
...$args
29+
): void
30+
{
31+
$this->assertFileAsserts($assertType, $file, ...$args);
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CacheBackendInterfaceStub;
4+
5+
use Drupal\Core\Cache\DatabaseBackend;
6+
use Drupal\Core\Cache\MemoryBackend;
7+
use function PHPStan\Testing\assertType;
8+
9+
$cids = ['foo'];
10+
11+
assert($dbBackend instanceof DatabaseBackend);
12+
assertType('object{data: mixed, created: int, tags: array<string>, valid: bool, expire: int, checksum: string, serialized: int}|false', $dbBackend->get('foo'));
13+
assertType('array<object{data: mixed, created: int, tags: array<string>, valid: bool, expire: int, checksum: string, serialized: int}>', $dbBackend->getMultiple($cids));
14+
15+
assert($memoryBackend instanceof MemoryBackend);
16+
assertType('object{data: mixed, created: int, tags: array<string>, valid: bool, expire: int, checksum: string, serialized: int}|false', $memoryBackend->get('foo'));
17+
assertType('array<object{data: mixed, created: int, tags: array<string>, valid: bool, expire: int, checksum: string, serialized: int}>', $memoryBackend->getMultiple($cids));

0 commit comments

Comments
 (0)