Completed
Push — master ( b60952...ee4ec3 )
by Greg
02:01
created

VersionIdentifiers   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 0
dl 0
loc 145
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setVidPattern() 0 4 1
A setVvalPattern() 0 4 1
A add() 0 4 1
A addVidsFromMessage() 0 24 5
A allExist() 0 15 4
A someTitleContains() 0 9 3
A isEmpty() 0 4 1
A all() 0 8 2
A ids() 0 4 1
A __toString() 0 16 3
1
<?php
2
3
namespace Hubph;
4
5
class VersionIdentifiers
6
{
7
    protected $data = [];
8
    protected $vidPattern;
9
    protected $vvalPattern;
10
11
    const DEFAULT_VID = '[A-Za-z_-]+ ?';
12
    const DEFAULT_VVAL = '#.#.#';
13
    const EXTRA = '(?:(stable|beta|b|RC|alpha|a|patch|pl|p)((?:[.-]?\d+)*+)?)?([.-]?dev)?';
14
    const NUMBER = '[0-9]+';
15
16
    /**
17
     * VersionIdentifiers constructor
18
     */
19
    public function __construct()
20
    {
21
        $this->vidPattern = '';
22
        $this->vvalPattern = '';
23
    }
24
25
    public function setVidPattern($vidPattern)
26
    {
27
        $this->vidPattern = $vidPattern;
28
    }
29
30
    public function setVvalPattern($vvalPattern)
31
    {
32
        $this->vvalPattern = $vvalPattern;
33
    }
34
35
    /**
36
     * Multiple provided value with our multiplier
37
     *
38
     * @param $value multiplicand
39
     * @return integer product of multiplier and multiplicand
40
     */
41
    public function add($vid, $vval)
42
    {
43
        $this->data[$vid] = $vval;
44
    }
45
46
    /**
47
     * Given a simple message like "Update to WordPress 4.9.8", return the
48
     * correct vid/vval pair. In this case, the vid is "WordPress " and the
49
     * vval is "4.9.8".
50
     *
51
     * @param string $message a commit message
52
     */
53
    public function addVidsFromMessage($message)
54
    {
55
        $vidPattern = empty($this->vidPattern) ? self::DEFAULT_VID : $this->vidPattern;
56
        $vvalPattern = empty($this->vvalPattern) ? self::DEFAULT_VVAL : $this->vvalPattern;
57
58
        $vid_vval_regex = "({$vidPattern})({$vvalPattern}[._-]?)" . self::EXTRA;
59
60
        $vid_vval_regex = str_replace('#.', '#\\.', $vid_vval_regex);
61
        $vid_vval_regex = str_replace('#', self::NUMBER, $vid_vval_regex);
62
63
        if (!preg_match_all("#$vid_vval_regex#", $message, $matches, PREG_SET_ORDER)) {
64
            throw new \Exception('Message does not contain a semver release identifier, e.g.: Update to myproject-1.2.3');
65
        }
66
        foreach ($matches as $matchset) {
67
            array_shift($matchset);
68
            $vid = array_shift($matchset);
69
            $vval = implode('', $matchset);
70
71
            // Trim trailing punctuation.
72
            $vval = preg_replace('#[^0-9a-zA-Z]*$#', '', $vval);
73
74
            $this->add($vid, $vval);
75
        }
76
    }
77
78
    /**
79
     * Check to see if a list of PR titles collectively contain all of the
80
     * vids with vvals.
81
     *
82
     * @param string[] $titles
83
     */
84
    public function allExist($titles)
85
    {
86
        // If we are empty do we return 'true' or 'false'? Maybe throw.
87
        if ($this->isEmpty()) {
88
            return false;
89
        }
90
91
        foreach ($this->all() as $value) {
92
            if (!$this->someTitleContains($titles, $value)) {
93
                return false;
94
            }
95
        }
96
97
        return true;
98
    }
99
100
    /**
101
     *  foreach ($existingPRs as $pr) {
102
     *    if (strpos($pr['title'], $value) !== false) {
103
     */
104
    protected function someTitleContains($titles, $value)
105
    {
106
        foreach ($titles as $title) {
107
            if (strpos($title, $value) !== false) {
108
                return true;
109
            }
110
        }
111
        return false;
112
    }
113
114
    public function isEmpty()
115
    {
116
        return empty($this->data);
117
    }
118
119
    public function all()
120
    {
121
        $result = [];
122
        foreach ($this->data as $vid => $vval) {
123
            $result[] = "{$vid}{$vval}";
124
        }
125
        return $result;
126
    }
127
128
    public function ids()
129
    {
130
        return array_keys($this->data);
131
    }
132
133
    public function __toString()
134
    {
135
        if ($this->isEmpty()) {
136
            return '';
137
        }
138
139
        $all = $this->all();
140
141
        $last = array_pop($all);
142
143
        if (empty($all)) {
144
            return $last;
145
        }
146
147
        return implode(', ', $all) . ", and $last";
148
    }
149
}
150