Completed
Pull Request — master (#326)
by Marco
07:03
created

benchInstantiationOfObjectWithPublicProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 ProxyManagerBench\Functional;
22
23
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
24
use ProxyManager\Generator\ClassGenerator;
25
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
26
use ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator;
27
use ProxyManagerTestAsset\ClassWithMixedProperties;
28
use ProxyManagerTestAsset\ClassWithPrivateProperties;
29
use ProxyManagerTestAsset\ClassWithProtectedProperties;
30
use ProxyManagerTestAsset\ClassWithPublicProperties;
31
use ProxyManagerTestAsset\EmptyClass;
32
use ReflectionClass;
33
34
/**
35
 * Benchmark that provides results for simple object instantiation for lazy loading ghost proxies
36
 *
37
 * @author Marco Pivetta <[email protected]>
38
 * @license MIT
39
 *
40
 * @BeforeMethods({"setUp"})
41
 */
42
class LazyLoadingGhostInstantiationBench
43
{
44
    /**
45
     * @var string
46
     */
47
    private $emptyClassProxy;
48
49
    /**
50
     * @var string
51
     */
52
    private $privatePropertiesProxy;
53
54
    /**
55
     * @var string
56
     */
57
    private $protectedPropertiesProxy;
58
59
    /**
60
     * @var string
61
     */
62
    private $publicPropertiesProxy;
63
64
    /**
65
     * @var string
66
     */
67
    private $mixedPropertiesProxy;
68
69
    public function setUp()
70
    {
71
        $this->emptyClassProxy          = $this->generateProxy(EmptyClass::class);
72
        $this->privatePropertiesProxy   = $this->generateProxy(ClassWithPrivateProperties::class);
73
        $this->protectedPropertiesProxy = $this->generateProxy(ClassWithProtectedProperties::class);
74
        $this->publicPropertiesProxy    = $this->generateProxy(ClassWithPublicProperties::class);
75
        $this->mixedPropertiesProxy     = $this->generateProxy(ClassWithMixedProperties::class);
76
    }
77
78
    public function benchOriginalConstructorInstantiationOfEmptyObject()
79
    {
80
        new $this->emptyClassProxy;
81
    }
82
83
    public function benchInstantiationOfEmptyObject()
84
    {
85
        ($this->emptyClassProxy)::staticProxyConstructor(function () {
1 ignored issue
show
Bug introduced by
The method staticProxyConstructor cannot be called on $this->emptyClassProxy (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
86
        });
87
    }
88
89
    public function benchOriginalConstructorInstantiationOfObjectWithPrivateProperties()
90
    {
91
        new $this->privatePropertiesProxy;
92
    }
93
94
    public function benchInstantiationOfObjectWithPrivateProperties()
95
    {
96
        ($this->privatePropertiesProxy)::staticProxyConstructor(function () {
1 ignored issue
show
Bug introduced by
The method staticProxyConstructor cannot be called on $this->privatePropertiesProxy (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
97
        });
98
    }
99
100
    public function benchOriginalConstructorInstantiationOfObjectWithProtectedProperties()
101
    {
102
        new $this->protectedPropertiesProxy;
103
    }
104
105
    public function benchInstantiationOfObjectWithProtectedProperties()
106
    {
107
        ($this->protectedPropertiesProxy)::staticProxyConstructor(function () {
1 ignored issue
show
Bug introduced by
The method staticProxyConstructor cannot be called on $this->protectedPropertiesProxy (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
108
        });
109
    }
110
111
    public function benchOriginalConstructorInstantiationOfObjectWithPublicProperties()
112
    {
113
        new $this->publicPropertiesProxy;
114
    }
115
116
    public function benchInstantiationOfObjectWithPublicProperties()
117
    {
118
        ($this->publicPropertiesProxy)::staticProxyConstructor(function () {
1 ignored issue
show
Bug introduced by
The method staticProxyConstructor cannot be called on $this->publicPropertiesProxy (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
119
        });
120
    }
121
122
    public function benchOriginalConstructorInstantiationOfObjectWithMixedProperties()
123
    {
124
        new $this->mixedPropertiesProxy;
125
    }
126
127
    public function benchInstantiationOfObjectWithMixedProperties()
128
    {
129
        ($this->mixedPropertiesProxy)::staticProxyConstructor(function () {
1 ignored issue
show
Bug introduced by
The method staticProxyConstructor cannot be called on $this->mixedPropertiesProxy (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
130
        });
131
    }
132
133
    private function generateProxy(string $originalClass) : string
134
    {
135
        $generatedClassName = __CLASS__ . '\\' . $originalClass;
136
137
        if (class_exists($generatedClassName)) {
138
            return $generatedClassName;
139
        }
140
141
        $generatedClass     = new ClassGenerator($generatedClassName);
142
143
        (new LazyLoadingGhostGenerator())->generate(new ReflectionClass($originalClass), $generatedClass, []);
144
        (new EvaluatingGeneratorStrategy())->generate($generatedClass);
145
146
        return $generatedClassName;
147
    }
148
}
149