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
|
|||
75 | } |
||
76 | } |
||
77 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.