Completed
Pull Request — master (#277)
by Marco
04:04 queued 17s
created

Constructor::generateOriginalConstructorCall()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3
Metric Value
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 8.8571
cc 3
eloc 17
nc 2
nop 2
crap 3
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
namespace ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator;
20
21
use ProxyManager\Generator\MethodGenerator;
22
use ProxyManager\ProxyGenerator\Util\Properties;
23
use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator;
24
use ReflectionClass;
25
use Zend\Code\Generator\ParameterGenerator;
26
use Zend\Code\Generator\PropertyGenerator;
27
use Zend\Code\Reflection\MethodReflection;
28
29
/**
30
 * The `__construct` implementation for lazy loading proxies
31
 *
32
 * @author Marco Pivetta <[email protected]>
33
 * @license MIT
34
 */
35
class Constructor extends MethodGenerator
36
{
37
    /**
38
     * Constructor
39
     *
40
     * @param ReflectionClass   $originalClass
41
     * @param PropertyGenerator $valueHolder
42
     *
43
     * @return MethodGenerator
44
     */
45 4
    public static function generateMethod(ReflectionClass $originalClass, PropertyGenerator $valueHolder)
46
    {
47 4
        $originalConstructor = self::getConstructor($originalClass);
48
49 4
        $constructor = $originalConstructor
50 1
            ? self::fromReflection($originalConstructor)
51 4
            : new self('__construct');
52
53 4
        $constructor->setDocblock('{@inheritDoc}');
54 4
        $constructor->setBody(
55
            'static $reflection;' . "\n\n"
56 4
            . 'if (! $this->' . $valueHolder->getName() . ') {' . "\n"
57 4
            . '    $reflection = $reflection ?: new \ReflectionClass('
58 4
            . var_export($originalClass->getName(), true)
59 4
            . ");\n"
60 4
            . '    $this->' . $valueHolder->getName() . ' = $reflection->newInstanceWithoutConstructor();' . "\n"
61 4
            . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'this')
62 4
            . "}"
63 4
            . self::generateOriginalConstructorCall($originalClass, $valueHolder)
64
        );
65
66 4
        return $constructor;
67
    }
68
69 4
    private static function generateOriginalConstructorCall(
70
        ReflectionClass $class,
71
        PropertyGenerator $valueHolder
72
    ) : string {
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...
73 4
        $originalConstructor = self::getConstructor($class);
74
75 4
        if (! $originalConstructor) {
76 3
            return '';
77
        }
78
79 1
        $constructor = self::fromReflection($originalConstructor);
80
81
        return "\n\n"
82 1
            . '$this->' . $valueHolder->getName() . '->' . $constructor->getName() . '('
83 1
            . implode(
84 1
                ', ',
85
                array_map(
86
                    function (ParameterGenerator $parameter) {
87 1
                        return ($parameter->getVariadic() ? '...' : '') . '$' . $parameter->getName();
88 1
                    },
89 1
                    $constructor->getParameters()
90
                )
91
            )
92 1
            . ');';
93
    }
94
95
    /**
96
     * @param ReflectionClass $class
97
     *
98
     * @return MethodReflection|null
99
     */
100 4
    private static function getConstructor(ReflectionClass $class)
101
    {
102 4
        $constructors = array_map(
103
            function (\ReflectionMethod $method) {
104 1
                return new MethodReflection(
105 1
                    $method->getDeclaringClass()->getName(),
106 1
                    $method->getName()
107
                );
108 4
            },
109
            array_filter(
110 4
                $class->getMethods(),
111 4
                function (\ReflectionMethod $method) {
112 1
                    return $method->isConstructor();
113 4
                }
114
            )
115
        );
116
117 4
        return reset($constructors) ?: null;
118
    }
119
}
120