MethodProxy::generateMethod()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
rs 8.8571
cc 2
eloc 26
nc 2
nop 4
1
<?php
2
3
namespace Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator;
4
5
use ProxyManager\Generator\MethodGenerator;
6
use Zend\Code\Generator\PropertyGenerator;
7
use Zend\Code\Reflection\MethodReflection;
8
9
class MethodProxy extends MethodGenerator
10
{
11
    /**
12
     * @param \Zend\Code\Reflection\MethodReflection $originalMethod
13
     * @param \Zend\Code\Generator\PropertyGenerator $wrappedObjectProperty
14
     * @param \Zend\Code\Generator\PropertyGenerator $lazyPropertiesProperty
15
     * @param PropertyGenerator $initializerProperty
16
     * @return MethodProxy
17
     */
18
    public static function generateMethod(
19
        MethodReflection $originalMethod,
20
        PropertyGenerator $wrappedObjectProperty,
21
        PropertyGenerator $lazyPropertiesProperty,
22
        PropertyGenerator $initializerProperty
23
    ) {
24
        $method          = static::fromReflection($originalMethod);
25
        $forwardedParams = [];
26
        $params          = [];
27
28
        foreach ($originalMethod->getParameters() as $parameter) {
29
            $paramName = '$' . $parameter->name;
30
            $forwardedParams[] = $paramName;
31
            $params[] = var_export($parameter->name, true) . ' => ' . $paramName;
32
        }
33
34
        $paramsString = 'array(' . implode(', ', $params) . ')';
35
36
        $methodBody = '$this->' . $initializerProperty->getName() . "->initialize(\$this->" . $lazyPropertiesProperty->getName() . ", \"" . $method->getName() . "\", \$this->" . $wrappedObjectProperty->getName() . ");\n\n"
37
            . 'if ($this->hasMethodReplacement("' . $method->getName() . '")) {' . "\n"
38
            . '    return $this->getMethodReplacement("' . $method->getName() .'")->getReplacement()->execute($this, "' . $method->getName() . '", ' . $paramsString . ');' . "\n"
39
            . '}' . "\n\n"
40
            . '$result = $this->' . $wrappedObjectProperty->getName() . '->' . $method->getName() .  '(' . implode(', ', $forwardedParams) . ');' . "\n"
41
            . 'if ($result === $this->' . $wrappedObjectProperty->getName() . ') {' ."\n"
42
            . '    return $this;' . "\n"
43
            . "}\n\n"
44
            . 'return $result;';
45
46
47
        $method->setDocblock('{@inheritDoc}');
48
        $method->setBody(
49
            $methodBody
50
        );
51
52
        return $method;
53
    }
54
}
55