Completed
Pull Request — master (#463)
by Alexander
30:17 queued 05:15
created

FunctionCallArgumentListGenerator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 61
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/*
5
 * Go! AOP framework
6
 *
7
 * @copyright Copyright 2018, Lisachenko Alexander <[email protected]>
8
 *
9
 * This source file is subject to the license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Go\Proxy\Part;
14
15
use Laminas\Code\Generator\AbstractGenerator;
16
use ReflectionFunctionAbstract;
17
18
/**
19
 * Prepares the function call argument list
20
 */
21
final class FunctionCallArgumentListGenerator extends AbstractGenerator
22
{
23
    /**
24
     * List of function arguments
25
     *
26
     * @var string[]
27
     */
28
    private array $arguments = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
29
30
    /**
31
     * If function contains optional arguments
32
     */
33
    private bool $hasOptionals = false;
34
35
    /**
36
     * Definition of variadic argument or null if function is not variadic
37
     */
38
    private ?string $variadicArgument = null;
39
40
    /**
41
     * FunctionCallArgumentList constructor.
42
     *
43
     * @param ReflectionFunctionAbstract $functionLike Instance of function or method to call
44
     */
45
    public function __construct(ReflectionFunctionAbstract $functionLike)
46 17
    {
47
        parent::__construct();
48 17
49
        foreach ($functionLike->getParameters() as $parameter) {
50 17
            $byReference  = ($parameter->isPassedByReference() && !$parameter->isVariadic()) ? '&' : '';
51 12
            $this->hasOptionals = $this->hasOptionals || $parameter->isOptional();
52 12
            $this->arguments[]  = $byReference . '$' . $parameter->name;
53 12
        }
54
        if ($functionLike->isVariadic()) {
55 17
            // Variadic argument is last and should be handled separately
56
            $this->variadicArgument = array_pop($this->arguments);
57 3
        }
58
    }
59 17
60
    /**
61
     * @inheritDoc
62
     */
63
    public function generate(): string
64 17
    {
65
        $argumentsPart = [];
66 17
        if ($this->variadicArgument !== null) {
67 17
            $argumentsPart[] = $this->variadicArgument;
68 3
        }
69
        if (!empty($this->arguments)) {
70 17
            $argumentLine = '[' . implode(', ', $this->arguments) . ']';
71 11
            if ($this->hasOptionals) {
72 11
                $argumentLine = "\\array_slice($argumentLine, 0, \\func_num_args())";
73 5
            }
74
            array_unshift($argumentsPart, $argumentLine);
75 11
        }
76
77
        return implode(', ', $argumentsPart);
78 17
    }
79
}
80