Failed Conditions
Pull Request — master (#7)
by Mickael
01:14
created

Package::factory()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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

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...
259
     */
260
    public function getTime(): ?DateTime
261
    {
262
        return $this->time;
263
    }
264
}
265