Completed
Pull Request — master (#350)
by Marco
08:04
created

MagicMethodGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 55
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
B addOrReplaceParameterNames() 0 27 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\Generator;
22
23
use ReflectionClass;
24
use Zend\Code\Generator\ParameterGenerator;
25
use Zend\Code\Reflection\ParameterReflection;
26
27
/**
28
 * Method generator for magic methods
29
 *
30
 * @author Marco Pivetta <[email protected]>
31
 * @license MIT
32
 */
33
class MagicMethodGenerator extends MethodGenerator
34
{
35
    /**
36
     * @param ReflectionClass      $originalClass
37
     * @param string               $name
38
     * @param ParameterGenerator[] $parameters
39
     *
40
     * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
41
     */
42 3
    public function __construct(ReflectionClass $originalClass, string $name, array $parameters = [])
43
    {
44 3
        parent::__construct($name, [], static::FLAG_PUBLIC);
45
46 3
        $this->setReturnsReference(strtolower($name) === '__get');
47
48 3
        if ($originalClass->hasMethod($name)) {
49 1
            $originalMethod = $originalClass->getMethod($name);
50
51 1
            $this->setDocBlock('{@inheritDoc}');
52 1
            $this->setReturnsReference($originalMethod->returnsReference());
53 1
            $this->setReturnType(((string) $originalMethod->getReturnType()) ?: null);
54 1
            $this->addOrReplaceParameterNames($originalMethod, ...$parameters);
55
        } else {
56 2
            $this->setParameters($parameters);
57
        }
58 3
    }
59
60 1
    private function addOrReplaceParameterNames(
61
        \ReflectionMethod $originalMethod,
62
        ParameterGenerator ...$newParameters
63
    ) : void {
64 1
        $originalParameters  = array_values(array_map(
65 1
            function (\ReflectionParameter $parameter) use ($originalMethod) : ParameterGenerator {
66 1
                return ParameterGenerator::fromReflection(new ParameterReflection(
67 1
                    [$originalMethod->getDeclaringClass()->getName(), $originalMethod->getName()],
68 1
                    $parameter->getPosition()
69
                ));
70 1
            },
71 1
            $originalMethod->getParameters()
72
        ));
73
74 1
        $parametersCount = max(count($originalParameters), count($newParameters));
75 1
        $parameters      = [];
76
77 1
        for ($idx = 0; $idx < $parametersCount; $idx += 1) {
78 1
            $parameter = $originalParameters[$idx] ?? $newParameters[$idx];
79
80 1
            $parameter->setName(($newParameters[$idx] ?? $originalParameters[$idx])->getName());
81
82 1
            $parameters[] = $parameter;
83
        }
84
85 1
        $this->setParameters($parameters);
86 1
    }
87
}
88