Completed
Pull Request — master (#278)
by Marco
40:37 queued 39s
created

buildLazyLoadingMethodInterceptor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1
Metric Value
dl 0
loc 12
ccs 1
cts 1
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
crap 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 60 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 ProxyManager\ProxyGenerator;
22
23
use ProxyManager\Generator\Util\ClassGeneratorUtils;
24
use ProxyManager\Proxy\VirtualProxyInterface;
25
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup;
26
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
27
use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor;
28
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\GetProxyInitializer;
29
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\InitializeProxy;
30
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\IsProxyInitialized;
31
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\LazyLoadingMethodInterceptor;
32
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicClone;
33
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicGet;
34
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicIsset;
35
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicSet;
36
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicSleep;
37
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicUnset;
38
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\SetProxyInitializer;
39
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\InitializerProperty;
40
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty;
41
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
42
use ProxyManager\ProxyGenerator\Util\Properties;
43
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
44
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
45
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue;
46
use ReflectionClass;
47
use ReflectionMethod;
48
use Zend\Code\Generator\ClassGenerator;
49
use Zend\Code\Generator\MethodGenerator;
50
use Zend\Code\Reflection\MethodReflection;
51
52
/**
53
 * Generator for proxies implementing {@see \ProxyManager\Proxy\VirtualProxyInterface}
54
 *
55
 * {@inheritDoc}
56
 *
57
 * @author Marco Pivetta <[email protected]>
58
 * @license MIT
59
 */
60
class LazyLoadingValueHolderGenerator implements ProxyGeneratorInterface
61
{
62
    /**
63 8
     * {@inheritDoc}
64
     */
65 8
    public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
66
    {
67 8
        CanProxyAssertion::assertClassCanBeProxied($originalClass);
68 8
69
        $interfaces          = [VirtualProxyInterface::class];
70 8
        $publicProperties    = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass));
71 2
72
        if ($originalClass->isInterface()) {
73 6
            $interfaces[] = $originalClass->getName();
74
        } else {
75
            $classGenerator->setExtendedClass($originalClass->getName());
76 8
        }
77 8
78 8
        $classGenerator->setImplementedInterfaces($interfaces);
79 8
        $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty());
80
        $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty());
81 8
        $classGenerator->addPropertyFromGenerator($publicProperties);
82
83 8
        array_map(
84 8
            function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
85
                ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
86
            },
87 8
            array_merge(
88 5
                array_map(
89 5
                    $this->buildLazyLoadingMethodInterceptor($initializer, $valueHolder),
90
                    ProxiedMethodsFilter::getProxiedMethods($originalClass)
91
                ),
92
                [
93 8
                    new StaticProxyConstructor($initializer, Properties::fromReflectionClass($originalClass)),
94 8
                    Constructor::generateMethod($originalClass, $valueHolder),
95
                    new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties),
96
                    new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties),
97 8
                    new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties),
98 8
                    new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties),
99 8
                    new MagicClone($originalClass, $initializer, $valueHolder),
100 8
                    new MagicSleep($originalClass, $initializer, $valueHolder),
101 8
                    new MagicWakeup($originalClass),
102 8
                    new SetProxyInitializer($initializer),
103 8
                    new GetProxyInitializer($initializer),
104 8
                    new InitializeProxy($initializer, $valueHolder),
105 8
                    new IsProxyInitialized($valueHolder),
106 8
                    new GetWrappedValueHolderValue($valueHolder),
107 8
                ]
108 8
            )
109 8
        );
110 8
    }
111
112
    private function buildLazyLoadingMethodInterceptor(
113
        InitializerProperty $initializer,
114 8
        ValueHolderProperty $valueHolder
115
    ) : callable {
1 ignored issue
show
Coding Style introduced by
There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration
Loading history...
116
        return function (ReflectionMethod $method) use ($initializer, $valueHolder) : LazyLoadingMethodInterceptor {
117
            return LazyLoadingMethodInterceptor::generateMethod(
118
                new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
119
                $initializer,
120
                $valueHolder
121
            );
122
        };
123
    }
124
}
125