Failed Conditions
Push — master ( 5aed68...741277 )
by Mickael
13s queued 11s
created

Package::factory()   C

Complexity

Conditions 16
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 5.5666
nc 1
cc 16
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
69
     * @param string $name
70
     * @param string $version
71
     * @param array $source
72
     * @param array $dist
73
     * @param array $require
74
     * @param array $requireDev
75
     * @param array $suggest
76
     * @param string $type
77
     * @param array $extra
78
     * @param array $autoload
79
     * @param string $notificationUrl
80
     * @param array $license
81
     * @param array $authors
82
     * @param string $description
83
     * @param string $homepage
84
     * @param array $keywords
85
     * @param DateTime|null $time
86
     */
87
    private function __construct(string $name, string $version, array $source, array $dist, array $require,
88
        array $requireDev, array $suggest, string $type, array $extra, array $autoload, string $notificationUrl, array $license, array $authors, string $description, string $homepage,
89
        array $keywords, $time)
90
    {
91
        $this->name = $name;
92
        $this->version = $version;
93
        $this->source = $source;
94
        $this->dist = $dist;
95
        $this->require = $require;
96
        $this->requireDev = $requireDev;
97
        $this->suggest = $suggest;
98
        $this->type = $type;
99
        $this->extra = $extra;
100
        $this->autoload = $autoload;
101
        $this->license = $license;
102
        $this->notificationUrl = $notificationUrl;
103
        $this->authors = $authors;
104
        $this->description = $description;
105
        $this->homepage = $homepage;
106
        $this->keywords = $keywords;
107
        $this->time = $time;
0 ignored issues
show
Documentation Bug introduced by
It seems like $time can also be of type object<DateTime>. However, the property $time is declared as type object<ComposerLockParser\Package\Datetime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
108
    }
109
110
    /**
111
     * @param array $packageInfo
112
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be Package?

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...
113
     */
114
    public static function factory(array $packageInfo)
115
    {
116
        return new self(
117
            $packageInfo['name'],
118
            $packageInfo['version'],
119
            isset($packageInfo['source']) ? $packageInfo['source'] : [],
120
            isset($packageInfo['dist']) ? $packageInfo['dist'] : [],
121
            isset($packageInfo['require']) ? $packageInfo['require'] : [],
122
            isset($packageInfo['require-dev']) ? $packageInfo['require-dev'] : [],
123
            isset($packageInfo['suggest']) ? $packageInfo['suggest'] : [],
124
            isset($packageInfo['type']) ? $packageInfo['type'] : '',
125
            isset($packageInfo['extra']) ? $packageInfo['extra'] : [],
126
            isset($packageInfo['autoload']) ? $packageInfo['autoload'] : [],
127
            isset($packageInfo['notification-url']) ? $packageInfo['notification-url'] : '',
128
            isset($packageInfo['license']) ? $packageInfo['license'] : [],
129
            isset($packageInfo['authors']) ? $packageInfo['authors'] : [],
130
            isset($packageInfo['description']) ? $packageInfo['description'] : '',
131
            isset($packageInfo['homepage']) ? $packageInfo['homepage'] : '',
132
            isset($packageInfo['keywords']) ? $packageInfo['keywords'] : [],
133
            isset($packageInfo['time']) ? new DateTime($packageInfo['time']) : null
134
        );
135
    }
136
    
137
    /**
138
     * @return string
139
     */
140
    public function getName(): string
141
    {
142
        return $this->name;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getVersion(): string
149
    {
150
        return $this->version;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getHomepage(): string
157
    {
158
        return $this->homepage;
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function getSource(): array
165
    {
166
        return $this->source;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getDist(): array
173
    {
174
        return $this->dist;
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function getRequire(): array
181
    {
182
        return $this->require;
183
    }
184
185
    /**
186
     * @return array
187
     */
188
    public function getRequireDev(): array
189
    {
190
        return $this->requireDev;
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    public function getType(): string
197
    {
198
        return $this->type;
199
    }
200
201
    /**
202
     * @return array
203
     */
204
    public function getAutoload(): array
205
    {
206
        return $this->autoload;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getNamespace(): string
213
    {
214
        $namespace = [];
215
        if (isset($this->autoload['psr-0'])) {
216
            $namespace = $this->autoload['psr-0'];
217
        } elseif (isset($this->autoload['psr-4'])) {
218
            $namespace = $this->autoload['psr-4'];
219
        }
220
        return trim(key($namespace), '\\');
221
    }
222
223
    /**
224
     * @return array
225
     */
226
    public function getLicense(): array
227
    {
228
        return $this->license;
229
    }
230
231
    /**
232
     * @return array
233
     */
234
    public function getAuthors(): array
235
    {
236
        return $this->authors;
237
    }
238
239
    /**
240
     * @return string
241
     */
242
    public function getDescription(): string
243
    {
244
        return $this->description;
245
    }
246
247
    /**
248
     * @return array
249
     */
250
    public function getKeywords(): array
251
    {
252
        return $this->keywords;
253
    }
254
255
    /**
256
     * @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...
257
     */
258
    public function getTime(): ?DateTime
259
    {
260
        return $this->time;
261
    }
262
}
263