Completed
Push — master ( fec521...a1295e )
by Pierre
17s queued 10s
created

Loader::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
ccs 4
cts 4
cp 1
cc 3
nc 3
nop 1
crap 3
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
0 ignored issues
show
Bug introduced by
The type Pickle\Package\PHP\Util\...Base\Interfaces\Package was not found. Did you mean Pickle\Base\Interfaces\Package? If so, make sure to prefix the type with \.
Loading history...
56
     */
57
    public function load($path, $versionOverride = null)
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) $versionOverride === '' ? (string) $xml->version->release : $versionOverride,
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
            $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) {
0 ignored issues
show
introduced by
$ret_pkg is of type Composer\Package\PackageInterface, thus it always evaluated to true.
Loading history...
135 1
            throw new \Exception("Package from '$path' failed to load.");
136
        }
137
        $ret_pkg->setRootDir(dirname($path));
0 ignored issues
show
Bug introduced by
The method setRootDir() does not exist on Composer\Package\PackageInterface. It seems like you code against a sub-type of Composer\Package\PackageInterface such as Pickle\Base\Interfaces\Package or Pickle\Package\PHP\Package or Pickle\Package\PHP\Package. ( Ignorable by Annotation )

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

137
        $ret_pkg->/** @scrutinizer ignore-call */ 
138
                  setRootDir(dirname($path));
Loading history...
138
139
        if ($versionOverride !== '') {
140
            $src_ver = $versionOverride ?? (string) new Header\Version($ret_pkg);
141
            if ($src_ver !== $ret_pkg->getPrettyVersion()) {
142
                throw new \Exception("Version mismatch - '".$src_ver."' != '".$ret_pkg->getPrettyVersion()."' in source vs. XML");
143
            }
144
        }
145
        $ret_pkg->setType('extension');
0 ignored issues
show
Bug introduced by
The method setType() does not exist on Composer\Package\PackageInterface. Did you maybe mean setDistType()? ( Ignorable by Annotation )

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

145
        $ret_pkg->/** @scrutinizer ignore-call */ 
146
                  setType('extension');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
147
        return $ret_pkg;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret_pkg returns the type Composer\Package\PackageInterface which is incompatible with the documented return type Pickle\Package\PHP\Util\...Base\Interfaces\Package.
Loading history...
148
    }
149
150
    protected function validate(\SimpleXMLElement $xml)
151
    {
152 1
        if (-1 === version_compare($xml['version'], '2.0')) {
153 1
            throw new \RuntimeException('Unsupported package.xml version, 2.0 or later only is supported');
154
        }
155
156 1
        if (!isset($xml->providesextension)) {
157 1
            throw new \RuntimeException('Only extension packages are supported');
158
        }
159 1
    }
160
}
161
162
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
163