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

LazyLoadingValueHolderInstantiationBench   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 97
rs 10
c 0
b 0
f 0
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\ProxyGenerator\LazyLoadingValueHolderGenerator;
11
use ProxyManagerTestAsset\ClassWithMixedProperties;
12
use ProxyManagerTestAsset\ClassWithPrivateProperties;
13
use ProxyManagerTestAsset\ClassWithProtectedProperties;
14
use ProxyManagerTestAsset\ClassWithPublicProperties;
15
use ProxyManagerTestAsset\EmptyClass;
16
use ReflectionClass;
17
use function class_exists;
18
19
/**
20
 * Benchmark that provides results for simple object instantiation for lazy loading value holder proxies
21
 *
22
 * @BeforeMethods({"setUp"})
23
 */
24
final class LazyLoadingValueHolderInstantiationBench
25
{
26
    private string $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...
27
    private string $privatePropertiesProxy;
28
    private string $protectedPropertiesProxy;
29
    private string $publicPropertiesProxy;
30
    private string $mixedPropertiesProxy;
31
32
    public function setUp() : void
33
    {
34
        $this->emptyClassProxy          = $this->generateProxy(EmptyClass::class);
35
        $this->privatePropertiesProxy   = $this->generateProxy(ClassWithPrivateProperties::class);
36
        $this->protectedPropertiesProxy = $this->generateProxy(ClassWithProtectedProperties::class);
37
        $this->publicPropertiesProxy    = $this->generateProxy(ClassWithPublicProperties::class);
38
        $this->mixedPropertiesProxy     = $this->generateProxy(ClassWithMixedProperties::class);
39
    }
40
41
    public function benchOriginalConstructorInstantiationOfEmptyObject() : void
42
    {
43
        new $this->emptyClassProxy();
44
    }
45
46
    public function benchInstantiationOfEmptyObject() : void
47
    {
48
        $this->emptyClassProxy::staticProxyConstructor(static function () : void {
49
        });
50
    }
51
52
    public function benchOriginalConstructorInstantiationOfObjectWithPrivateProperties() : void
53
    {
54
        new $this->privatePropertiesProxy();
55
    }
56
57
    public function benchInstantiationOfObjectWithPrivateProperties() : void
58
    {
59
        $this->privatePropertiesProxy::staticProxyConstructor(static function () : void {
60
        });
61
    }
62
63
    public function benchOriginalConstructorInstantiationOfObjectWithProtectedProperties() : void
64
    {
65
        new $this->protectedPropertiesProxy();
66
    }
67
68
    public function benchInstantiationOfObjectWithProtectedProperties() : void
69
    {
70
        $this->protectedPropertiesProxy::staticProxyConstructor(static function () : void {
71
        });
72
    }
73
74
    public function benchOriginalConstructorInstantiationOfObjectWithPublicProperties() : void
75
    {
76
        new $this->publicPropertiesProxy();
77
    }
78
79
    public function benchInstantiationOfObjectWithPublicProperties() : void
80
    {
81
        $this->publicPropertiesProxy::staticProxyConstructor(static function () : void {
82
        });
83
    }
84
85
    public function benchOriginalConstructorInstantiationOfObjectWithMixedProperties() : void
86
    {
87
        new $this->mixedPropertiesProxy();
88
    }
89
90
    public function benchInstantiationOfObjectWithMixedProperties() : void
91
    {
92
        $this->mixedPropertiesProxy::staticProxyConstructor(static function () : void {
93
        });
94
    }
95
96
    private function generateProxy(string $originalClass) : string
97
    {
98
        $generatedClassName = self::class . '\\' . $originalClass;
99
100
        if (class_exists($generatedClassName)) {
101
            return $generatedClassName;
102
        }
103
104
        $generatedClass = new ClassGenerator($generatedClassName);
105
106
        (new LazyLoadingValueHolderGenerator())->generate(new ReflectionClass($originalClass), $generatedClass);
107
        (new EvaluatingGeneratorStrategy())->generate($generatedClass);
108
109
        return $generatedClassName;
110
    }
111
}
112