RequiresTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 131
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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