Completed
Push — master ( ed922a...ce73d9 )
by Nikola
03:26
created

ParameterMetadata::fromParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
3
namespace RunOpenCode\AbstractBuilder\Ast;
4
5
use PhpParser\Node\Param;
6
7
class ParameterMetadata
8
{
9
    /**
10
     * @var string
11
     */
12
    private $name;
13
14
    /**
15
     * @var string
16
     */
17
    private $type;
18
19
    /**
20
     * @var bool
21
     */
22
    private $byRef;
23
24
    /**
25
     * @var bool
26
     */
27
    private $variadic;
28
29
    /**
30
     * @var mixed
31
     */
32
    private $default;
33
34
    /**
35
     * ParameterMetadata constructor.
36
     *
37
     * @param string $name
38
     * @param string $type
39
     * @param bool $byRef
40
     * @param bool $variadic
41
     * @param mixed $default
42
     */
43
    public function __construct($name, $type, $byRef, $variadic, $default)
44
    {
45
        $this->name = $name;
46
        $this->type = $type;
47
        $this->byRef = $byRef;
48
        $this->variadic = $variadic;
49
        $this->default = $default;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getName()
56
    {
57
        return $this->name;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getType()
64
    {
65
        return $this->type;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function byReference()
72
    {
73
        return $this->byRef;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function isVariadic()
80
    {
81
        return $this->variadic;
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    public function getDefault()
88
    {
89
        return $this->default;
90
    }
91
92
    /**
93
     * Creates parameter metadata instance from \PhpParser\Node\Param
94
     *
95
     * @param Param $param
96
     * @return ParameterMetadata|static
97
     */
98
    public static function fromParameter(Param $param)
99
    {
100
        return new static(
101
            $param->name,
102
            $param->type,
0 ignored issues
show
Bug introduced by
It seems like $param->type can also be of type null or object<PhpParser\Node\Name> or object<PhpParser\Node\NullableType>; however, RunOpenCode\AbstractBuil...Metadata::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
103
            $param->byRef,
104
            $param->variadic,
105
            $param->default
106
        );
107
    }
108
}
109