Method::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 8
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
ccs 16
cts 16
cp 1
crap 4

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
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php;
16
17
use phpDocumentor\Reflection\DocBlock;
18
use phpDocumentor\Reflection\Element;
19
use phpDocumentor\Reflection\Fqsen;
20
use phpDocumentor\Reflection\Location;
21
use phpDocumentor\Reflection\Type;
22
use phpDocumentor\Reflection\Types\Mixed_;
23
24
/**
25
 * Descriptor representing a Method in a Class, Interface or Trait.
26
 */
27
final class Method implements Element
28
{
29
    /**
30
     * @var DocBlock|null documentation of this method.
31
     */
32
    private $docBlock = null;
33
34
    /**
35
     * @var Fqsen Full Qualified Structural Element Name
36
     */
37
    private $fqsen;
38
39
    /**
40
     * @var bool
41
     */
42
    private $abstract = false;
43
44
    /**
45
     * @var bool
46
     */
47
    private $final = false;
48
49
    /**
50
     * @var bool
51
     */
52
    private $static = false;
53
54
    /**
55
     * @var null|Visibility visibility of this method
56
     */
57
    private $visibility = null;
58
59
    /**
60
     * @var Argument[]
61
     */
62
    private $arguments = [];
63
64
    /**
65
     * @var Location
66
     */
67
    private $location;
68
69
    /**
70
     * @var Type
71
     */
72
    private $returnType;
73
74
    /**
75
     * Initializes the all properties.
76
     *
77
     * @param Visibility|null $visibility when null is provided a default 'public' is set.
78
     */
79 9
    public function __construct(
80
        Fqsen $fqsen,
81
        ?Visibility $visibility = null,
82
        ?DocBlock $docBlock = null,
83
        bool $abstract = false,
84
        bool $static = false,
85
        bool $final = false,
86
        ?Location $location = null,
87
        ?Type $returnType = null
88
    ) {
89 9
        $this->fqsen = $fqsen;
90 9
        $this->visibility = $visibility;
91 9
        $this->docBlock = $docBlock;
92
93 9
        if ($this->visibility === null) {
94 3
            $this->visibility = new Visibility('public');
95
        }
96
97 9
        if ($location === null) {
98 9
            $location = new Location(-1);
99
        }
100
101 9
        if ($returnType === null) {
102 8
            $returnType = new Mixed_();
103
        }
104
105 9
        $this->abstract = $abstract;
106 9
        $this->static = $static;
107 9
        $this->final = $final;
108 9
        $this->location = $location;
109 9
        $this->returnType = $returnType;
110 9
    }
111
112
    /**
113
     * Returns true when this method is abstract. Otherwise returns false.
114
     */
115 1
    public function isAbstract(): bool
116
    {
117 1
        return $this->abstract;
118
    }
119
120
    /**
121
     * Returns true when this method is final. Otherwise returns false.
122
     */
123 1
    public function isFinal(): bool
124
    {
125 1
        return $this->final;
126
    }
127
128
    /**
129
     * Returns true when this method is static. Otherwise returns false.
130
     */
131 1
    public function isStatic(): bool
132
    {
133 1
        return $this->static;
134
    }
135
136
    /**
137
     * Returns the Visibility of this method.
138
     */
139 2
    public function getVisibility(): ?Visibility
140
    {
141 2
        return $this->visibility;
142
    }
143
144
    /**
145
     * Returns the arguments of this method.
146
     *
147
     * @return Argument[]
148
     */
149 1
    public function getArguments(): array
150
    {
151 1
        return $this->arguments;
152
    }
153
154
    /**
155
     * Add new argument to this method.
156
     */
157 1
    public function addArgument(Argument $argument): void
158
    {
159 1
        $this->arguments[] = $argument;
160 1
    }
161
162
    /**
163
     * Returns the Fqsen of the element.
164
     */
165 1
    public function getFqsen(): Fqsen
166
    {
167 1
        return $this->fqsen;
168
    }
169
170
    /**
171
     * Returns the name of the element.
172
     */
173 1
    public function getName(): string
174
    {
175 1
        return $this->fqsen->getName();
176
    }
177
178
    /**
179
     * Returns the DocBlock of this method if available.
180
     *
181
     * @returns null|DocBlock
182
     */
183 1
    public function getDocBlock(): ?DocBlock
184
    {
185 1
        return $this->docBlock;
186
    }
187
188
    public function getLocation(): Location
189
    {
190
        return $this->location;
191
    }
192
193
    /**
194
     * Returns the in code defined return type.
195
     *
196
     * Return types are introduced in php 7.0 when your could doesn't have a
197
     * return type defined this method will return Mixed_ by default. The return value of this
198
     * method is not affected by the return tag in your docblock.
199
     */
200 2
    public function getReturnType(): Type
201
    {
202 2
        return $this->returnType;
203
    }
204
}
205