Completed
Push — master ( 556a60...b1e8a1 )
by Nikola
02:34
created

MethodMetadata::getName()   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
 * This file is part of the Abstract builder package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\AbstractBuilder\Ast\Metadata;
11
12
use PhpParser\Node\Param;
13
use PhpParser\Node\Stmt;
14
use RunOpenCode\AbstractBuilder\Utils\MethodUtils;
15
16
/**
17
 * Class MethodMetadata
18
 *
19
 * @package RunOpenCode\AbstractBuilder\Ast\Metadata
20
 */
21
class MethodMetadata
22
{
23
    const PRIVATE = 'private';
24
    const PUBLIC = 'public';
25
    const PROTECTED = 'protected';
26
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * @var bool
34
     */
35
    private $abstract;
36
37
    /**
38
     * @var bool
39
     */
40
    private $final;
41
42
    /**
43
     * @var string
44
     */
45
    private $visibility;
46
47
    /**
48
     * @var string
49
     */
50
    private $returnType;
51
52
    /**
53
     * @var bool
54
     */
55
    private $byRef;
56
57
    /**
58
     * @var bool
59
     */
60
    private $static;
61
62
    /**
63
     * @var ParameterMetadata[]
64
     */
65
    private $parameters;
66
67
    /**
68
     * @var \PhpParser\Node\Stmt\ClassMethod
69
     */
70
    private $ast;
71
72
    /**
73
     * MethodMetadata constructor.
74
     *
75
     * @param string $name
76
     * @param bool $abstract
77
     * @param bool $final
78
     * @param string $visibility
79
     * @param string $returnType
80
     * @param bool $byRef
81
     * @param bool $static
82
     * @param ParameterMetadata[] $parameters
83
     * @param \PhpParser\Node\Stmt\ClassMethod $ast
84
     */
85
    public function __construct($name, $abstract = false, $final = false, $visibility = self::PUBLIC, $returnType = null, $byRef = false, $static = false, array $parameters, Stmt\ClassMethod $ast)
86
    {
87
        $this->name = $name;
88
        $this->abstract = $abstract;
89
        $this->final = $final;
90
        $this->visibility = $visibility;
91
        $this->returnType = $returnType;
92
        $this->byRef = $byRef;
93
        $this->static = $static;
94
        $this->parameters = $parameters;
95
        $this->ast = $ast;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getName()
102
    {
103
        return $this->name;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isAbstract()
110
    {
111
        return $this->abstract;
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function isFinal()
118
    {
119
        return $this->final;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getVisibility()
126
    {
127
        return $this->visibility;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getReturnType()
134
    {
135
        return $this->returnType;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function byReference()
142
    {
143
        return $this->byRef;
144
    }
145
146
    /**
147
     * @return bool
148
     */
149
    public function isStatic()
150
    {
151
        return $this->static;
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    public function hasParameters()
158
    {
159
        return count($this->parameters) > 0;
160
    }
161
162
    /**
163
     * @return ParameterMetadata[]
164
     */
165
    public function getParameters()
166
    {
167
        return $this->parameters;
168
    }
169
170
    /**
171
     * Check if method is constructor.
172
     *
173
     * @return bool
174
     */
175
    public function isConstructor()
176
    {
177
        return $this->getName() === '__construct';
178
    }
179
180
    /**
181
     * @return bool
182
     */
183
    public function isPrivate()
184
    {
185
        return $this->visibility === self::PRIVATE;
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function isProtected()
192
    {
193
        return $this->visibility === self::PROTECTED;
194
    }
195
196
    /**
197
     * @return bool
198
     */
199
    public function isPublic()
200
    {
201
        return $this->visibility === self::PUBLIC;
202
    }
203
204
    /**
205
     * @return \PhpParser\Node\Stmt\ClassMethod
206
     */
207
    public function getAst()
208
    {
209
        return $this->ast;
210
    }
211
212
    /**
213
     * Create method metadata instance from \PhpParser\Node\Stmt\ClassMethod
214
     *
215
     * @param \PhpParser\Node\Stmt\ClassMethod $method
216
     * @return MethodMetadata|static
217
     */
218
    public static function fromClassMethod(Stmt\ClassMethod $method)
219
    {
220
        $parameters = [];
221
222
        /**
223
         * @var Param $param
224
         */
225
        foreach ($method->getParams() as $param) {
226
            $parameters[] = ParameterMetadata::fromParameter($param);
227
        }
228
229
        return new static(
230
            $method->name,
231
            $method->isAbstract(),
232
            $method->isFinal(),
233
            MethodUtils::getVisibility($method->flags),
234
            $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 object<PhpParser\Node\Name> or object<PhpParser\Node\NullableType>; however, RunOpenCode\AbstractBuil...Metadata::__construct() does only seem to accept string|null, 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...
235
            $method->returnsByRef(),
236
            $method->isStatic(),
237
            $parameters,
238
            $method
239
        );
240
    }
241
}
242