Completed
Push — master ( f657d9...210110 )
by ARCANEDEV
8s
created

RequiresTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 129
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeRequires() 0 20 2
A mergeDevRequires() 0 20 2
A mergeStabilityFlags() 0 11 1
A mergeLinks() 0 16 4
replaceSelfVersionDependencies() 0 3 ?
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
     |  Traits
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    use PackageTrait;
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Main Functions
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * Merge require into a RootPackage.
27
     *
28
     * @param  \Composer\Package\RootPackageInterface    $root
29
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
30
     */
31
    protected function mergeRequires(RootPackageInterface $root, PluginState $state)
32
    {
33
        if ( ! empty($requires = $this->getPackage()->getRequires())) {
34
            $this->mergeStabilityFlags($root, $requires);
35
36
            $duplicateLinks = [];
37
            $requires       = $this->replaceSelfVersionDependencies(
38
                'require', $requires, $root
39
            );
40
41
            $root->setRequires($this->mergeLinks(
42
                $root->getRequires(),
43
                $requires,
44
                $state->replaceDuplicateLinks(),
45
                $duplicateLinks
46
            ));
47
48
            $state->addDuplicateLinks('require', $duplicateLinks);
49
        }
50
    }
51
52
    /**
53
     * Merge require-dev into RootPackage.
54
     *
55
     * @param  \Composer\Package\RootPackageInterface    $root
56
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
57
     */
58
    protected function mergeDevRequires(RootPackageInterface $root, PluginState $state)
59
    {
60
        if ( ! empty($requires = $this->getPackage()->getDevRequires())) {
61
            $this->mergeStabilityFlags($root, $requires);
62
63
            $duplicateLinks = [];
64
            $requires       = $this->replaceSelfVersionDependencies(
65
                'require-dev', $requires, $root
66
            );
67
68
            $root->setDevRequires($this->mergeLinks(
69
                $root->getDevRequires(),
70
                $requires,
71
                $state->replaceDuplicateLinks(),
72
                $duplicateLinks
73
            ));
74
75
            $state->addDuplicateLinks('require-dev', $duplicateLinks);
76
        }
77
    }
78
79
    /* ------------------------------------------------------------------------------------------------
80
     |  Other Functions
81
     | ------------------------------------------------------------------------------------------------
82
     */
83
    /**
84
     * Extract and merge stability flags from the given collection of
85
     * requires and merge them into a RootPackage.
86
     *
87
     * @param  \Composer\Package\RootPackageInterface  $root
88
     * @param  \Composer\Package\Link[]                $requires
89
     */
90
    protected function mergeStabilityFlags(RootPackageInterface $root, array $requires)
91
    {
92
        $flags = StabilityFlags::extract(
93
            $root->getStabilityFlags(),
94
            $root->getMinimumStability(),
95
            $requires
96
        );
97
98
        self::unwrapIfNeeded($root, 'setStabilityFlags')
99
            ->setStabilityFlags($flags);
100
    }
101
102
    /**
103
     * Merge two collections of package links and collect duplicates for subsequent processing.
104
     *
105
     * @param  \Composer\Package\Link[]  $origin          Primary collection
106
     * @param  array                     $merge           Additional collection
107
     * @param  bool                      $replace         Replace existing links ?
108
     * @param  array                     $duplicateLinks  Duplicate storage
109
     *
110
     * @return \Composer\Package\Link[]                   Merged collection
111
     */
112
    private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks)
113
    {
114
        foreach ($merge as $name => $link) {
115
            if ( ! isset($origin[$name]) || $replace) {
116
                $this->getLogger()->info("Merging <comment>{$name}</comment>");
117
                $origin[$name] = $link;
118
            }
119
            else {
120
                // Defer to solver.
121
                $this->getLogger()->info("Deferring duplicate <comment>{$name}</comment>");
122
                $duplicateLinks[] = $link;
123
            }
124
        }
125
126
        return $origin;
127
    }
128
129
    /**
130
     * Update Links with a 'self.version' constraint with the root package's version.
131
     *
132
     * @param  string                                  $type
133
     * @param  array                                   $links
134
     * @param  \Composer\Package\RootPackageInterface  $root
135
     *
136
     * @return array
137
     */
138
    abstract protected function replaceSelfVersionDependencies(
139
        $type, array $links, RootPackageInterface $root
140
    );
141
}
142