InterceptorGenerator   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 4
dl 0
loc 58
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A createInterceptedMethodBody() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\Util;
6
7
use Laminas\Code\Generator\ParameterGenerator;
8
use Laminas\Code\Generator\PropertyGenerator;
9
use ProxyManager\Generator\MethodGenerator;
10
use ProxyManager\Generator\Util\ProxiedMethodReturnExpression;
11
use ReflectionMethod;
12
use function array_keys;
13
use function array_map;
14
use function implode;
15
use function str_replace;
16
use function var_export;
17
18
/**
19
 * Utility to create pre- and post- method interceptors around a given method body
20
 *
21
 * @private - this class is just here as a small utility for this component, don't use it in your own code
22
 */
23
class InterceptorGenerator
24
{
25
    private const TEMPLATE = <<<'PHP'
26
if (isset($this->{{$prefixInterceptorsName}}[{{$name}}])) {
27
    $returnEarly       = false;
28
    $prefixReturnValue = $this->{{$prefixInterceptorsName}}[{{$name}}]->__invoke($this, $this, {{$name}}, {{$paramsString}}, $returnEarly);
29
30
    if ($returnEarly) {
31
        {{$prefixEarlyReturnExpression}}
32
    }
33
}
34
35
{{$methodBody}}
36
37
if (isset($this->{{$suffixInterceptorsName}}[{{$name}}])) {
38
    $returnEarly       = false;
39
    $suffixReturnValue = $this->{{$suffixInterceptorsName}}[{{$name}}]->__invoke($this, $this, {{$name}}, {{$paramsString}}, $returnValue, $returnEarly);
40
41
    if ($returnEarly) {
42
        {{$suffixEarlyReturnExpression}}
43
    }
44
}
45
46
{{$returnExpression}}
47
PHP;
48
49
    /**
50
     * @param string $methodBody the body of the previously generated code.
51
     *                           It MUST assign the return value to a variable
52
     *                           `$returnValue` instead of directly returning
53 3
     */
54
    public static function createInterceptedMethodBody(
55
        string $methodBody,
56
        MethodGenerator $method,
57
        PropertyGenerator $prefixInterceptors,
58
        PropertyGenerator $suffixInterceptors,
59
        ?ReflectionMethod $originalMethod
60
    ) : string {
61 3
        $replacements = [
62 3
            '{{$name}}'                        => var_export($method->getName(), true),
63 3
            '{{$prefixInterceptorsName}}'      => $prefixInterceptors->getName(),
64 3
            '{{$prefixEarlyReturnExpression}}' => ProxiedMethodReturnExpression::generate('$prefixReturnValue', $originalMethod),
65 3
            '{{$methodBody}}'                  => $methodBody,
66 3
            '{{$suffixInterceptorsName}}'      => $suffixInterceptors->getName(),
67 3
            '{{$suffixEarlyReturnExpression}}' => ProxiedMethodReturnExpression::generate('$suffixReturnValue', $originalMethod),
68
            '{{$returnExpression}}'            => ProxiedMethodReturnExpression::generate('$returnValue', $originalMethod),
69 3
            '{{$paramsString}}'                => 'array(' . implode(', ', array_map(static function (ParameterGenerator $parameter) : string {
70 3
                return var_export($parameter->getName(), true) . ' => $' . $parameter->getName();
71
            }, $method->getParameters())) . ')',
72
        ];
73 3
74 3
        return str_replace(
75 3
            array_keys($replacements),
76 3
            $replacements,
77
            self::TEMPLATE
78
        );
79
    }
80
}
81