Version::parseFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
/**
14
 * `version` file component.
15
 */
16
class Version extends File
17
{
18
    protected $_file = 'version';
19
20
    protected $parsedRelease;
21
    protected $parsedHash;
22
    protected $parsedName;
23
    protected $release;
24
    protected $date;
25
    protected $time;
26
    protected $zone;
27
    protected $hash;
28
    protected $commit;
29
    protected $branch;
30
31
    public function init()
32
    {
33
        if ($this->exists()) {
34
            $this->parseFile();
35
            $this->parsedRelease = $this->release;
36
            $this->parsedHash = $this->hash;
37
        }
38
    }
39
40
    public function parseFile()
41
    {
42
        $line = trim($this->getFile()->read());
43
        list($this->parsedName, $this->release, $this->date, $this->time, $this->zone, $this->hash) = explode(' ', $line);
44
        $this->parseRelease();
45
    }
46
47
    public function parseRelease()
48
    {
49
        if (preg_match('/^(\S+)-(\S+)-(\S+)/', $this->release, $matches)) {
50
            $this->release  = $matches[1];
51
            $this->branch   = $matches[2];
52
        }
53
    }
54
55
    public function renderFile()
56
    {
57
        return implode(' ', [$this->getName(), $this->renderRelease(), $this->date, $this->time, $this->zone, $this->hash]);
58
    }
59
60
    public function renderRelease()
61
    {
62
        if ($this->release === 'dev') {
63
            return implode('-', [$this->release, $this->getBranch(), substr($this->hash, 0, 7)]);
64
        } else {
65
            return $this->release;
66
        }
67
    }
68
69
    public function getBranch()
70
    {
71
        return $this->branch ?: 'master';
72
    }
73
74
    public function update($release)
75
    {
76
        $this->load();
77
        $this->setRelease($release);
78
        $this->save();
79
    }
80
81
    public function load()
82
    {
83
        $line = trim($this->exec('git', ['log', '-n', '1', '--pretty=%ai %H %s'])[0]);
0 ignored issues
show
Bug introduced by
array('log', '-n', '1', '--pretty=%ai %H %s') of type array<integer,string> is incompatible with the type string expected by parameter $args of hidev\base\Component::exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $line = trim($this->exec('git', /** @scrutinizer ignore-type */ ['log', '-n', '1', '--pretty=%ai %H %s'])[0]);
Loading history...
84
        list($this->date, $this->time, $this->zone, $this->hash, $this->commit) = explode(' ', $line, 5);
85
        if ($this->hash !== $this->parsedHash) {
86
            if ($this->release !== 'dev') {
87
                $this->branch = $this->release;
88
            }
89
            $this->release = 'dev';
90
        }
91
    }
92
93
    public function save()
94
    {
95
        $this->getFile()->write($this->renderFile() . "\n");
96
    }
97
98
    public function setRelease($release = null, $branch = '')
99
    {
100
        $this->release = $this->getRelease($release);
101
        $this->setBranch($branch, $this->release);
102
    }
103
104
    public function getRelease($release = null)
105
    {
106
        return $release ?: $this->release ?: 'dev';
107
    }
108
109
    public function setBranch($branch, $release = null)
110
    {
111
        if (isset($branch) && $release !== 'dev') {
112
            $this->branch = $branch;
113
        }
114
    }
115
116
    public function getAbspath()
117
    {
118
        return $this->getFile()->getAbspath();
119
    }
120
121
    public function getName()
122
    {
123
        return $this->take('package')->name;
124
    }
125
}
126