Completed
Push — master ( 9645a9...5dfc66 )
by Bryan
03:08
created

ExtraPackage::mergeDevInto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of the Composer Merge plugin.
4
 *
5
 * Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors
6
 *
7
 * This software may be modified and distributed under the terms of the MIT
8
 * license. See the LICENSE file for details.
9
 */
10
11
namespace Wikimedia\Composer\Merge;
12
13
use Wikimedia\Composer\Logger;
14
15
use Composer\Composer;
16
use Composer\Json\JsonFile;
17
use Composer\Package\BasePackage;
18
use Composer\Package\CompletePackage;
19
use Composer\Package\Link;
20
use Composer\Package\Loader\ArrayLoader;
21
use Composer\Package\RootAliasPackage;
22
use Composer\Package\RootPackage;
23
use Composer\Package\RootPackageInterface;
24
use Composer\Package\Version\VersionParser;
25
use UnexpectedValueException;
26
27
/**
28
 * Processing for a composer.json file that will be merged into
29
 * a RootPackageInterface
30
 *
31
 * @author Bryan Davis <[email protected]>
32
 */
33
class ExtraPackage
34
{
35
36
    /**
37
     * @var Composer $composer
38
     */
39
    protected $composer;
40
41
    /**
42
     * @var Logger $logger
43
     */
44
    protected $logger;
45
46
    /**
47
     * @var string $path
48
     */
49
    protected $path;
50
51
    /**
52
     * @var array $json
53
     */
54
    protected $json;
55
56
    /**
57
     * @var CompletePackage $package
58
     */
59
    protected $package;
60
61
    /**
62
     * @var VersionParser $versionParser
63
     */
64
    protected $versionParser;
65
66
    /**
67
     * @param string $path Path to composer.json file
68
     * @param Composer $composer
69
     * @param Logger $logger
70
     */
71 100
    public function __construct($path, Composer $composer, Logger $logger)
72
    {
73 100
        $this->path = $path;
74 100
        $this->composer = $composer;
75 100
        $this->logger = $logger;
76 100
        $this->json = $this->readPackageJson($path);
77 100
        $this->package = $this->loadPackage($this->json);
0 ignored issues
show
Documentation introduced by Bryan Davis
$this->json is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78 100
        $this->versionParser = new VersionParser();
79 100
    }
80
81
    /**
82
     * Get list of additional packages to include if precessing recursively.
83
     *
84
     * @return array
85
     */
86 95
    public function getIncludes()
87
    {
88 95
        return isset($this->json['extra']['merge-plugin']['include']) ?
89 95
            $this->json['extra']['merge-plugin']['include'] : array();
90
    }
91
92
    /**
93
     * Get list of additional packages to require if precessing recursively.
94
     *
95
     * @return array
96
     */
97 95
    public function getRequires()
98
    {
99 95
        return isset($this->json['extra']['merge-plugin']['require']) ?
100 95
            $this->json['extra']['merge-plugin']['require'] : array();
101
    }
102
103
    /**
104
     * Read the contents of a composer.json style file into an array.
105
     *
106
     * The package contents are fixed up to be usable to create a Package
107
     * object by providing dummy "name" and "version" values if they have not
108
     * been provided in the file. This is consistent with the default root
109
     * package loading behavior of Composer.
110
     *
111
     * @param string $path
112
     * @return array
113
     */
114 100
    protected function readPackageJson($path)
115
    {
116 100
        $file = new JsonFile($path);
117 100
        $json = $file->read();
118 100
        if (!isset($json['name'])) {
119 95
            $json['name'] = 'merge-plugin/' .
120 95
                strtr($path, DIRECTORY_SEPARATOR, '-');
121 95
        }
122 100
        if (!isset($json['version'])) {
123 100
            $json['version'] = '1.0.0';
124 100
        }
125 100
        return $json;
126
    }
127
128
    /**
129
     * @param string $json
130
     * @return CompletePackage
131
     */
132 100
    protected function loadPackage($json)
133
    {
134 100
        $loader = new ArrayLoader();
135 100
        $package = $loader->load($json);
0 ignored issues
show
Documentation introduced by Bryan Davis
$json is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
        // @codeCoverageIgnoreStart
137
        if (!$package instanceof CompletePackage) {
138
            throw new UnexpectedValueException(
139
                'Expected instance of CompletePackage, got ' .
140
                get_class($package)
141
            );
142
        }
143
        // @codeCoverageIgnoreEnd
144 100
        return $package;
145
    }
146
147
    /**
148
     * Merge this package into a RootPackageInterface
149
     *
150
     * @param RootPackageInterface $root
151
     * @param PluginState $state
152
     */
153 100
    public function mergeInto(RootPackageInterface $root, PluginState $state)
154
    {
155 100
        $this->addRepositories($root, $state->shouldPrependRepositories());
156
157 100
        $this->mergeRequires('require', $root, $state);
158
159 100
        $this->mergePackageLinks('conflict', $root);
160 100
        $this->mergePackageLinks('replace', $root);
161 100
        $this->mergePackageLinks('provide', $root);
162
163 100
        $this->mergeSuggests($root);
164
165 100
        $this->mergeAutoload('autoload', $root);
166
167 100
        $this->mergeExtra($root, $state);
168
169 100
        if ($state->isDevMode()) {
170 95
            $this->mergeDevInto($root, $state);
171 95
        } else {
172 5
            $this->mergeReferences($root);
173
        }
174 100
    }
175
176
    /**
177
     * Merge just the dev portion into a RootPackageInterface
178
     *
179
     * @param RootPackageInterface $root
180
     * @param PluginState $state
181
     */
182 95
    public function mergeDevInto(RootPackageInterface $root, PluginState $state)
183
    {
184 95
        $this->mergeRequires('require-dev', $root, $state);
185 95
        $this->mergeAutoload('devAutoload', $root);
186 95
        $this->mergeReferences($root);
187 95
    }
188
189
    /**
190
     * Add a collection of repositories described by the given configuration
191
     * to the given package and the global repository manager.
192
     *
193
     * @param RootPackageInterface $root
194
     * @param bool $prepend Should repositories be prepended to repository manager.
195
     */
196 100
    protected function addRepositories(RootPackageInterface $root, $prepend)
197
    {
198 100
        if (!isset($this->json['repositories'])) {
199 85
            return;
200
        }
201 15
        $repoManager = $this->composer->getRepositoryManager();
202 15
        $newRepos = array();
203
204 15
        foreach ($this->json['repositories'] as $repoJson) {
205 15
            if (!isset($repoJson['type'])) {
206 10
                continue;
207
            }
208 15
            if ($prepend) {
209 5
                $this->logger->info("Prepending {$repoJson['type']} repository");
210 5
            } else {
211 10
                $this->logger->info("Adding {$repoJson['type']} repository");
212
            }
213 15
            $repo = $repoManager->createRepository(
214 15
                $repoJson['type'],
215
                $repoJson
216 15
            );
217 15
            if ($prepend) {
218 5
                $repoManager->prependRepository($repo);
219 5
            } else {
220 10
                $repoManager->addRepository($repo);
221
            }
222 15
            $newRepos[] = $repo;
223 15
        }
224
225 15
        $unwrapped = self::unwrapIfNeeded($root, 'setRepositories');
226 15
        if ($prepend) {
227 5
            $unwrapped->setRepositories(array_merge(
228 5
                $newRepos,
229 5
                $root->getRepositories()
230 5
            ));
231 5
        } else {
232 10
            $unwrapped->setRepositories(array_merge(
233 10
                $root->getRepositories(),
234
                $newRepos
235 10
            ));
236
        }
237 15
    }
238
239
    /**
240
     * Merge require or require-dev into a RootPackageInterface
241
     *
242
     * @param string $type 'require' or 'require-dev'
243
     * @param RootPackageInterface $root
244
     * @param PluginState $state
245
     */
246 100
    protected function mergeRequires(
247
        $type,
248
        RootPackageInterface $root,
249
        PluginState $state
250
    ) {
251 100
        $linkType = BasePackage::$supportedLinkTypes[$type];
252 100
        $getter = 'get' . ucfirst($linkType['method']);
253 100
        $setter = 'set' . ucfirst($linkType['method']);
254
255 100
        $requires = $this->package->{$getter}();
256 100
        if (empty($requires)) {
257 80
            return;
258
        }
259
260 65
        $this->mergeStabilityFlags($root, $requires);
261
262 65
        $requires = $this->replaceSelfVersionDependencies(
263 65
            $type,
264 65
            $requires,
265
            $root
266 65
        );
267
268 65
        $root->{$setter}($this->mergeOrDefer(
269 65
            $type,
270 65
            $root->{$getter}(),
271 65
            $requires,
272
            $state
273 65
        ));
274 65
    }
275
276
    /**
277
     * Merge two collections of package links and collect duplicates for
278
     * subsequent processing.
279
     *
280
     * @param string $type 'require' or 'require-dev'
281
     * @param array $origin Primary collection
282
     * @param array $merge Additional collection
283
     * @param PluginState $state
284
     * @return array Merged collection
285
     */
286 65
    protected function mergeOrDefer(
287
        $type,
288
        array $origin,
289
        array $merge,
290
        $state
291
    ) {
292 65
        $dups = array();
293 65
        foreach ($merge as $name => $link) {
294 65
            if (!isset($origin[$name]) || $state->replaceDuplicateLinks()) {
295 65
                $this->logger->info("Merging <comment>{$name}</comment>");
296 65
                $origin[$name] = $link;
297 65
            } else {
298
                // Defer to solver.
299 15
                $this->logger->info(
300 15
                    "Deferring duplicate <comment>{$name}</comment>"
301 15
                );
302 15
                $dups[] = $link;
303
            }
304 65
        }
305 65
        $state->addDuplicateLinks($type, $dups);
306 65
        return $origin;
307
    }
308
309
    /**
310
     * Merge autoload or autoload-dev into a RootPackageInterface
311
     *
312
     * @param string $type 'autoload' or 'devAutoload'
313
     * @param RootPackageInterface $root
314
     */
315 100
    protected function mergeAutoload($type, RootPackageInterface $root)
316
    {
317 100
        $getter = 'get' . ucfirst($type);
318 100
        $setter = 'set' . ucfirst($type);
319
320 100
        $autoload = $this->package->{$getter}();
321 100
        if (empty($autoload)) {
322 95
            return;
323
        }
324
325 10
        $unwrapped = self::unwrapIfNeeded($root, $setter);
326 10
        $unwrapped->{$setter}(array_merge_recursive(
327 10
            $root->{$getter}(),
328 10
            $this->fixRelativePaths($autoload)
329 10
        ));
330 10
    }
331
332
    /**
333
     * Fix a collection of paths that are relative to this package to be
334
     * relative to the base package.
335
     *
336
     * @param array $paths
337
     * @return array
338
     */
339 10
    protected function fixRelativePaths(array $paths)
340
    {
341 10
        $base = dirname($this->path);
342 10
        $base = ($base === '.') ? '' : "{$base}/";
343
344 10
        array_walk_recursive(
345 10
            $paths,
346
            function (&$path) use ($base) {
347 10
                $path = "{$base}{$path}";
348 10
            }
349 10
        );
350 10
        return $paths;
351
    }
352
353
    /**
354
     * Extract and merge stability flags from the given collection of
355
     * requires and merge them into a RootPackageInterface
356
     *
357
     * @param RootPackageInterface $root
358
     * @param array $requires
359
     */
360 65
    protected function mergeStabilityFlags(
361
        RootPackageInterface $root,
362
        array $requires
363
    ) {
364 65
        $flags = $root->getStabilityFlags();
365 65
        $sf = new StabilityFlags($flags, $root->getMinimumStability());
366
367 65
        $unwrapped = self::unwrapIfNeeded($root, 'setStabilityFlags');
368 65
        $unwrapped->setStabilityFlags(array_merge(
369 65
            $flags,
370 65
            $sf->extractAll($requires)
371 65
        ));
372 65
    }
373
374
    /**
375
     * Merge package links of the given type  into a RootPackageInterface
376
     *
377
     * @param string $type 'conflict', 'replace' or 'provide'
378
     * @param RootPackageInterface $root
379
     */
380 100
    protected function mergePackageLinks($type, RootPackageInterface $root)
381
    {
382 100
        $linkType = BasePackage::$supportedLinkTypes[$type];
383 100
        $getter = 'get' . ucfirst($linkType['method']);
384 100
        $setter = 'set' . ucfirst($linkType['method']);
385
386 100
        $links = $this->package->{$getter}();
387 100
        if (!empty($links)) {
388 20
            $unwrapped = self::unwrapIfNeeded($root, $setter);
389
            // @codeCoverageIgnoreStart
390
            if ($root !== $unwrapped) {
391
                $this->logger->warning(
392
                    'This Composer version does not support ' .
393
                    "'{$type}' merging for aliased packages."
394
                );
395
            }
396
            // @codeCoverageIgnoreEnd
397 20
            $unwrapped->{$setter}(array_merge(
398 20
                $root->{$getter}(),
399 20
                $this->replaceSelfVersionDependencies($type, $links, $root)
400 20
            ));
401 20
        }
402 100
    }
403
404
    /**
405
     * Merge suggested packages into a RootPackageInterface
406
     *
407
     * @param RootPackageInterface $root
408
     */
409 100
    protected function mergeSuggests(RootPackageInterface $root)
410
    {
411 100
        $suggests = $this->package->getSuggests();
412 100
        if (!empty($suggests)) {
413 10
            $unwrapped = self::unwrapIfNeeded($root, 'setSuggests');
414 10
            $unwrapped->setSuggests(array_merge(
415 10
                $root->getSuggests(),
416
                $suggests
417 10
            ));
418 10
        }
419 100
    }
420
421
    /**
422
     * Merge extra config into a RootPackageInterface
423
     *
424
     * @param RootPackageInterface $root
425
     * @param PluginState $state
426
     */
427 100
    public function mergeExtra(RootPackageInterface $root, PluginState $state)
428
    {
429 100
        $extra = $this->package->getExtra();
430 100
        unset($extra['merge-plugin']);
431 100
        if (!$state->shouldMergeExtra() || empty($extra)) {
432 85
            return;
433
        }
434
435 15
        $rootExtra = $root->getExtra();
436 15
        $unwrapped = self::unwrapIfNeeded($root, 'setExtra');
437
438 15
        if ($state->replaceDuplicateLinks()) {
439 5
            $unwrapped->setExtra(
440 5
                array_merge($rootExtra, $extra)
441 5
            );
442
443 5
        } else {
444 10
            foreach (array_intersect(
445 10
                array_keys($extra),
446 10
                array_keys($rootExtra)
447 10
            ) as $key) {
448 5
                $this->logger->info(
449 5
                    "Ignoring duplicate <comment>{$key}</comment> in ".
450 5
                    "<comment>{$this->path}</comment> extra config."
451 5
                );
452 10
            }
453 10
            $unwrapped->setExtra(
454 10
                array_merge($extra, $rootExtra)
455 10
            );
456
        }
457 15
    }
458
459
    /**
460
     * Update Links with a 'self.version' constraint with the root package's
461
     * version.
462
     *
463
     * @param string $type Link type
464
     * @param array $links
465
     * @param RootPackageInterface $root
466
     * @return array
467
     */
468 75
    protected function replaceSelfVersionDependencies(
469
        $type,
470
        array $links,
471
        RootPackageInterface $root
472
    ) {
473 75
        $linkType = BasePackage::$supportedLinkTypes[$type];
474 75
        $version = $root->getVersion();
475 75
        $prettyVersion = $root->getPrettyVersion();
476 75
        $vp = $this->versionParser;
477
478 75
        $method = 'get' . ucfirst($linkType['method']);
479 75
        $packages = $root->$method();
480
481 75
        return array_map(
482 75
            function ($link) use ($linkType, $version, $prettyVersion, $vp, $packages) {
483 75
                if ('self.version' === $link->getPrettyConstraint()) {
484 10
                    if (isset($packages[$link->getSource()])) {
485
                        /** @var Link $package */
486 5
                        $package = $packages[$link->getSource()];
487 5
                        return new Link(
488 5
                            $link->getSource(),
489 5
                            $link->getTarget(),
490 5
                            $vp->parseConstraints($package->getConstraint()->getPrettyString()),
491 5
                            $linkType['description'],
492 5
                            $package->getPrettyConstraint()
493 5
                        );
494
                    }
495
496 5
                    return new Link(
497 5
                        $link->getSource(),
498 5
                        $link->getTarget(),
499 5
                        $vp->parseConstraints($version),
500 5
                        $linkType['description'],
501
                        $prettyVersion
502 5
                    );
503
                }
504 75
                return $link;
505 75
            },
506
            $links
507 75
        );
508
    }
509
510
    /**
511
     * Get a full featured Package from a RootPackageInterface.
512
     *
513
     * In Composer versions before 599ad77 the RootPackageInterface only
514
     * defines a sub-set of operations needed by composer-merge-plugin and
515
     * RootAliasPackage only implemented those methods defined by the
516
     * interface. Most of the unimplemented methods in RootAliasPackage can be
517
     * worked around because the getter methods that are implemented proxy to
518
     * the aliased package which we can modify by unwrapping. The exception
519
     * being modifying the 'conflicts', 'provides' and 'replaces' collections.
520
     * We have no way to actually modify those collections unfortunately in
521
     * older versions of Composer.
522
     *
523
     * @param RootPackageInterface $root
524
     * @param string $method Method needed
525
     * @return RootPackageInterface|RootPackage
526
     */
527 100
    public static function unwrapIfNeeded(
528
        RootPackageInterface $root,
529
        $method = 'setExtra'
530
    ) {
531
        // @codeCoverageIgnoreStart
532
        if ($root instanceof RootAliasPackage &&
533
            !method_exists($root, $method)
534
        ) {
535
            // Unwrap and return the aliased RootPackage.
536
            $root = $root->getAliasOf();
537
        }
538
        // @codeCoverageIgnoreEnd
539 100
        return $root;
540
    }
541
542
    /**
543
     * Update the root packages reference information.
544
     *
545
     * @param RootPackageInterface $root
546
     */
547 100
    protected function mergeReferences(RootPackageInterface $root)
548
    {
549
        // Merge source reference information for merged packages.
550
        // @see RootPackageLoader::load
551 100
        $references = array();
552 100
        $unwrapped = $this->unwrapIfNeeded($root, 'setReferences');
553 100
        foreach (array('require', 'require-dev') as $linkType) {
554 100
            $linkInfo = BasePackage::$supportedLinkTypes[$linkType];
555 100
            $method = 'get'.ucfirst($linkInfo['method']);
556 100
            $links = array();
557 100
            foreach ($unwrapped->$method() as $link) {
558 40
                $links[$link->getTarget()] = $link->getConstraint()->getPrettyString();
559 100
            }
560 100
            $references = $this->extractReferences($links, $references);
561 100
        }
562 100
        $unwrapped->setReferences($references);
563 100
    }
564
565
    /**
566
     * Extract vcs revision from version constraint (dev-master#abc123.
567
     *
568
     * @param array $requires
569
     * @param array $references
570
     * @return array
571
     * @see RootPackageLoader::extractReferences()
572
     */
573 100
    protected function extractReferences(array $requires, array $references)
574
    {
575 100
        foreach ($requires as $reqName => $reqVersion) {
576 40
            $reqVersion = preg_replace('{^([^,\[email protected]]+) as .+$}', '$1', $reqVersion);
577 40
            $stabilityName = VersionParser::parseStability($reqVersion);
578
            if (
579 40
                preg_match('{^[^,\[email protected]]+?#([a-f0-9]+)$}', $reqVersion, $match) &&
580
                $stabilityName === 'dev'
581 40
            ) {
582 10
                $name = strtolower($reqName);
583 10
                $references[$name] = $match[1];
584 10
            }
585 100
        }
586
587 100
        return $references;
588
    }
589
}
590
// vim:sw=4:ts=4:sts=4:et:
591