Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

lib/Doctrine/ORM/Cache/Region/DefaultRegion.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\ORM\Cache\Region;
22
23
use Doctrine\Common\Cache\Cache as CacheAdapter;
24
use Doctrine\Common\Cache\ClearableCache;
25
use Doctrine\ORM\Cache\CacheEntry;
26
use Doctrine\ORM\Cache\CacheKey;
27
use Doctrine\ORM\Cache\CollectionCacheEntry;
28
use Doctrine\ORM\Cache\Lock;
29
use Doctrine\ORM\Cache\Region;
30
31
/**
32
 * The simplest cache region compatible with all doctrine-cache drivers.
33
 *
34
 * @since   2.5
35
 * @author  Fabio B. Silva <[email protected]>
36
 */
37
class DefaultRegion implements Region
38
{
39
    const REGION_KEY_SEPARATOR = '_';
40
41
    /**
42
     * @var CacheAdapter
43
     */
44
    protected $cache;
45
46
    /**
47
     * @var string
48
     */
49
    protected $name;
50
51
    /**
52
     * @var integer
53
     */
54
    protected $lifetime = 0;
55
56
    /**
57
     * @param string       $name
58
     * @param CacheAdapter $cache
59
     * @param integer      $lifetime
60
     */
61 263
    public function __construct($name, CacheAdapter $cache, $lifetime = 0)
62
    {
63 263
        $this->cache    = $cache;
64 263
        $this->name     = (string) $name;
65 263
        $this->lifetime = (integer) $lifetime;
66 263
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 131
    public function getName()
72
    {
73 131
        return $this->name;
74
    }
75
76
    /**
77
     * @return \Doctrine\Common\Cache\CacheProvider
78
     */
79 6
    public function getCache()
80
    {
81 6
        return $this->cache;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 123
    public function contains(CacheKey $key)
88
    {
89 123
        return $this->cache->contains($this->getCacheEntryKey($key));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 91
    public function get(CacheKey $key)
96
    {
97 91
        return $this->cache->fetch($this->getCacheEntryKey($key)) ?: null;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function getMultiple(CollectionCacheEntry $collection)
104
    {
105 1
        $result = [];
106
107 1
        foreach ($collection->identifiers as $key) {
108 1
            $entryKey   = $this->getCacheEntryKey($key);
109 1
            $entryValue = $this->cache->fetch($entryKey);
110
111 1
            if ($entryValue === false) {
112
                return null;
113
            }
114
115 1
            $result[] = $entryValue;
116
        }
117
118 1
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type declared by the interface Doctrine\ORM\Cache\MultiGetRegion::getMultiple of type Doctrine\ORM\Cache\CacheEntry[]|null.

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...
119
    }
120
121
    /**
122
     * @param CacheKey $key
123
     * @return string
124
     */
125 133
    protected function getCacheEntryKey(CacheKey $key)
126
    {
127 133
        return $this->name . self::REGION_KEY_SEPARATOR . $key->hash;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 129
    public function put(CacheKey $key, CacheEntry $entry, Lock $lock = null)
134
    {
135 129
        return $this->cache->save($this->getCacheEntryKey($key), $entry, $this->lifetime);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 37
    public function evict(CacheKey $key)
142
    {
143 37
        return $this->cache->delete($this->getCacheEntryKey($key));
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 83
    public function evictAll()
150
    {
151 83
        if (! $this->cache instanceof ClearableCache) {
152 1
            throw new \BadMethodCallException(sprintf(
153 1
                'Clearing all cache entries is not supported by the supplied cache adapter of type %s',
154 1
                get_class($this->cache)
155
            ));
156
        }
157
158 82
        return $this->cache->deleteAll();
159
    }
160
}
161