Passed
Push — master ( 830a0c...6b86a6 )
by Asmir
01:30 queued 48s
created

PsrCacheAdapter::sanitizeCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
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
    public function __construct(string $prefix, CacheItemPoolInterface $pool)
28
    {
29
        $this->prefix = $prefix;
30
        $this->pool = $pool;
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function load(string $class): ?ClassMetadata
37
    {
38
        $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...
39
40
        return $this->lastItem->get();
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function put(ClassMetadata $metadata): void
47
    {
48
        $key = $this->sanitizeCacheKey($this->prefix . $metadata->name);
49
50
        if (null === $this->lastItem || $this->lastItem->getKey() !== $key) {
0 ignored issues
show
Bug introduced by
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

50
        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...
51
            $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...
52
        }
53
54
        $this->pool->save($this->lastItem->set($metadata));
0 ignored issues
show
Bug introduced by
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

54
        $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...
Bug introduced by
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

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