1 | <?php |
||
43 | class Loader |
||
44 | { |
||
45 | protected $loader; |
||
46 | |||
47 | public function __construct(LoaderInterface $loader) |
||
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); |
||
|
|||
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) |
||
158 | } |
||
159 | |||
161 |
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.