Completed
Push — master ( 0f341b...3e6c76 )
by Tobias
21:32
created

FixedTaggingCachePool::invalidateTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\Cache;
13
14
use Cache\TagInterop\TaggableCacheItemInterface;
15
use Cache\TagInterop\TaggableCacheItemPoolInterface;
16
use Psr\Cache\CacheItemInterface;
17
use Psr\Cache\InvalidArgumentException;
18
19
/**
20
 * This class is a decorator for a TaggableCacheItemPoolInterface. It tags everything with predefined tags.
21
 *
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
class FixedTaggingCachePool implements TaggableCacheItemPoolInterface
25
{
26
    /**
27
     * @type TaggableCacheItemPoolInterface
28
     */
29
    private $cache;
30
31
    /**
32
     * @type array
33
     */
34
    private $tags;
35
36
    /**
37
     * @param TaggableCacheItemPoolInterface $cache
38
     * @param array                          $tags
39
     */
40
    public function __construct(TaggableCacheItemPoolInterface $cache, array $tags)
41
    {
42
        $this->cache = $cache;
43
        $this->tags  = $tags;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getItem($key)
50
    {
51
        return $this->cache->getItem($key);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getItems(array $keys = [])
58
    {
59
        return $this->cache->getItems($keys);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function hasItem($key)
66
    {
67
        return $this->cache->hasItem($key);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function clear()
74
    {
75
        return $this->cache->clear();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function deleteItem($key)
82
    {
83
        return $this->cache->deleteItem($key);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function deleteItems(array $keys)
90
    {
91
        return $this->cache->deleteItems($keys);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 View Code Duplication
    public function save(CacheItemInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        if (!$item instanceof TaggableCacheItemInterface) {
100
            throw new InvalidArgumentException('Cache items are not transferable between pools. Item MUST implement TaggableCacheItemInterface.');
0 ignored issues
show
Unused Code introduced by
The call to InvalidArgumentException::__construct() has too many arguments starting with 'Cache items are not tra...bleCacheItemInterface.'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
        }
102
103
        $item->setTags($this->tags);
104
105
        return $this->cache->save($item);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 View Code Duplication
    public function saveDeferred(CacheItemInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        if (!$item instanceof TaggableCacheItemInterface) {
114
            throw new InvalidArgumentException('Cache items are not transferable between pools. Item MUST implement TaggableCacheItemInterface.');
0 ignored issues
show
Unused Code introduced by
The call to InvalidArgumentException::__construct() has too many arguments starting with 'Cache items are not tra...bleCacheItemInterface.'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
        }
116
117
        $item->setTags($this->tags);
118
119
        return $this->cache->saveDeferred($item);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function commit()
126
    {
127
        return $this->cache->commit();
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function invalidateTag($tag)
134
    {
135
        return $this->invalidateTag($tag);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function invalidateTags(array $tags)
142
    {
143
        return $this->cache - $this->invalidateTags($tags);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->cache - $t...>invalidateTags($tags); (integer|double) is incompatible with the return type declared by the interface Cache\TagInterop\Taggabl...terface::invalidateTags of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
144
    }
145
}
146