Completed
Pull Request — master (#8)
by ARCANEDEV
02:14
created

RequiresTrait::mergeLinks()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 3
nop 4
1
<?php namespace Arcanedev\Composer\Entities\PackageTraits;
2
3
use Arcanedev\Composer\Entities\PluginState;
4
use Arcanedev\Composer\Entities\StabilityFlags;
5
use Composer\Package\RootPackageInterface;
6
7
/**
8
 * Trait     RequiresTrait
9
 *
10
 * @package  Arcanedev\Composer\Entities\PackageTraits
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
trait RequiresTrait
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /** @var \Arcanedev\Composer\Utilities\Logger $logger */
20
    protected $logger;
21
22
    /** @var \Composer\Package\CompletePackage $package */
23
    protected $package;
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Main Functions
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * Merge require into a RootPackage.
31
     *
32
     * @param  \Composer\Package\RootPackageInterface    $root
33
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
34
     */
35
    protected function mergeRequires(RootPackageInterface $root, PluginState $state)
36
    {
37
        if ( ! empty($requires = $this->package->getRequires())) {
38
            $this->mergeStabilityFlags($root, $requires);
39
40
            $duplicateLinks = [];
41
            $requires       = $this->replaceSelfVersionDependencies(
42
                'require', $requires, $root
43
            );
44
45
            $root->setRequires($this->mergeLinks(
46
                $root->getRequires(),
47
                $requires,
48
                $state->replaceDuplicateLinks(),
49
                $duplicateLinks
50
            ));
51
52
            $state->addDuplicateLinks('require', $duplicateLinks);
53
        }
54
    }
55
56
    /**
57
     * Merge require-dev into RootPackage.
58
     *
59
     * @param  \Composer\Package\RootPackageInterface    $root
60
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
61
     */
62
    protected function mergeDevRequires(RootPackageInterface $root, PluginState $state)
63
    {
64
        if ( ! empty($requires = $this->package->getDevRequires())) {
65
            $this->mergeStabilityFlags($root, $requires);
66
67
            $duplicateLinks = [];
68
            $requires       = $this->replaceSelfVersionDependencies(
69
                'require-dev', $requires, $root
70
            );
71
72
            $root->setDevRequires($this->mergeLinks(
73
                $root->getDevRequires(),
74
                $requires,
75
                $state->replaceDuplicateLinks(),
76
                $duplicateLinks
77
            ));
78
79
            $state->addDuplicateLinks('require-dev', $duplicateLinks);
80
        }
81
    }
82
83
    /* ------------------------------------------------------------------------------------------------
84
     |  Other Functions
85
     | ------------------------------------------------------------------------------------------------
86
     */
87
    /**
88
     * Extract and merge stability flags from the given collection of
89
     * requires and merge them into a RootPackage.
90
     *
91
     * @param  \Composer\Package\RootPackageInterface  $root
92
     * @param  \Composer\Package\Link[]                $requires
93
     */
94
    protected function mergeStabilityFlags(RootPackageInterface $root, array $requires)
95
    {
96
        $flags = StabilityFlags::extract(
97
            $root->getStabilityFlags(),
98
            $root->getMinimumStability(),
99
            $requires
100
        );
101
102
        self::unwrapIfNeeded($root, 'setStabilityFlags')
103
            ->setStabilityFlags($flags);
104
    }
105
106
    /**
107
     * Merge two collections of package links and collect duplicates for subsequent processing.
108
     *
109
     * @param  \Composer\Package\Link[]  $origin          Primary collection
110
     * @param  array                     $merge           Additional collection
111
     * @param  bool                      $replace         Replace existing links ?
112
     * @param  array                     $duplicateLinks  Duplicate storage
113
     *
114
     * @return \Composer\Package\Link[]                   Merged collection
115
     */
116
    private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks)
117
    {
118
        foreach ($merge as $name => $link) {
119
            if ( ! isset($origin[$name]) || $replace) {
120
                $this->logger->info("Merging <comment>{$name}</comment>");
121
                $origin[$name] = $link;
122
            }
123
            else {
124
                // Defer to solver.
125
                $this->logger->info("Deferring duplicate <comment>{$name}</comment>");
126
                $duplicateLinks[] = $link;
127
            }
128
        }
129
130
        return $origin;
131
    }
132
133
    /**
134
     * Update Links with a 'self.version' constraint with the root package's version.
135
     *
136
     * @param  string                                  $type
137
     * @param  array                                   $links
138
     * @param  \Composer\Package\RootPackageInterface  $root
139
     *
140
     * @return array
141
     */
142
    abstract protected function replaceSelfVersionDependencies(
143
        $type, array $links, RootPackageInterface $root
144
    );
145
146
    /**
147
     * Get a full featured Package from a RootPackageInterface.
148
     *
149
     * @param  \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage  $root
150
     * @param  string                                                                $method
151
     *
152
     * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage
153
     */
154
    abstract protected function unwrapIfNeeded(
155
        RootPackageInterface $root, $method = 'setExtra'
156
    );
157
}
158