Completed
Pull Request — master (#151)
by Patrick D
23:18 queued 21:22
created

StabilityFlags::getParsedStability()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 1
crap 3.054
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 Composer\Package\BasePackage;
14
use Composer\Package\Version\VersionParser;
15
16
/**
17
 * Adapted from Composer's RootPackageLoader::extractStabilityFlags
18
 * @author Bryan Davis <[email protected]>
19
 */
20
class StabilityFlags
21
{
22
23
    /**
24
     * @var array Current package name => stability mappings
25
     */
26
    protected $stabilityFlags;
27
28
    /**
29
     * @var int Current default minimum stability
30
     */
31
    protected $minimumStability;
32
33
    /**
34
     * @var string Regex to extract an explict stability flag (eg '@dev')
35
     */
36
    protected $explicitStabilityRe;
37
38
39
    /**
40
     * @param array $stabilityFlags Current package name => stability mappings
41
     * @param int $minimumStability Current default minimum stability
42
     */
43 55
    public function __construct(
44
        array $stabilityFlags = array(),
45
        $minimumStability = BasePackage::STABILITY_STABLE
46
    ) {
47 55
        $this->stabilityFlags = $stabilityFlags;
48 55
        $this->minimumStability = $this->getStabilityInt($minimumStability);
49 55
        $this->explicitStabilityRe = '/^[^@]*?@(' .
50 55
            implode('|', array_keys(BasePackage::$stabilities)) .
51 55
            ')$/i';
52 55
    }
53
54
    /**
55
     * Get the stability value for a given string.
56
     *
57
     * @param string $name Stability name
58
     * @return int Stability value
59
     */
60 55
    protected function getStabilityInt($name)
61
    {
62 55
        $name = VersionParser::normalizeStability($name);
63 55
        return isset(BasePackage::$stabilities[$name]) ?
64 55
            BasePackage::$stabilities[$name] :
65 55
            BasePackage::STABILITY_STABLE;
66
    }
67
68
    /**
69
     * Extract and merge stability flags from the given collection of
70
     * requires with another collection of stability flags.
71
     *
72
     * @param array $requires New package name => link mappings
73
     * @return array Unified package name => stability mappings
74
     */
75 55
    public function extractAll(array $requires)
76
    {
77 55
        $flags = array();
78
79 55
        foreach ($requires as $name => $link) {
80 55
            $name = strtolower($name);
81 55
            $version = $link->getPrettyConstraint();
82
83 55
            $stability = $this->getExplicitStability($version);
84
85 55
            if ($stability === null) {
86 15
                $stability = $this->getParsedStability($version);
87 15
            }
88
89 55
            $flags[$name] = max($stability, $this->getCurrentStability($name));
90 55
        }
91
92
        // Filter out null stability values
93 55
        return array_filter($flags, function ($v) {
94 55
            return $v !== null;
95 55
        });
96
    }
97
98
99
    /**
100
     * Extract the most unstable explicit stability (eg '@dev') from a version
101
     * specification.
102
     *
103
     * @param string $version
104
     * @return int|null Stability or null if no explict stability found
105
     */
106 55
    protected function getExplicitStability($version)
107
    {
108 55
        $found = null;
109 55
        $constraints = $this->splitConstraints($version);
110 55
        foreach ($constraints as $constraint) {
111 55
            if (preg_match($this->explicitStabilityRe, $constraint, $match)) {
112 40
                $stability = $this->getStabilityInt($match[1]);
113 40
                $found = max($stability, $found);
114 40
            }
115 55
        }
116 55
        return $found;
117
    }
118
119
120
    /**
121
     * Split a version specification into a list of version constraints.
122
     *
123
     * @param string $version
124
     * @return array
125
     */
126 55
    protected function splitConstraints($version)
127
    {
128 55
        $found = array();
129 55
        $orConstraints = preg_split('/\s*\|\|?\s*/', trim($version));
130 55
        foreach ($orConstraints as $constraints) {
131 55
            $andConstraints = preg_split(
132 55
                '/(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)/',
133
                $constraints
134 55
            );
135 55
            foreach ($andConstraints as $constraint) {
136 55
                $found[] = $constraint;
137 55
            }
138 55
        }
139 55
        return $found;
140
    }
141
142
143
    /**
144
     * Get the stability of a version
145
     *
146
     * @param string $version
147
     * @return int|null Stability or null if STABLE or less than minimum
148
     */
149 15
    protected function getParsedStability($version)
150
    {
151
        // Drop aliasing if used
152 15
        $version = preg_replace('/^([^,\s@]+) as .+$/', '$1', $version);
153 15
        $stability = $this->getStabilityInt(
154 15
            VersionParser::parseStability($version)
155 15
        );
156
157 15
        if ($stability === BasePackage::STABILITY_STABLE ||
158 15
            $this->minimumStability > $stability
159 15
        ) {
160
            // Ignore if 'stable' or more stable than the global
161
            // minimum
162
            $stability = null;
163
        }
164
165 15
        return $stability;
166
    }
167
168
169
    /**
170
     * Get the current stability of a given package.
171
     *
172
     * @param string $name
173
     * @return int|null Stability of null if not set
174
     */
175 55
    protected function getCurrentStability($name)
176
    {
177 55
        return isset($this->stabilityFlags[$name]) ?
178 55
            $this->stabilityFlags[$name] : null;
179
    }
180
}
181
// vim:sw=4:ts=4:sts=4:et:
182