Issues (67)

src/Package/Util/Loader.php (13 issues)

1
<?php
2
3
/*
4
 * Pickle
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2015-2015, Pickle community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
namespace Pickle\Package\Util;
38
39
use Composer\Package\Loader\LoaderInterface;
40
use Composer\Package\Version\VersionParser;
41
use DateTime;
42
use DateTimeZone;
43
use Exception;
44
use Pickle\Base\Interfaces;
45
use Pickle\Package;
46
use UnexpectedValueException;
47
48
class Loader implements LoaderInterface
49
{
50
    protected $versionParser;
51
52
    protected $loadOptions;
53
54
    public function __construct(?VersionParser $parser = null, $loadOptions = false)
55
    {
56
        $this->versionParser = $parser ?: new VersionParser();
57
        $this->loadOptions = $loadOptions;
58
    }
59
60
    /**
61
     * @param string $package
62
     *
63
     * @return \Pickle\Base\Interfaces\Package $package
64
     */
65
    public function load(array $config, $package = 'Pickle\Base\Interfaces\Package')
66
    {
67
        if (isset($config['version'])) {
68
            $version = $this->versionParser->normalize($config['version']);
69
            $package = Package::factory($config['name'], $version, $config['version'], true);
70
        } else {
71
            $package = Package::factory($config['name'], '', '', true);
72
        }
73
74
        if (isset($config['type']) && $config['type'] != 'extension') {
75
            throw new UnexpectedValueException($package->getName() . ' is not a extension(s) package');
76
        }
77
        $package->setType('extension');
78
79
        $this->setPackageSource($package, $config);
80
        $this->setPackageDist($package, $config);
81
        $this->setPackageReleaseDate($package, $config);
82
        $this->setPackageStability($package, $config);
83
        $this->setPackageExtra($package, $config);
84
        $this->setPackageDescription($package, $config);
85
        $this->setPackageHomepage($package, $config);
86
        $this->setPackageKeywords($package, $config);
87
        $this->setPackageLicense($package, $config);
88
        $this->setPackageAuthors($package, $config);
89
        $this->setPackageSupport($package, $config);
90
91
        return $package;
92
    }
93
94
    protected function setPackageStability(Interfaces\Package $package, array $config)
95
    {
96
        if ($this->isValid($config, 'stability', 'string')) {
97
            $package->setStability($config['stability']);
98
        }
99
    }
100
101
    protected function setPackageExtra(Interfaces\Package $package, array $config)
102
    {
103
        if ($this->isValid($config, 'extra', 'array')) {
104
            $package->setExtra($config['extra']);
0 ignored issues
show
The method setExtra() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
            $package->/** @scrutinizer ignore-call */ 
105
                      setExtra($config['extra']);
Loading history...
105
        }
106
    }
107
108
    protected function setPackageDescription(Interfaces\Package $package, array $config)
109
    {
110
        if ($this->isValid($config, 'description', 'string')) {
111
            $package->setDescription($config['description']);
0 ignored issues
show
The method setDescription() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            $package->/** @scrutinizer ignore-call */ 
112
                      setDescription($config['description']);
Loading history...
112
        }
113
    }
114
115
    protected function setPackageHomepage(Interfaces\Package $package, array $config)
116
    {
117
        if ($this->isValid($config, 'homepage', 'string')) {
118
            $package->setHomepage($config['homepage']);
0 ignored issues
show
The method setHomepage() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
            $package->/** @scrutinizer ignore-call */ 
119
                      setHomepage($config['homepage']);
Loading history...
119
        }
120
    }
121
122
    protected function setPackageKeywords(Interfaces\Package $package, array $config)
123
    {
124
        if ($this->isValid($config, 'keywords', 'array')) {
125
            $package->setKeywords($config['keywords']);
0 ignored issues
show
The method setKeywords() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
            $package->/** @scrutinizer ignore-call */ 
126
                      setKeywords($config['keywords']);
Loading history...
126
        }
127
    }
128
129
    protected function setPackageLicense(Interfaces\Package $package, array $config)
130
    {
131
        if (!empty($config['license'])) {
132
            $package->setLicense(is_array($config['license']) ? $config['license'] : [$config['license']]);
0 ignored issues
show
The method setLicense() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            $package->/** @scrutinizer ignore-call */ 
133
                      setLicense(is_array($config['license']) ? $config['license'] : [$config['license']]);
Loading history...
133
        }
134
    }
135
136
    protected function setPackageAuthors(Interfaces\Package $package, array $config)
