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

buildMethodInterceptor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 14
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 3
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\AccessInterceptorValueHolderInterface;
25
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup;
26
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor;
27
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor;
28
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors;
29
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors;
30
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\InterceptedMethod;
31
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicClone;
32
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicGet;
33
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicIsset;
34
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicSet;
35
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicUnset;
36
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\StaticProxyConstructor;
37
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
38
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty;
39
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
40
use ProxyManager\ProxyGenerator\Util\Properties;
41
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
42
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
43
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue;
44
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\MagicSleep;
45
use ReflectionClass;
46
use ReflectionMethod;
47
use Zend\Code\Generator\ClassGenerator;
48
use Zend\Code\Generator\MethodGenerator;
49
use Zend\Code\Reflection\MethodReflection;
50
51
/**
52
 * Generator for proxies implementing {@see \ProxyManager\Proxy\ValueHolderInterface}
53
 * and {@see \ProxyManager\Proxy\AccessInterceptorInterface}
54
 *
55
 * {@inheritDoc}
56
 *
57
 * @author Marco Pivetta <[email protected]>
58
 * @license MIT
59
 */
60
class AccessInterceptorValueHolderGenerator implements ProxyGeneratorInterface
61
{
62
    /**
63
     * {@inheritDoc}
64 8
     */
65
    public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
66 8
    {
67
        CanProxyAssertion::assertClassCanBeProxied($originalClass);
68 8
69 8
        $publicProperties = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass));
70
        $interfaces       = [AccessInterceptorValueHolderInterface::class];
71 8
72 2
        if ($originalClass->isInterface()) {
73
            $interfaces[] = $originalClass->getName();
74 6
        } else {
75
            $classGenerator->setExtendedClass($originalClass->getName());
76
        }
77 8
78 8
        $classGenerator->setImplementedInterfaces($interfaces);
79 8
        $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty());
80 8
        $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors());
81 8
        $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors());
82
        $classGenerator->addPropertyFromGenerator($publicProperties);
83 8
84
        array_map(
85 8
            function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
86 8
                ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
87
            },
88
            array_merge(
89 8
                array_map(
90 5
                    $this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors, $valueHolder),
91 5
                    ProxiedMethodsFilter::getProxiedMethods($originalClass)
92
                ),
93
                [
94
                    Constructor::generateMethod($originalClass, $valueHolder),
95
                    new StaticProxyConstructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors),
96 8
                    new GetWrappedValueHolderValue($valueHolder),
97 8
                    new SetMethodPrefixInterceptor($prefixInterceptors),
98
                    new SetMethodSuffixInterceptor($suffixInterceptors),
99
                    new MagicGet(
100 8
                        $originalClass,
101 8
                        $valueHolder,
102 8
                        $prefixInterceptors,
103 8
                        $suffixInterceptors,
104 8
                        $publicProperties
105 8
                    ),
106
                    new MagicSet(
107
                        $originalClass,
108
                        $valueHolder,
109
                        $prefixInterceptors,
110
                        $suffixInterceptors,
111
                        $publicProperties
112 8
                    ),
113
                    new MagicIsset(
114
                        $originalClass,
115
                        $valueHolder,
116
                        $prefixInterceptors,
117
                        $suffixInterceptors,
118
                        $publicProperties
119 8
                    ),
120
                    new MagicUnset(
121
                        $originalClass,
122
                        $valueHolder,
123
                        $prefixInterceptors,
124
                        $suffixInterceptors,
125
                        $publicProperties
126 8
                    ),
127
                    new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors),
128
                    new MagicSleep($originalClass, $valueHolder),
129
                    new MagicWakeup($originalClass, $valueHolder),
130
                ]
131
            )
132
        );
133 8
    }
134 8
135 8
    private function buildMethodInterceptor(
136
        MethodPrefixInterceptors $prefixes,
137
        MethodSuffixInterceptors $suffixes,
138
        ValueHolderProperty $valueHolder
139 8
    ) : 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...
140
        return function (ReflectionMethod $method) use ($prefixes, $suffixes, $valueHolder) : InterceptedMethod {
141
            return InterceptedMethod::generateMethod(
142
                new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
143
                $valueHolder,
144
                $prefixes,
145
                $suffixes
146
            );
147
        };
148
    }
149
}
150