Issues (19)

src/Cache/PsrCacheAdapter.php (5 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Metadata\Cache;
6
7
use Metadata\ClassMetadata;
8
use Psr\Cache\CacheItemPoolInterface;
9
10
class PsrCacheAdapter implements CacheInterface, ClearableCacheInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $prefix;
16
17
    /**
18
     * @var CacheItemPoolInterface
19
     */
20
    private $pool;
21
22
    /**
23
     * @var CacheItemPoolInterface
24
     */
25
    private $lastItem;
26
27 2
    public function __construct(string $prefix, CacheItemPoolInterface $pool)
28
    {
29 2
        $this->prefix = $prefix;
30 2
        $this->pool = $pool;
31 2
    }
32
33 2
    public function load(string $class): ?ClassMetadata
34
    {
35 2
        $this->lastItem = $this->pool->getItem($this->sanitizeCacheKey($this->prefix . $class));
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->pool->getItem($th...this->prefix . $class)) of type Psr\Cache\CacheItemInterface is incompatible with the declared type Psr\Cache\CacheItemPoolInterface of property $lastItem.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
37 2
        return $this->lastItem->get();
38
    }
39
40 2
    public function put(ClassMetadata $metadata): void
41
    {
42 2
        $key = $this->sanitizeCacheKey($this->prefix . $metadata->name);
43
44 2
        if (null === $this->lastItem || $this->lastItem->getKey() !== $key) {
0 ignored issues
show
The method getKey() does not exist on Psr\Cache\CacheItemPoolInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        if (null === $this->lastItem || $this->lastItem->/** @scrutinizer ignore-call */ getKey() !== $key) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            $this->lastItem = $this->pool->getItem($key);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->pool->getItem($key) of type Psr\Cache\CacheItemInterface is incompatible with the declared type Psr\Cache\CacheItemPoolInterface of property $lastItem.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        }
47
48 2
        $this->pool->save($this->lastItem->set($metadata));
0 ignored issues
show
The method set() does not exist on Psr\Cache\CacheItemPoolInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $this->pool->save($this->lastItem->/** @scrutinizer ignore-call */ set($metadata));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
It seems like $this->lastItem->set($metadata) can also be of type Psr\Cache\CacheItemPoolInterface; however, parameter $item of Psr\Cache\CacheItemPoolInterface::save() does only seem to accept Psr\Cache\CacheItemInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $this->pool->save(/** @scrutinizer ignore-type */ $this->lastItem->set($metadata));
Loading history...
49 2
    }
50
51 2
    public function evict(string $class): void
52
    {
53 2
        $this->pool->deleteItem($this->sanitizeCacheKey($this->prefix . $class));
54 2
    }
55
56
    public function clear(): bool
57
    {
58
        return $this->pool->clear();
59
    }
60 2
61
    /**
62 2
     * If anonymous class is to be cached, it contains invalid path characters that need to be removed/replaced
63
     * Example of anonymous class name: class@anonymous\x00/app/src/Controller/DefaultController.php0x7f82a7e026ec
64
     */
65
    private function sanitizeCacheKey(string $key): string
66
    {
67
        return str_replace(['\\', "\0", '@', '/', '$', '{', '}', ':'], '-', $key);
68
    }
69
}
70