Passed
Pull Request — develop (#1)
by Jimmy
06:04
created

Version   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 151
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseVersionFile() 0 26 6
A generateSemVer() 0 10 3
A parseVersion() 0 11 2
A __toString() 0 3 1
A __construct() 0 7 1
1
<?php
2
3
namespace Spinen\Version;
4
5
/**
6
 * Class Version
7
 *
8
 * Parse the version file into the parts
9
 *
10
 * @package App\Support
11
 */
12
class Version
13
{
14
    /**
15
     * Major version
16
     *
17
     * @var integer|null
18
     */
19
    public $major = null;
20
21
    /**
22
     * Meta
23
     *
24
     * Extra data about the version with dots separating key data and underscores for spaces
25
     *
26
     * @var string|null
27
     */
28
    public $meta = null;
29
30
    /**
31
     * Minor version
32
     *
33
     * @var integer|null
34
     */
35
    public $minor = null;
36
37
    /**
38
     * Patch version
39
     *
40
     * @var integer|null
41
     */
42
    public $patch = null;
43
44
    /**
45
     * Pre release
46
     *
47
     * This will most likely be the branch version, unless we are on the master branch
48
     *
49
     * @var string|null
50
     */
51
    public $pre_release = null;
52
53
    /**
54
     * Version in SemVer format
55
     *
56
     * @var string
57
     */
58
    public $semver;
59
60
    /**
61
     * The version of the application
62
     *
63
     * @var string
64
     */
65
    public $version = "UNDEFINED";
66
67
    /**
68
     * The file that holds the version info
69
     *
70
     * @var string
71
     */
72
    protected $version_file;
73
74
    /**
75
     * Version constructor.
76
     *
77
     * @param string $file
78
     */
79 9
    public function __construct($file)
80
    {
81 9
        $this->version_file = $file;
82
83 9
        $this->parseVersionFile();
84
85 9
        $this->generateSemVer();
86
    }
87
88
    /**
89
     * If the object is used as a string, then return the full version
90
     *
91
     * @return string
92
     */
93 8
    public function __toString()
94
    {
95 8
        return $this->semver;
96
    }
97
98
    /**
99
     * Put together version parts into the SemVer format
100
     */
101 9
    protected function generateSemVer()
102
    {
103 9
        $this->semver = $this->version;
104
105 9
        if ($this->pre_release) {
106 3
            $this->semver .= '-' . $this->pre_release;
107
        }
108
109 9
        if ($this->meta) {
110 3
            $this->semver .= '+' . $this->meta;
111
        }
112
    }
113
114
    /**
115
     * If the read in line matches a version, then parse it
116
     *
117
     * @param string $version
118
     *
119
     * @return bool
120
     */
121 7
    protected function parseVersion($version)
122
    {
123 7
        if (!preg_match('/\\d+\\.\\d+\\.\\d+/u', $version)) {
124 1
            return false;
125
        }
126
127 6
        $this->version = $version;
128
129 6
        list($this->major, $this->minor, $this->patch) = array_map('intval', explode(".", $this->version));
130
131 6
        return true;
132
    }
133
134
    /**
135
     * Read the version file into the various parts
136
     */
137 9
    protected function parseVersionFile()
138
    {
139 9
        if (!file_exists($this->version_file)) {
140 1
            return;
141
        }
142
143
        // Read file in as an array & remove any empty lines
144 8
        $version_parts = array_filter(explode("\n", @file_get_contents($this->version_file)));
145
146
        // First line is the version
147 8
        if (empty($version_parts) or !$this->parseVersion(array_shift($version_parts))) {
148 2
            return;
149
        }
150
151
        // Next line is branch/pre release
152 6
        $pre_release = array_shift($version_parts);
153
154 6
        $this->pre_release = ($pre_release !== 'master') ? $pre_release : null;
155
156
        // Is there anything left in the file for meta?
157 6
        if (empty($version_parts)) {
158 3
            return;
159
        }
160
161
        // Everything left is the meta, so concatenate it with .'s & replace the spaces with _'s
162 3
        $this->meta = preg_replace("/\\s+/u", "_", implode('.', $version_parts));
163
    }
164
}
165