2020use Composer \Package \BasePackage ;
2121use Composer \Package \CompletePackage ;
2222use Composer \Package \Loader \ArrayLoader ;
23- use Composer \Package \RootPackageInterface ;
23+ use Composer \Package \RootPackage ;
2424use Composer \Package \Version \VersionParser ;
2525use Composer \Plugin \PluginInterface ;
2626use Composer \Script \Event ;
2727use Composer \Script \ScriptEvents ;
28+ use UnexpectedValueException ;
2829
2930/**
3031 * Composer plugin that allows merging multiple composer.json files.
@@ -134,7 +135,7 @@ public static function getSubscribedEvents()
134135 */
135136 public function onInstallOrUpdate (Event $ event )
136137 {
137- $ config = $ this ->readConfig ($ this ->composer -> getPackage ());
138+ $ config = $ this ->readConfig ($ this ->getRootPackage ());
138139 if (isset ($ config ['recurse ' ])) {
139140 $ this ->recurse = (bool )$ config ['recurse ' ];
140141 }
@@ -150,10 +151,10 @@ public function onInstallOrUpdate(Event $event)
150151 }
151152
152153 /**
153- * @param RootPackageInterface $package
154+ * @param RootPackage $package
154155 * @return array
155156 */
156- protected function readConfig (RootPackageInterface $ package )
157+ protected function readConfig (RootPackage $ package )
157158 {
158159 $ config = array (
159160 'include ' => array (),
@@ -176,7 +177,7 @@ protected function readConfig(RootPackageInterface $package)
176177 */
177178 protected function mergePackages (array $ config )
178179 {
179- $ root = $ this ->composer -> getPackage ();
180+ $ root = $ this ->getRootPackage ();
180181 foreach (array_reduce (
181182 array_map ('glob ' , $ config ['include ' ]),
182183 'array_merge ' ,
@@ -189,7 +190,7 @@ protected function mergePackages(array $config)
189190 /**
190191 * Read a JSON file and merge its contents
191192 *
192- * @param RootPackageInterface $root
193+ * @param RootPackage $root
193194 * @param string $path
194195 */
195196 protected function loadFile ($ root , $ path )
@@ -202,7 +203,7 @@ protected function loadFile($root, $path)
202203 }
203204 $ this ->debug ("Loading <comment> {$ path }</comment>... " );
204205 $ json = $ this ->readPackageJson ($ path );
205- $ package = $ this ->loader -> load ($ json );
206+ $ package = $ this ->jsonToPackage ($ json );
206207
207208 $ this ->mergeRequires ($ root , $ package );
208209 $ this ->mergeDevRequires ($ root , $ package );
@@ -250,15 +251,15 @@ protected function readPackageJson($path)
250251 }
251252
252253 /**
253- * @param RootPackageInterface $root
254+ * @param RootPackage $root
254255 * @param CompletePackage $package
255256 */
256257 protected function mergeRequires (
257- RootPackageInterface $ root ,
258+ RootPackage $ root ,
258259 CompletePackage $ package
259260 ) {
260261 $ requires = $ package ->getRequires ();
261- if (! $ requires ) {
262+ if (empty ( $ requires) ) {
262263 return ;
263264 }
264265
@@ -272,15 +273,15 @@ protected function mergeRequires(
272273 }
273274
274275 /**
275- * @param RootPackageInterface $root
276+ * @param RootPackage $root
276277 * @param CompletePackage $package
277278 */
278279 protected function mergeDevRequires (
279- RootPackageInterface $ root ,
280+ RootPackage $ root ,
280281 CompletePackage $ package
281282 ) {
282283 $ requires = $ package ->getDevRequires ();
283- if (! $ requires ) {
284+ if (empty ( $ requires) ) {
284285 return ;
285286 }
286287
@@ -294,18 +295,17 @@ protected function mergeDevRequires(
294295 }
295296
296297 /**
297- * @param RootPackageInterface $root
298+ * @param RootPackage $root
298299 * @param CompletePackage $package
299300 * @param string $path
300301 */
301302 protected function mergeAutoload (
302- RootPackageInterface $ root ,
303+ RootPackage $ root ,
303304 CompletePackage $ package ,
304305 $ path
305306 ) {
306307 $ autoload = $ package ->getAutoload ();
307-
308- if (!$ autoload ) {
308+ if (empty ($ autoload )) {
309309 return ;
310310 }
311311
@@ -328,11 +328,11 @@ function(&$path) use ($packagePath) {
328328 * Extract and merge stability flags from the given collection of
329329 * requires.
330330 *
331- * @param RootPackageInterface $root
331+ * @param RootPackage $root
332332 * @param array $requires
333333 */
334334 protected function mergeStabilityFlags (
335- RootPackageInterface $ root ,
335+ RootPackage $ root ,
336336 array $ requires
337337 ) {
338338 $ flags = $ root ->getStabilityFlags ();
@@ -350,11 +350,11 @@ protected function mergeStabilityFlags(
350350 * to the given package and the global repository manager.
351351 *
352352 * @param array $repositories
353- * @param RootPackageInterface $root
353+ * @param RootPackage $root
354354 */
355355 protected function addRepositories (
356356 array $ repositories ,
357- RootPackageInterface $ root
357+ RootPackage $ root
358358 ) {
359359 $ repoManager = $ this ->composer ->getRepositoryManager ();
360360 $ newRepos = array ();
@@ -412,7 +412,7 @@ protected function mergeLinks(array $origin, array $merge, array &$dups)
412412 */
413413 public function onDependencySolve (InstallerEvent $ event )
414414 {
415- if (! $ this ->duplicateLinks ) {
415+ if (empty ( $ this ->duplicateLinks ) ) {
416416 return ;
417417 }
418418
@@ -429,6 +429,39 @@ public function onDependencySolve(InstallerEvent $event)
429429 }
430430 }
431431
432+ /**
433+ * @return RootPackage
434+ */
435+ protected function getRootPackage ()
436+ {
437+ $ root = $ this ->composer ->getPackage ();
438+ // @codeCoverageIgnoreStart
439+ if (!$ root instanceof RootPackage) {
440+ throw new UnexpectedValueException (
441+ 'Expected instance of RootPackage, got ' . get_class ($ root )
442+ );
443+ }
444+ // @codeCoverageIgnoreEnd
445+ return $ root ;
446+ }
447+
448+ /**
449+ * @return CompletePackage
450+ */
451+ protected function jsonToPackage ($ json )
452+ {
453+ $ package = $ this ->loader ->load ($ json );
454+ // @codeCoverageIgnoreStart
455+ if (!$ package instanceof CompletePackage) {
456+ throw new UnexpectedValueException (
457+ 'Expected instance of CompletePackage, got ' .
458+ get_class ($ package )
459+ );
460+ }
461+ // @codeCoverageIgnoreEnd
462+ return $ package ;
463+ }
464+
432465 /**
433466 * Log a debug message
434467 *
@@ -439,6 +472,7 @@ public function onDependencySolve(InstallerEvent $event)
439472 */
440473 protected function debug ($ message )
441474 {
475+ // @codeCoverageIgnoreStart
442476 if ($ this ->inputOutput ->isVerbose ()) {
443477 $ message = " <info>[merge]</info> {$ message }" ;
444478
@@ -449,6 +483,7 @@ protected function debug($message)
449483 $ this ->inputOutput ->write ($ message );
450484 }
451485 }
486+ // @codeCoverageIgnoreEnd
452487 }
453488}
454489// vim:sw=4:ts=4:sts=4:et:
0 commit comments