Completed
Push — master ( 1a9936...b9df3b )
by Marco
23:13
created

AccessInterceptorValueHolderGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 69
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 2
Metric Value
dl 0
loc 69
ccs 29
cts 29
cp 1
rs 9.2083
cc 2
eloc 52
nc 2
nop 2
crap 2

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
 * 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