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; |
|
|
|
|
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
|
|
|
|
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.