Completed
Push — master ( 3be072...ba2d3a )
by Marco
231:32 queued 209:55
created

LazyLoadingGhostPropertyAccessBench::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 8.8945
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManagerBench;
6
7
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
8
use ProxyManager\Generator\ClassGenerator;
9
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
10
use ProxyManager\Proxy\LazyLoadingInterface;
11
use ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator;
12
use ProxyManagerTestAsset\ClassWithMixedProperties;
13
use ProxyManagerTestAsset\ClassWithPrivateProperties;
14
use ProxyManagerTestAsset\ClassWithProtectedProperties;
15
use ProxyManagerTestAsset\ClassWithPublicProperties;
16
use ProxyManagerTestAsset\EmptyClass;
17
use ReflectionClass;
18
use ReflectionProperty;
19
use function assert;
20
use function class_exists;
21
22
/**
23
 * Benchmark that provides results for simple initialization/state access for lazy loading ghost proxies
24
 *
25
 * @BeforeMethods({"setUp"})
26
 */
27
final class LazyLoadingGhostPropertyAccessBench
28
{
29
    /** @var EmptyClass&LazyLoadingInterface */
30
    private EmptyClass $emptyClassProxy;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
31
32
    /** @var EmptyClass&LazyLoadingInterface */
33
    private EmptyClass $initializedEmptyClassProxy;
34
35
    /** @var ClassWithPrivateProperties&LazyLoadingInterface */
36
    private ClassWithPrivateProperties $privatePropertiesProxy;
37
38
    /** @var ClassWithPrivateProperties&LazyLoadingInterface */
39
    private ClassWithPrivateProperties $initializedPrivatePropertiesProxy;
40
    private ReflectionProperty $accessPrivateProperty;
41
42
    /** @var ClassWithProtectedProperties&LazyLoadingInterface */
43
    private ClassWithProtectedProperties $protectedPropertiesProxy;
44
45
    /** @var ClassWithProtectedProperties&LazyLoadingInterface */
46
    private ClassWithProtectedProperties $initializedProtectedPropertiesProxy;
47
    private ReflectionProperty $accessProtectedProperty;
48
49
    /** @var ClassWithPublicProperties&LazyLoadingInterface */
50
    private ClassWithPublicProperties $publicPropertiesProxy;
51
52
    /** @var ClassWithPublicProperties&LazyLoadingInterface */
53
    private ClassWithPublicProperties $initializedPublicPropertiesProxy;
54
55
    /** @var ClassWithMixedProperties&LazyLoadingInterface */
56
    private ClassWithMixedProperties $mixedPropertiesProxy;
57
58
    /** @var ClassWithMixedProperties&LazyLoadingInterface */
59
    private ClassWithMixedProperties $initializedMixedPropertiesProxy;
60
61
    public function setUp() : void
62
    {
63
        $emptyClassProxy                     = $this->buildProxy(EmptyClass::class);
64
        $privatePropertiesProxy              = $this->buildProxy(ClassWithPrivateProperties::class);
65
        $protectedPropertiesProxy            = $this->buildProxy(ClassWithProtectedProperties::class);
66
        $publicPropertiesProxy               = $this->buildProxy(ClassWithPublicProperties::class);
67
        $mixedPropertiesProxy                = $this->buildProxy(ClassWithMixedProperties::class);
68
        $initializedEmptyClassProxy          = $this->buildProxy(EmptyClass::class);
69
        $initializedPrivatePropertiesProxy   = $this->buildProxy(ClassWithPrivateProperties::class);
70
        $initializedProtectedPropertiesProxy = $this->buildProxy(ClassWithProtectedProperties::class);
71
        $initializedPublicPropertiesProxy    = $this->buildProxy(ClassWithPublicProperties::class);
72
        $initializedMixedPropertiesProxy     = $this->buildProxy(ClassWithMixedProperties::class);
73
74
        assert($emptyClassProxy instanceof LazyLoadingInterface);
75
        assert($privatePropertiesProxy instanceof LazyLoadingInterface);
76
        assert($protectedPropertiesProxy instanceof LazyLoadingInterface);
77
        assert($publicPropertiesProxy instanceof LazyLoadingInterface);
78
        assert($mixedPropertiesProxy instanceof LazyLoadingInterface);
79
        assert($initializedEmptyClassProxy instanceof LazyLoadingInterface);
80
        assert($initializedPrivatePropertiesProxy instanceof LazyLoadingInterface);
81
        assert($initializedProtectedPropertiesProxy instanceof LazyLoadingInterface);
82
        assert($initializedPublicPropertiesProxy instanceof LazyLoadingInterface);
83
        assert($initializedMixedPropertiesProxy instanceof LazyLoadingInterface);
84
85
        assert($emptyClassProxy instanceof EmptyClass);
86
        assert($privatePropertiesProxy instanceof ClassWithPrivateProperties);
87
        assert($protectedPropertiesProxy instanceof ClassWithProtectedProperties);
88
        assert($publicPropertiesProxy instanceof ClassWithPublicProperties);
89
        assert($mixedPropertiesProxy instanceof ClassWithMixedProperties);
90
        assert($initializedEmptyClassProxy instanceof EmptyClass);
91
        assert($initializedPrivatePropertiesProxy instanceof ClassWithPrivateProperties);
92
        assert($initializedProtectedPropertiesProxy instanceof ClassWithProtectedProperties);
93
        assert($initializedPublicPropertiesProxy instanceof ClassWithPublicProperties);
94
        assert($initializedMixedPropertiesProxy instanceof ClassWithMixedProperties);
95
96
        $this->emptyClassProxy          = $emptyClassProxy;
97
        $this->privatePropertiesProxy   = $privatePropertiesProxy;
98
        $this->protectedPropertiesProxy = $protectedPropertiesProxy;
99
        $this->publicPropertiesProxy    = $publicPropertiesProxy;
100
        $this->mixedPropertiesProxy     = $mixedPropertiesProxy;
101
102
        $this->initializedEmptyClassProxy          = $initializedEmptyClassProxy;
103
        $this->initializedPrivatePropertiesProxy   = $initializedPrivatePropertiesProxy;
104
        $this->initializedProtectedPropertiesProxy = $initializedProtectedPropertiesProxy;
105
        $this->initializedPublicPropertiesProxy    = $initializedPublicPropertiesProxy;
106
        $this->initializedMixedPropertiesProxy     = $initializedMixedPropertiesProxy;
107
108
        $this->initializedEmptyClassProxy->initializeProxy();
109
        $this->initializedPrivatePropertiesProxy->initializeProxy();
110
        $this->initializedProtectedPropertiesProxy->initializeProxy();
111
        $this->initializedPublicPropertiesProxy->initializeProxy();
112
        $this->initializedMixedPropertiesProxy->initializeProxy();
113
114
        $this->accessPrivateProperty = new ReflectionProperty(ClassWithPrivateProperties::class, 'property0');
115
        $this->accessPrivateProperty->setAccessible(true);
116
117
        $this->accessProtectedProperty = new ReflectionProperty(ClassWithProtectedProperties::class, 'property0');
118
        $this->accessProtectedProperty->setAccessible(true);
119
    }
120
121
    public function benchEmptyClassInitialization() : void
122
    {
123
        $this->emptyClassProxy->initializeProxy();
124
    }
125
126
    public function benchInitializedEmptyClassInitialization() : void
127
    {
128
        $this->initializedEmptyClassProxy->initializeProxy();
129
    }
130
131
    public function benchObjectWithPrivatePropertiesInitialization() : void
132
    {
133
        $this->privatePropertiesProxy->initializeProxy();
134
    }
135
136
    public function benchInitializedObjectWithPrivatePropertiesInitialization() : void
137
    {
138
        $this->initializedPrivatePropertiesProxy->initializeProxy();
139
    }
140
141
    public function benchObjectWithPrivatePropertiesPropertyRead() : void
142
    {
143
        $this->accessPrivateProperty->getValue($this->privatePropertiesProxy);
144
    }
145
146
    public function benchInitializedObjectWithPrivatePropertiesPropertyRead() : void
147
    {
148
        $this->accessPrivateProperty->getValue($this->initializedPrivatePropertiesProxy);
149
    }
150
151
    public function benchObjectWithPrivatePropertiesPropertyWrite() : void
152
    {
153
        $this->accessPrivateProperty->setValue($this->privatePropertiesProxy, 'foo');
154
    }
155
156
    public function benchInitializedObjectWithPrivatePropertiesPropertyWrite() : void
157
    {
158
        $this->accessPrivateProperty->setValue($this->initializedPrivatePropertiesProxy, 'foo');
159
    }
160
161
    public function benchObjectWithProtectedPropertiesInitialization() : void
162
    {
163
        $this->protectedPropertiesProxy->initializeProxy();
164
    }
165
166
    public function benchInitializedObjectWithProtectedPropertiesInitialization() : void
167
    {
168
        $this->initializedProtectedPropertiesProxy->initializeProxy();
169
    }
170
171
    public function benchObjectWithProtectedPropertiesPropertyRead() : void
172
    {
173
        $this->accessProtectedProperty->getValue($this->protectedPropertiesProxy);
174
    }
175
176
    public function benchInitializedObjectWithProtectedPropertiesPropertyRead() : void
177
    {
178
        $this->accessProtectedProperty->getValue($this->initializedProtectedPropertiesProxy);
179
    }
180
181
    public function benchObjectWithProtectedPropertiesPropertyWrite() : void
182
    {
183
        $this->accessProtectedProperty->setValue($this->protectedPropertiesProxy, 'foo');
184
    }
185
186
    public function benchInitializedObjectWithProtectedPropertiesPropertyWrite() : void
187
    {
188
        $this->accessProtectedProperty->setValue($this->initializedProtectedPropertiesProxy, 'foo');
189
    }
190
191
    public function benchObjectWithPublicPropertiesInitialization() : void
192
    {
193
        $this->publicPropertiesProxy->initializeProxy();
194
    }
195
196
    public function benchInitializedObjectWithPublicPropertiesInitialization() : void
197
    {
198
        $this->initializedPublicPropertiesProxy->initializeProxy();
199
    }
200
201
    public function benchObjectWithPublicPropertiesPropertyRead() : void
202
    {
203
        $this->publicPropertiesProxy->property0;
204
    }
205
206
    public function benchInitializedObjectWithPublicPropertiesPropertyRead() : void
207
    {
208
        $this->initializedPublicPropertiesProxy->property0;
209
    }
210
211
    public function benchObjectWithPublicPropertiesPropertyWrite() : void
212
    {
213
        $this->publicPropertiesProxy->property0 = 'foo';
214
    }
215
216
    public function benchInitializedObjectWithPublicPropertiesPropertyWrite() : void
217
    {
218
        $this->initializedPublicPropertiesProxy->property0 = 'foo';
219
    }
220
221
    public function benchObjectWithPublicPropertiesPropertyIsset() : void
222
    {
223
        /* @noinspection PhpExpressionResultUnusedInspection */
224
        /* @noinspection UnSafeIsSetOverArrayInspection */
225
        isset($this->publicPropertiesProxy->property0);
226
    }
227
228
    public function benchInitializedObjectWithPublicPropertiesPropertyIsset() : void
229
    {
230
        /* @noinspection PhpExpressionResultUnusedInspection */
231
        /* @noinspection UnSafeIsSetOverArrayInspection */
232
        isset($this->initializedPublicPropertiesProxy->property0);
233
    }
234
235
    public function benchObjectWithPublicPropertiesPropertyUnset() : void
236
    {
237
        unset($this->publicPropertiesProxy->property0);
238
    }
239
240
    public function benchInitializedObjectWithPublicPropertiesPropertyUnset() : void
241
    {
242
        unset($this->initializedPublicPropertiesProxy->property0);
243
    }
244
245
    public function benchObjectWithMixedPropertiesInitialization() : void
246
    {
247
        $this->mixedPropertiesProxy->initializeProxy();
248
    }
249
250
    public function benchInitializedObjectWithMixedPropertiesInitialization() : void
251
    {
252
        $this->initializedMixedPropertiesProxy->initializeProxy();
253
    }
254
255
    public function benchObjectWithMixedPropertiesPropertyRead() : void
256
    {
257
        $this->mixedPropertiesProxy->publicProperty0;
258
    }
259
260
    public function benchInitializedObjectWithMixedPropertiesPropertyRead() : void
261
    {
262
        $this->initializedMixedPropertiesProxy->publicProperty0;
263
    }
264
265
    public function benchObjectWithMixedPropertiesPropertyWrite() : void
266
    {
267
        $this->mixedPropertiesProxy->publicProperty0 = 'foo';
268
    }
269
270
    public function benchInitializedObjectWithMixedPropertiesPropertyWrite() : void
271
    {
272
        $this->initializedMixedPropertiesProxy->publicProperty0 = 'foo';
273
    }
274
275
    public function benchObjectWithMixedPropertiesPropertyIsset() : void
276
    {
277
        /* @noinspection PhpExpressionResultUnusedInspection */
278
        /* @noinspection UnSafeIsSetOverArrayInspection */
279
        isset($this->mixedPropertiesProxy->publicProperty0);
280
    }
281
282
    public function benchInitializedObjectWithMixedPropertiesPropertyIsset() : void
283
    {
284
        /* @noinspection PhpExpressionResultUnusedInspection */
285
        /* @noinspection UnSafeIsSetOverArrayInspection */
286
        isset($this->initializedMixedPropertiesProxy->publicProperty0);
287
    }
288
289
    public function benchObjectWithMixedPropertiesPropertyUnset() : void
290
    {
291
        unset($this->mixedPropertiesProxy->publicProperty0);
292
    }
293
294
    public function benchInitializedObjectWithMixedPropertiesPropertyUnset() : void
295
    {
296
        unset($this->initializedMixedPropertiesProxy->publicProperty0);
297
    }
298
299
    private function buildProxy(string $originalClass) : LazyLoadingInterface
300
    {
301
        return $this->generateProxyClass($originalClass)::staticProxyConstructor(
302
            static function ($proxy, string $method, $params, & $initializer) : bool {
303
                $initializer = null;
304
305
                return true;
306
            }
307
        );
308
    }
309
310
    private function generateProxyClass(string $originalClassName) : string
311
    {
312
        $generatedClassName = self::class . '\\' . $originalClassName;
313
314
        if (class_exists($generatedClassName)) {
315
            return $generatedClassName;
316
        }
317
318
        $generatedClass = new ClassGenerator($generatedClassName);
319
320
        (new LazyLoadingGhostGenerator())->generate(new ReflectionClass($originalClassName), $generatedClass, []);
321
        (new EvaluatingGeneratorStrategy())->generate($generatedClass);
322
323
        return $generatedClassName;
324
    }
325
}
326