Completed
Push — master ( f1462e...556a60 )
by Nikola
09:28 queued 07:57
created

MethodMetadata::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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