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

buildMethodInterceptor()   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 55 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\AccessInterceptorInterface;
25
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor;
26
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor;
27
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors;
28
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors;
29
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\BindProxyProperties;
30
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\InterceptedMethod;
31
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicClone;
32
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicGet;
33
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicIsset;
34
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSet;
35
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSleep;
36
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicUnset;
37
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\StaticProxyConstructor;
38
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
39
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
40
use ReflectionClass;
41
use ReflectionMethod;
42
use Zend\Code\Generator\ClassGenerator;
43
use Zend\Code\Generator\MethodGenerator;
44
use Zend\Code\Reflection\MethodReflection;
45
46
/**
47
 * Generator for proxies implementing {@see \ProxyManager\Proxy\ValueHolderInterface}
48
 * and localizing scope of the proxied object at instantiation
49
 *
50
 * {@inheritDoc}
51
 *
52
 * @author Marco Pivetta <[email protected]>
53
 * @license MIT
54
 */
55
class AccessInterceptorScopeLocalizerGenerator implements ProxyGeneratorInterface
56
{
57 8
    /**
58
     * {@inheritDoc}
59 8
     */
60
    public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
61 6
    {
62 6
        CanProxyAssertion::assertClassCanBeProxied($originalClass, false);
63 6
64 6
        $classGenerator->setExtendedClass($originalClass->getName());
65
        $classGenerator->setImplementedInterfaces([AccessInterceptorInterface::class]);
66 6
        $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors());
67
        $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors());
68 6
69 6
        array_map(
70
            function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
71
                ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
72 6
            },
73 5
            array_merge(
74 5
                array_map(
75
                    $this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors),
76
                    ProxiedMethodsFilter::getProxiedMethods(
77
                        $originalClass,
78 6
                        ['__get', '__set', '__isset', '__unset', '__clone', '__sleep']
79 6
                    )
80
                ),
81 6
                [
82
                    new StaticProxyConstructor($originalClass, $prefixInterceptors, $suffixInterceptors),
83
                    new BindProxyProperties($originalClass, $prefixInterceptors, $suffixInterceptors),
84
                    new SetMethodPrefixInterceptor($prefixInterceptors),
85 6
                    new SetMethodSuffixInterceptor($suffixInterceptors),
86 6
                    new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors),
87 6
                    new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors),
88 6
                    new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors),
89 6
                    new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors),
90 6
                    new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors),
91 6
                    new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors),
92 6
                ]
93 6
            )
94 6
        );
95
    }
96
97
    private function buildMethodInterceptor(
98 6
        MethodPrefixInterceptors $prefixInterceptors,
99
        MethodSuffixInterceptors $suffixInterceptors
100
    ) : 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...
101
        return function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors) : InterceptedMethod {
102
            return InterceptedMethod::generateMethod(
103
                new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
104
                $prefixInterceptors,
105
                $suffixInterceptors
106
            );
107
        };
108
    }
109
}
110