Package::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 17
nc 1
nop 17
dl 0
loc 21
rs 9.7
c 2
b 0
f 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
/**
4
 * Author: mickael Louzet @micklouzet
5
 * File: Package.php
6
 * Created: 07/12/2019
7
 */
8
9
declare(strict_types=1);
10
11
namespace ComposerLockParser\Package;
12
13
use DateTime;
14
15
class Package {
16
    
17
    /** @var string */
18
    private $name;
19
    
20
    /** @var string */
21
    private $version;
22
    
23
    /** @var array */
24
    private $source;
25
    
26
    /** @var array */
27
    private $dist;
28
    
29
    /** @var array */
30
    private $require;
31
    
32
    /** @var array */
33
    private $requireDev;
34
    
35
    /** @var array */
36
    private $suggest;
37
    
38
    /** @var string */
39
    private $type;
40
    
41
    /** @var array */
42
    private $extra;
43
    
44
    /** @var array */
45
    private $autoload;
46
    
47
    /** @var string */
48
    private $notificationUrl;
49
    
50
    /** @var array */
51
    private $license;
52
    
53
    /** @var array */
54
    private $authors;
55
    
56
    /** @var string */
57
    private $description;
58
    
59
    /** @var string */
60
    private $homepage;
61
    
62
    /** @var array */
63
    private $keywords;
64
    
65
    private $time;
66
67
    /**
68
     * @param string        $name
69
     * @param string        $version
70
     * @param array         $source
71
     * @param array         $dist
72
     * @param array         $require
73
     * @param array         $requireDev
74
     * @param array         $suggest
75
     * @param string        $type
76
     * @param array         $extra
77
     * @param array         $autoload
78
     * @param string        $notificationUrl
79
     * @param array         $license
80
     * @param array         $authors
81
     * @param string        $description
82
     * @param string        $homepage
83
     * @param array         $keywords
84
     * @param DateTime|null $time
85
     */
86
    private function __construct(string $name, string $version, array $source, array $dist, array $require,
87
        array $requireDev, array $suggest, string $type, array $extra, array $autoload, string $notificationUrl, array $license, array $authors, string $description, string $homepage,
88
        array $keywords, ?DateTime $time)
89
    {
90
        $this->name = $name;
91
        $this->version = $version;
92
        $this->source = $source;
93
        $this->dist = $dist;
94
        $this->require = $require;
95
        $this->requireDev = $requireDev;
96
        $this->suggest = $suggest;
97
        $this->type = $type;
98
        $this->extra = $extra;
99
        $this->autoload = $autoload;
100
        $this->license = $license;
101
        $this->notificationUrl = $notificationUrl;
102
        $this->authors = $authors;
103
        $this->description = $description;
104
        $this->homepage = $homepage;
105
        $this->keywords = $keywords;
106
        $this->time = $time;
107
    }
108
109
    /**
110
     * @param array $packageInfo
111
     *
112
     * @return Package
113
     * @throws \Exception
114
     */
115
    public static function factory(array $packageInfo): self
116
    {
117
        return new self(
118
            $packageInfo['name'],
119
            $packageInfo['version'],
120
            $packageInfo['source'] ?? [],
121
            $packageInfo['dist'] ?? [],
122
            $packageInfo['require'] ?? [],
123
            $packageInfo['require-dev'] ?? [],
124
            $packageInfo['suggest'] ?? [],
125
            $packageInfo['type'] ?? '',
126
            $packageInfo['extra'] ?? [],
127
            $packageInfo['autoload'] ?? [],
128
            $packageInfo['notification-url'] ?? '',
129
            $packageInfo['license'] ?? [],
130
            $packageInfo['authors'] ?? [],
131
            $packageInfo['description'] ?? '',
132
            $packageInfo['homepage'] ?? '',
133
            $packageInfo['keywords'] ?? [],
134
            isset($packageInfo['time']) ? new DateTime($packageInfo['time']) : null
135
        );
136
    }
137
    
138
    /**
139
     * @return string
140
     */
141
    public function getName(): string
142
    {
143
        return $this->name;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getVersion(): string
150
    {
151
        return $this->version;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getHomepage(): string
158
    {
159
        return $this->homepage;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function getSource(): array
166
    {
167
        return $this->source;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function getDist(): array
174
    {
175
        return $this->dist;
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    public function getRequire(): array
182
    {
183
        return $this->require;
184
    }
185
186
    /**
187
     * @return array
188
     */
189
    public function getRequireDev(): array
190
    {
191
        return $this->requireDev;
192
    }
193
194
    /**
195
     * @return array
196
     */
197
    public function getSuggest(): array
198
    {
199
        return $this->suggest;
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getType(): string
206
    {
207
        return $this->type;
208
    }
209
210
    /**
211
     * @return array
212
     */
213
    public function getExtra(): array
214
    {
215
        return $this->extra;
216
    }
217
218
    /**
219
     * @return array
220
     */
221
    public function getAutoload(): array
222
    {
223
        return $this->autoload;
224
    }
225
226
    /**
227
     * @return string
228
     */
229
    public function getNamespace(): string
230
    {
231
        $namespace = [];
232
        if (isset($this->autoload['psr-0'])) {
233
            $namespace = $this->autoload['psr-0'];
234
        } elseif (isset($this->autoload['psr-4'])) {
235
            $namespace = $this->autoload['psr-4'];
236
        }
237
        return trim((string) key($namespace), '\\');
238
    }
239
240
    /**
241
     * @return array
242
     */
243
    public function getLicense(): array
244
    {
245
        return $this->license;
246
    }
247
248
    /**
249
     * @return string
250
     */
251
    public function getNotificationUrl(): string
252
    {
253
        return $this->notificationUrl;
254
    }
255
    /**
256
     * @return array
257
     */
258
    public function getAuthors(): array
259
    {
260
        return $this->authors;
261
    }
262
263
    /**
264
     * @return string
265
     */
266
    public function getDescription(): string
267
    {
268
        return $this->description;
269
    }
270
271
    /**
272
     * @return array
273
     */
274
    public function getKeywords(): array
275
    {
276
        return $this->keywords;
277
    }
278
279
    /**
280
     * @return DateTime|null
281
     */
282
    public function getTime(): ?DateTime
283
    {
284
        return $this->time;
285
    }
286
}
287