Failed Conditions
Push — master ( 2ade86...13f838 )
by Jonathan
18s
created

ORM/Cache/Region/DefaultMultiGetRegion.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\MultiGetCache;
24
use Doctrine\ORM\Cache\CollectionCacheEntry;
25
26
/**
27
 * A cache region that enables the retrieval of multiple elements with one call
28
 *
29
 * @since   2.5
30
 * @author  Asmir Mustafic <[email protected]>
31
 */
32
class DefaultMultiGetRegion extends DefaultRegion
33
{
34
    /**
35
     * Note that the multiple type is due to doctrine/cache not integrating the MultiGetCache interface
36
     * in its signature due to BC in 1.x
37
     *
38
     * @var MultiGetCache|\Doctrine\Common\Cache\Cache
39
     */
40
    protected $cache;
41
42
    /**
43
     * {@inheritDoc}
44
     *
45
     * @param MultiGetCache $cache
46
     */
47 137
    public function __construct($name, MultiGetCache $cache, $lifetime = 0)
48
    {
49
        /* @var $cache \Doctrine\Common\Cache\Cache */
50 137
        parent::__construct($name, $cache, $lifetime);
51 137
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 45
    public function getMultiple(CollectionCacheEntry $collection)
57
    {
58 45
        $keysToRetrieve = [];
59
60 45
        foreach ($collection->identifiers as $index => $key) {
61 43
            $keysToRetrieve[$index] = $this->getCacheEntryKey($key);
62
        }
63
64 45
        $items = $this->cache->fetchMultiple($keysToRetrieve);
65 45
        if (count($items) !== count($keysToRetrieve)) {
66 3
            return null;
67
        }
68
69 44
        $returnableItems = [];
70 44
        foreach ($keysToRetrieve as $index => $key) {
71 42
            $returnableItems[$index] = $items[$key];
72
        }
73
74 44
        return $returnableItems;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $returnableItems; (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...
75
    }
76
}
77