Completed
Pull Request — master (#359)
by Jefersson
06:36
created

InterceptorGenerator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 0
loc 55
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B createInterceptedMethodBody() 0 40 2
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\AccessInterceptorValueHolder\MethodGenerator\Util;
22
23
use ProxyManager\Generator\MethodGenerator;
24
use ProxyManager\Generator\Util\ProxiedMethodReturnExpression;
25
use Zend\Code\Generator\PropertyGenerator;
26
27
/**
28
 * Utility to create pre- and post- method interceptors around a given method body
29
 *
30
 * @author Marco Pivetta <[email protected]>
31
 * @license MIT
32
 *
33
 * @private - this class is just here as a small utility for this component, don't use it in your own code
34
 */
35
class InterceptorGenerator
36
{
37
    /**
38
     * @param string                                  $methodBody         the body of the previously generated code.
39
     *                                                                    It MUST assign the return value to a variable
40
     *                                                                    `$returnValue` instead of directly returning
41
     * @param \ProxyManager\Generator\MethodGenerator $method
42
     * @param \Zend\Code\Generator\PropertyGenerator  $valueHolder
43
     * @param \Zend\Code\Generator\PropertyGenerator  $prefixInterceptors
44
     * @param \Zend\Code\Generator\PropertyGenerator  $suffixInterceptors
45
     * @param \ReflectionMethod|null                  $originalMethod
46
     *
47 1
     * @return string
48
     */
49
    public static function createInterceptedMethodBody(
50
        string $methodBody,
51
        MethodGenerator $method,
52
        PropertyGenerator $valueHolder,
53
        PropertyGenerator $prefixInterceptors,
54 1
        PropertyGenerator $suffixInterceptors,
55 1
        ?\ReflectionMethod $originalMethod
56 1
    ) : string {
57 1
        $name                   = var_export($method->getName(), true);
58 1
        $valueHolderName        = $valueHolder->getName();
59
        $prefixInterceptorsName = $prefixInterceptors->getName();
60 1
        $suffixInterceptorsName = $suffixInterceptors->getName();
61 1
        $params                 = [];
62 1
63
        foreach ($method->getParameters() as $parameter) {
64
            $parameterName = $parameter->getName();
65 1
            $params[]      = var_export($parameterName, true) . ' => $' . $parameter->getName();
66
        }
67 1
68 1
        $paramsString = 'array(' . implode(', ', $params) . ')';
69 1
70 1
        return "if (isset(\$this->$prefixInterceptorsName" . "[$name])) {\n"
71 1
            . "    \$returnEarly       = false;\n"
72 1
            . "    \$prefixReturnValue = \$this->$prefixInterceptorsName" . "[$name]->__invoke("
73 1
            . "\$this, \$this->$valueHolderName, $name, $paramsString, \$returnEarly);\n\n"
74 1
            . "    if (\$returnEarly) {\n"
75 1
            . '        ' . ProxiedMethodReturnExpression::generate('$prefixReturnValue', $originalMethod) . "\n"
76 1
            . "    }\n"
77 1
            . "}\n\n"
78 1
            . $methodBody . "\n\n"
79 1
            . "if (isset(\$this->$suffixInterceptorsName" . "[$name])) {\n"
80 1
            . "    \$returnEarly       = false;\n"
81 1
            . "    \$suffixReturnValue = \$this->$suffixInterceptorsName" . "[$name]->__invoke("
82 1
            . "\$this, \$this->$valueHolderName, $name, $paramsString, \$returnValue, \$returnEarly);\n\n"
83 1
            . "    if (\$returnEarly) {\n"
84 1
            . '        ' . ProxiedMethodReturnExpression::generate('$suffixReturnValue', $originalMethod) . "\n"
85
            . "    }\n"
86
            . "}\n\n"
87
            . ProxiedMethodReturnExpression::generate('$returnValue', $originalMethod);
88
    }
89
}
90