Completed
Push — master ( ed5962...9634df )
by Nikola
03:22
created

MethodMetadata::byReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace RunOpenCode\AbstractBuilder\Ast;
4
5
use PhpParser\Node\Param;
6
use PhpParser\Node\Stmt\ClassMethod;
7
8
class MethodMetadata
9
{
10
    const PRIVATE = 'private';
11
    const PUBLIC = 'public';
12
    const PROTECTED = 'protected';
13
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
19
    /**
20
     * @var bool
21
     */
22
    private $abstract;
23
24
    /**
25
     * @var bool
26
     */
27
    private $final;
28
29
    /**
30
     * @var string
31
     */
32
    private $visibility;
33
34
    /**
35
     * @var string
36
     */
37
    private $returnType;
38
39
    /**
40
     * @var bool
41
     */
42
    private $byRef;
43
44
    /**
45
     * @var bool
46
     */
47
    private $static;
48
49
    /**
50
     * @var ParameterMetadata[]
51
     */
52
    private $parameters;
53
54
    /**
55
     * MethodMetadata constructor.
56
     *
57
     * @param string $name
58
     * @param bool $abstract
59
     * @param bool $final
60
     * @param string $visibility
61
     * @param string $returnType
62
     * @param bool $byRef
63
     * @param bool $static
64
     * @param ParameterMetadata[] $parameters
65
     */
66
    public function __construct($name, $abstract, $final, $visibility, $returnType, $byRef, $static, array $parameters)
67
    {
68
        $this->name = $name;
69
        $this->abstract = $abstract;
70
        $this->final = $final;
71
        $this->visibility = $visibility;
72
        $this->returnType = $returnType;
73
        $this->byRef = $byRef;
74
        $this->static = $static;
75
        $this->parameters = $parameters;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getName()
82
    {
83
        return $this->name;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    public function isAbstract()
90
    {
91
        return $this->abstract;
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    public function isFinal()
98
    {
99
        return $this->final;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getVisibility()
106
    {
107
        return $this->visibility;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getReturnType()
114
    {
115
        return $this->returnType;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    public function byReference()
122
    {
123
        return $this->byRef;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function isStatic()
130
    {
131
        return $this->static;
132
    }
133
134
    /**
135
     * @return bool
136
     */
137
    public function hasParameters()
138
    {
139
        return count($this->parameters) > 0;
140
    }
141
142
    /**
143
     * @return ParameterMetadata[]
144
     */
145
    public function getParameters()
146
    {
147
        return $this->parameters;
148
    }
149
150
    /**
151
     * Check if method is constructor.
152
     *
153
     * @return bool
154
     */
155
    public function isConstructor()
156
    {
157
        return $this->getName() === '__construct';
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function isPrivate()
164
    {
165
        return $this->visibility === self::PRIVATE;
166
    }
167
168
    /**
169
     * @return bool
170
     */
171
    public function isProtected()
172
    {
173
        return $this->visibility === self::PROTECTED;
174
    }
175
176
    /**
177
     * @return bool
178
     */
179
    public function isPublic()
180
    {
181
        return $this->visibility === self::PUBLIC;
182
    }
183
184
    /**
185
     * Create method metadata instance from \PhpParser\Node\Stmt\ClassMethod
186
     *
187
     * @param ClassMethod $method
188
     * @return MethodMetadata|static
189
     */
190
    public static function fromClassMethod(ClassMethod $method)
191
    {
192
        $parameters = [];
193
194
        /**
195
         * @var Param $param
196
         */
197
        foreach ($method->getParams() as $param) {
198
            $parameters[] = ParameterMetadata::fromParameter($param);
199
        }
200
201
        $visibility = self::PRIVATE;
202
203
        if ($method->isProtected()) {
204
            $visibility = self::PROTECTED;
205
        }
206
207
        if ($method->isPublic()) {
208
            $visibility = self::PUBLIC;
209
        }
210
211
        return new static(
212
            $method->name,
213
            $method->isAbstract(),
214
            $method->isFinal(),
215
            $visibility,
216
            $method->getReturnType(),
0 ignored issues
show
Bug introduced by
It seems like $method->getReturnType() targeting PhpParser\Node\Stmt\ClassMethod::getReturnType() 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?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
217
            $method->returnsByRef(),
218
            $method->isStatic(),
219
            $parameters
220
        );
221
    }
222
}
223