Completed
Pull Request — master (#117)
by Fabian
23:06 queued 18:41
created

ExtraPackage::mergeDev()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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