Test Failed
Branch add-cache (2af103)
by Michael
02:53
created

CachedConfigCollection::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace micmania1\config;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
7
class CachedConfigCollection extends ConfigCollectionInterface
0 ignored issues
show
Bug introduced by
There is one abstract method delete in this class; you could implement it, or declare this class as abstract.
Loading history...
8
{
9
    /**
10
     * @var CachePoolInterface
11
     */
12
    protected $pool;
13
14
    /**
15
     * @var boolean
16
     */
17
    protected $trackMetadata = false;
18
19
    /**
20
     * @param boolean $trackHistory
0 ignored issues
show
Bug introduced by
There is no parameter named $trackHistory. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
21
     * @param CacheItemPoolInterface $pool
22
     */
23
    public function __construct(CacheItemPoolInterface $pool, $trackMetadata = false)
24
    {
25
        $this->pool = $pool;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pool of type object<Psr\Cache\CacheItemPoolInterface> is incompatible with the declared type object<micmania1\config\CachePoolInterface> of property $pool.

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...
26
        $this->trackMetadata = (bool) $trackMetadata;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function set($key, ConfigItemInterface $item)
33
    {
34
        // We use null as the key to return an empty cache item
35
        $cacheItem = $this->pool->getItem($key);
36
37
        // If we're tracking history and already have an existing record, we grab
38
        // that and set the new record.
39
        if($this->Metadata && $existing = $cacheItem->get()) {
0 ignored issues
show
Bug introduced by
The property Metadata does not seem to exist. Did you mean trackMetadata?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
40
            $existing->trackMetadata();
41
            $existing->set($item->getValue(), $item->getMetadata());
42
            $item = $existing;
43
        }
44
45
        $cacheItem->set($item);
46
47
        // We defer saving until later
48
        $this->pool->saveDeffered($cacheItem);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function get($key)
55
    {
56
        if(!$this->exists($key)) {
57
            return null;
58
        }
59
60
        $cacheItem = $this->pool->getItem($key);
61
62
        return $cacheItem->get() ?: new ConfigItem(null, [], $this->trackHistory);
0 ignored issues
show
Bug introduced by
The property trackHistory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function exists($key)
69
    {
70
        return $this->pool->hasItem($key);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function clear($key)
77
    {
78
        $this->pool->deleteItem($key);
79
    }
80
81
    /**
82
     * Commits the cache
83
     */
84
    public function __destruct()
85
    {
86
        $this->pool->commit();
87
    }
88
}
89