Completed
Push — master ( 1a9936...b9df3b )
by Marco
23:13
created

LazyLoadingMock   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 63
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A staticProxyConstructor() 0 8 1
A setProxyInitializer() 0 4 1
A getProxyInitializer() 0 4 1
A initializeProxy() 0 5 1
A isProxyInitialized() 0 4 1
A getWrappedValueHolderValue() 0 5 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types=1);
20
21
namespace ProxyManagerTestAsset;
22
23
use ProxyManager\Proxy\VirtualProxyInterface;
24
25
/**
26
 * Base test class to catch instantiations of lazy loading objects
27
 *
28
 * @author Marco Pivetta <[email protected]>
29
 * @license MIT
30
 */
31
class LazyLoadingMock implements VirtualProxyInterface
32
{
33
    /**
34
     * @var callable
35
     */
36
    public $initializer;
37
38
    /**
39
     * @param callable $initializer
40
     *
41
     * @return static
42
     */
43
    public static function staticProxyConstructor($initializer) : self
44
    {
45
        $instance = new static();
46
47
        $instance->initializer = $initializer;
48
49
        return $instance;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function setProxyInitializer(\Closure $initializer = null)
56
    {
57
        $this->initializer = $initializer;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getProxyInitializer()
64
    {
65
        return $this->initializer;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->initializer; (callable) is incompatible with the return type declared by the interface ProxyManager\Proxy\LazyL...ce::getProxyInitializer of type Closure|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...
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function initializeProxy() : bool
72
    {
73
        // empty (on purpose)
74
        return true;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function isProxyInitialized() : bool
81
    {
82
        return true;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function getWrappedValueHolderValue()
89
    {
90
        // we're not supposed to call this
91
        throw new \BadMethodCallException('Not implemented');
92
    }
93
}
94