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

MethodMetadata::isPublic()   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 ParameterMetadata[]
136
     */
137
    public function getParameters()
138
    {
139
        return $this->parameters;
140
    }
141
142
    /**
143
     * Check if method is constructor.
144
     *
145
     * @return bool
146
     */
147
    public function isConstructor()
148
    {
149
        return $this->getName() === '__construct';
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function isPrivate()
156
    {
157
        return $this->visibility === self::PRIVATE;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function isProtected()
164
    {
165
        return $this->visibility === self::PROTECTED;
166
    }
167
168
    /**
169
     * @return bool
170
     */
171
    public function isPublic()
172
    {
173
        return $this->visibility === self::PUBLIC;
174
    }
175
176
    /**
177
     * Create method metadata instance from \PhpParser\Node\Stmt\ClassMethod
178
     *
179
     * @param ClassMethod $method
180
     * @return MethodMetadata|static
181
     */
182
    public static function fromClassMethod(ClassMethod $method)
183
    {
184
        $parameters = [];
185
186
        /**
187
         * @var Param $param
188
         */
189
        foreach ($method->getParams() as $param) {
190
            $parameters[] = ParameterMetadata::fromParameter($param);
191
        }
192
193
        $visibility = self::PRIVATE;
194
195
        if ($method->isProtected()) {
196
            $visibility = self::PROTECTED;
197
        }
198
199
        if ($method->isPublic()) {
200
            $visibility = self::PUBLIC;
201
        }
202
203
        return new static(
204
            $method->name,
205
            $method->isAbstract(),
206
            $method->isFinal(),
207
            $visibility,
208
            $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...
209
            $method->returnsByRef(),
210
            $method->isStatic(),
211
            $parameters
212
        );
213
    }
214
}
215