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

FunctionParameterList::__construct()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 8.1155
c 0
b 0
f 0
cc 8
nc 10
nop 2
crap 8
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\ParameterGenerator;
16
use Laminas\Code\Generator\ValueGenerator;
17
use ReflectionFunctionAbstract;
18
use ReflectionNamedType;
19
20
/**
21
 * Generates parameters from reflection definition
22
 */
23
final class FunctionParameterList
24
{
25
    /**
26
     * @var ParameterGenerator[]
27
     */
28
    private array $generatedParameters = [];
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
     * ParameterListGenerator constructor.
32
     *
33
     * @param ReflectionFunctionAbstract $functionLike    Instance of function or method
34 26
     * @param bool                       $useTypeWidening Should generated parameters use type widening
35
     */
36 26
    public function __construct(ReflectionFunctionAbstract $functionLike, bool $useTypeWidening = false)
37 26
    {
38 18
        $reflectionParameters = $functionLike->getParameters();
39
        foreach ($reflectionParameters as $reflectionParameter) {
40 18
            $defaultValue = null;
41 18
42 4
            $isDefaultValueAvailable = $reflectionParameter->isDefaultValueAvailable();
43 17
            if ($isDefaultValueAvailable) {
44 3
                $defaultValue = new ValueGenerator($reflectionParameter->getDefaultValue());
45
            } elseif ($reflectionParameter->isOptional() && !$reflectionParameter->isVariadic()) {
46
                $defaultValue = new ValueGenerator(null);
47 18
            }
48 18
49 18
            $parameterTypeName = null;
50 18
            if (!$useTypeWidening && $reflectionParameter->hasType()) {
51 18
                $parameterReflectionType = $reflectionParameter->getType();
52 18
                if ($parameterReflectionType instanceof ReflectionNamedType) {
53
                    $parameterTypeName = $parameterReflectionType->getName();
54 18
                } else {
55
                    $parameterTypeName = (string) $parameterReflectionType;
56 18
                }
57
            }
58 26
59
            $generatedParameter = new ParameterGenerator(
60
                $reflectionParameter->getName(),
61
                $parameterTypeName,
62
                $defaultValue,
63
                $reflectionParameter->getPosition(),
64
                $reflectionParameter->isPassedByReference()
65 26
            );
66
            $generatedParameter->setVariadic($reflectionParameter->isVariadic());
67 26
68
            $this->generatedParameters[] = $generatedParameter;
69
        }
70
    }
71
72
    /**
73
     * Returns the list of generated parameters
74
     *
75
     * @return ParameterGenerator[]
76
     */
77
    public function getGeneratedParameters(): array
78
    {
79
        return $this->generatedParameters;
80
    }
81
}
82