137
    {
138
        if ($this->isValid($config, 'authors', 'array')) {
139
            $package->setAuthors($config['authors']);
0 ignored issues
show
The method setAuthors() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
            $package->/** @scrutinizer ignore-call */ 
140
                      setAuthors($config['authors']);
Loading history...
140
        }
141
    }
142
143
    protected function setPackageSupport(Interfaces\Package $package, array $config)
144
    {
145
        if (isset($config['support'])) {
146
            $package->setSupport($config['support']);
0 ignored issues
show
The method setSupport() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
            $package->/** @scrutinizer ignore-call */ 
147
                      setSupport($config['support']);
Loading history...
147
        }
148
    }
149
150
    protected function isValid($config, $key, $type = 'any')
151
    {
152
        switch ($type) {
153
            case 'string':
154
                return isset($config[$key]) && !empty($config[$key]) && is_string($config[$key]);
155
156
            case 'array':
157
                return isset($config[$key]) && !empty($config[$key]) && is_array($config[$key]);
158
        }
159
160
        return false;
161
    }
162
163
    protected function setPackageSource(Interfaces\Package $package, array $config)
164
    {
165
        if (!isset($config['source'])) {
166
            return;
167
        }
168
169
        if (!isset($config['source']['type']) || !isset($config['source']['url']) || !isset($config['source']['reference'])) {
170
            throw new UnexpectedValueException(sprintf(
171
                "Package %s's source key should be specified as {\"type\": ..., \"url\": ..., \"reference\": ...},\n%s given.",
172
                $config['name'],
173
                json_encode($config['source'])
174
            ));
175
        }
176
        $package->setSourceType($config['source']['type']);
0 ignored issues
show
The method setSourceType() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
        $package->/** @scrutinizer ignore-call */ 
177
                  setSourceType($config['source']['type']);
Loading history...
177
        $package->setSourceUrl($config['source']['url']);
0 ignored issues
show
The method setSourceUrl() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
        $package->/** @scrutinizer ignore-call */ 
178
                  setSourceUrl($config['source']['url']);
Loading history...
178
        $package->setSourceReference($config['source']['reference']);
179
        if (isset($config['source']['mirrors'])) {
180
            $package->setSourceMirrors($config['source']['mirrors']);
0 ignored issues
show
The method setSourceMirrors() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

180
            $package->/** @scrutinizer ignore-call */ 
181
                      setSourceMirrors($config['source']['mirrors']);
Loading history...
181
        }
182
    }
183
184
    protected function setPackageDist(Interfaces\Package $package, array $config)
185
    {
186
        if (!isset($config['dist'])) {
187
            return;
188
        }
189
190
        if (!isset($config['dist']['type'])
191
            || !isset($config['dist']['url'])) {
192
            throw new UnexpectedValueException(sprintf(
193
                "Package %s's dist key should be specified as "
194
                . "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given.",
195
                $config['name'],
196
                json_encode($config['dist'])
197
            ));
198
        }
199
200
        $package->setDistType($config['dist']['type']);
201
        $package->setDistUrl($config['dist']['url']);
202
        $package->setDistReference($config['dist']['reference'] ?? null);
203
        $package->setDistSha1Checksum($config['dist']['shasum'] ?? null);
0 ignored issues
show
The method setDistSha1Checksum() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
        $package->/** @scrutinizer ignore-call */ 
204
                  setDistSha1Checksum($config['dist']['shasum'] ?? null);
Loading history...
204
        if (isset($config['dist']['mirrors'])) {
205
            $package->setDistMirrors($config['dist']['mirrors']);
0 ignored issues
show
The method setDistMirrors() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
            $package->/** @scrutinizer ignore-call */ 
206
                      setDistMirrors($config['dist']['mirrors']);
Loading history...
206
        }
207
    }
208
209
    protected function setPackageReleaseDate(Interfaces\Package $package, array $config)
210
    {
211
        if (empty($config['time'])) {
212
            return;
213
        }
214
215
        $time = ctype_digit($config['time']) ? '@' . $config['time'] : $config['time'];
216
217
        try {
218
            $date = new DateTime($time, new DateTimeZone('UTC'));
219
            $package->setReleaseDate($date);
0 ignored issues
show
The method setReleaseDate() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
            $package->/** @scrutinizer ignore-call */ 
220
                      setReleaseDate($date);
Loading history...
220
        } catch (Exception $e) {
221
            // don't crash if time is incorrect
222
        }
223
    }
224
}
225
226
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
227