Failed Conditions
Pull Request — master (#6)
by Mickael
01:58
created

Package::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.568
nc 1
cc 1
nop 17

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 {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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
    /** @var Datetime */
66
    private $time;
67
68
    private function __construct(string $name, string $version, array $source, array $dist, array $require,
69
        array $requireDev, array $suggest, string $type, array $extra, array $autoload, string $notificationUrl, array $license, array $authors, string $description, string $homepage,
70
        array $keywords, $time)
71
    {
72
        $this->name = $name;
73
        $this->version = $version;
74
        $this->source = $source;
75
        $this->dist = $dist;
76
        $this->require = $require;
77
        $this->requireDev = $requireDev;
78
        $this->suggest = $suggest;
79
        $this->type = $type;
80
        $this->extra = $extra;
81
        $this->autoload = $autoload;
82
        $this->license = $license;
83
        $this->notificationUrl = $notificationUrl;
84
        $this->authors = $authors;
85
        $this->description = $description;
86
        $this->homepage = $homepage;
87
        $this->keywords = $keywords;
88
        $this->time = $time;
89
    }
90
    public static function factory(array $packageInfo)
91
    {
92
        return new self(
93
            $packageInfo['name'],
94
            $packageInfo['version'],
95
            isset($packageInfo['source']) ? $packageInfo['source'] : [],
96
            isset($packageInfo['dist']) ? $packageInfo['dist'] : [],
97
            isset($packageInfo['require']) ? $packageInfo['require'] : [],
98
            isset($packageInfo['require-dev']) ? $packageInfo['require-dev'] : [],
99
            isset($packageInfo['suggest']) ? $packageInfo['suggest'] : [],
100
            isset($packageInfo['type']) ? $packageInfo['type'] : '',
101
            isset($packageInfo['extra']) ? $packageInfo['extra'] : [],
102
            isset($packageInfo['autoload']) ? $packageInfo['autoload'] : [],
103
            isset($packageInfo['notification-url']) ? $packageInfo['notification-url'] : '',
104
            isset($packageInfo['license']) ? $packageInfo['license'] : [],
105
            isset($packageInfo['authors']) ? $packageInfo['authors'] : [],
106
            isset($packageInfo['description']) ? $packageInfo['description'] : '',
107
            isset($packageInfo['homepage']) ? $packageInfo['homepage'] : '',
108
            isset($packageInfo['keywords']) ? $packageInfo['keywords'] : [],
109
            isset($packageInfo['time']) ? new DateTime($packageInfo['time']) : null
110
        );
111
    }
112
    /**
113
     * @return string
114
     */
115
    public function getName(): string
116
    {
117
        return $this->name;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getVersion(): string
124
    {
125
        return $this->version;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getHomepage(): string
132
    {
133
        return $this->homepage;
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getSource(): array
140
    {
141
        return $this->source;
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function getDist(): array
148
    {
149
        return $this->dist;
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    public function getRequire(): array
156
    {
157
        return $this->require;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    public function getRequireDev(): array
164
    {
165
        return $this->requireDev;
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getType(): string
172
    {
173
        return $this->type;
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function getAutoload(): array
180
    {
181
        return $this->autoload;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getNamespace(): string
188
    {
189
        $namespace = [];
190
        if (isset($this->autoload['psr-0'])) {
191
            $namespace = $this->autoload['psr-0'];
192
        } elseif (isset($this->autoload['psr-4'])) {
193
            $namespace = $this->autoload['psr-4'];
194
        }
195
        return trim(key($namespace), '\\');
196
    }
197
198
    /**
199
     * @return array
200
     */
201
    public function getLicense(): array
202
    {
203
        return $this->license;
204
    }
205
206
    /**
207
     * @return array
208
     */
209
    public function getAuthors(): array
210
    {
211
        return $this->authors;
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public function getDescription(): string
218
    {
219
        return $this->description;
220
    }
221
222
    /**
223
     * @return array
224
     */
225
    public function getKeywords(): array
226
    {
227
        return $this->keywords;
228
    }
229
230
    /**
231
     * @return \DateTime|null
0 ignored issues
show
Documentation introduced by
Should the return type not be Datetime?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
232
     */
233
    public function getTime(): ?DateTime
234
    {
235
        return $this->time;
236
    }
237
}
238