Completed
Push — master ( 0209da...c2d684 )
by Julien
04:18 queued 12s
created

Loader   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.96%

Importance

Changes 8
Bugs 4 Features 0
Metric Value
wmc 19
c 8
b 4
f 0
lcom 1
cbo 3
dl 0
loc 116
rs 10
ccs 49
cts 57
cp 0.8596

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 10 3
F load() 0 90 15
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\PHP\Util\XML;
38
39
use Composer\Package\Loader\LoaderInterface;
40
use Pickle\Package\PHP;
41
use Pickle\Package\Util\Header;
42
43
class Loader
44
{
45
    protected $loader;
46
47
    public function __construct(LoaderInterface $loader)
48
    {
49 1
        $this->loader = $loader;
50 1
    }
51
52
    /**
53
     * @param string $path
54
     *
55
     * @return Pickle\Base\Interfaces\Package
56
     */
57
    public function load($path)
58
    {
59 1
        if (false === is_file($path)) {
60 1
            throw new \InvalidArgumentException('File not found: '.$path);
61
        }
62
63 1
        $xml = @simplexml_load_file($path);
64
65 1
        if (false === $xml) {
66 1
            $error = error_get_last();
67 1
            $exception = null;
68
69 1
            if (null !== $error) {
70
                $exception = new \Exception($error['message'], $error['type']);
71
            }
72
73 1
            throw new \RuntimeException('Failed to read '.$path, 0, $exception);
74
        }
75
76 1
        $this->validate($xml);
77
78
        $package = [
79 1
            'name' => (string) $xml->name,
80 1
            'version' => (string) $xml->version->release,
81 1
            'stability' => (string) $xml->stability->release,
82 1
            'description' => (string) $xml->summary,
83
        ];
84
85 1
        if (!isset($xml->providesextension)) {
86
            throw new \Exception('not a PHP extension package.xml, providesextension tag missing');
87
        }
88
89 1
        $authors = array();
90 1
        foreach (array($xml->lead, $xml->developer, $xml->contributor, $xml->helper) as $devs) {
91 1
            foreach($devs as $dev) {
92 1
                $authors[] = $dev;
93
            }
94
        }
95
96 1
        if (false === empty($authors)) {
97 1
            $package['authors'] = [];
98
99 1
            foreach ($authors as $author) {
100 1
                $package['authors'][] = [
101 1
                    'name' => (string) $author->name,
102 1
                    'email' => (string) $author->email,
103
                ];
104
            }
105
        }
106
107 1
        $opts = $configureOptions = [];
108
109 1
        if (isset($xml->extsrcrelease->configureoption)) {
110 1
            $opts = $xml->extsrcrelease->configureoption;
111
        }
112
113 1
        foreach ($opts as $opt) {
114 1
            $name = trim($opt['name']);
115 1
            $default = trim($opt['default']);
116 1
            $prompt = trim($opt['prompt']);
117
118 1
            $configureOptions[$name] = [
119 1
                'default' => $default,
120 1
                'prompt' => $prompt,
121
            ];
122
        }
123
124 1
        if (false === empty($configureOptions)) {
125 1
            $package['extra'] = ['configure-options' => $configureOptions];
126
        }
127
128 1
        if (isset($xml->license)) {
129 1
            $package['license'] = (string) $xml->license;
130
        }
131 1
        $package['type'] = 'extension';
132
133 1
        $ret_pkg = $this->loader->load($package);
134 1
	if (!$ret_pkg) {
135 1
		throw new \Exception("Package from '$path' failed to load.");
136
	}
137
        $ret_pkg->setRootDir(dirname($path));
138
139
        $src_ver = new Header\Version($ret_pkg);
0 ignored issues
show
Compatibility introduced by
$ret_pkg of type object<Composer\Package\PackageInterface> is not a sub-type of object<Pickle\Base\Interfaces\Package>. It seems like you assume a child interface of the interface Composer\Package\PackageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
140
        if ($src_ver != $ret_pkg->getPrettyVersion()) {
141
            throw new \Exception("Version mismatch - '".$src_ver."' != '".$ret_pkg->getPrettyVersion()."' in source vs. XML");
142
        }
143
        $ret_pkg->setType('extension');
144
145
        return $ret_pkg;
146
    }
147
148
    protected function validate(\SimpleXMLElement $xml)
149
    {
150 1
        if (-1 === version_compare($xml['version'], '2.0')) {
151 1
            throw new \RuntimeException('Unsupported package.xml version, 2.0 or later only is supported');
152
        }
153
154 1
        if (!isset($xml->providesextension)) {
155 1
            throw new \RuntimeException('Only extension packages are supported');
156
        }
157 1
    }
158
}
159
160
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
161