Completed
Push — master ( 4bb285...2f71f6 )
by Andrii
03:37
created

Version::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
/**
14
 * `version` file component.
15
 */
16
class Version extends \hidev\base\ConfigFile
17
{
18
    protected $_file = 'version';
19
20
    protected $fileRelease;
21
    protected $fileHash;
22
    protected $release;
23
    protected $date;
24
    protected $time;
25
    protected $zone;
26
    protected $hash;
27
    protected $commit;
28
    protected $branch;
29
30
    public function init()
31
    {
32
        if ($this->exists()) {
33
            $this->parseFile();
34
            $this->fileRelease = $this->release;
35
            $this->fileHash = $this->hash;
36
        }
37
    }
38
39
    public function parseFile()
40
    {
41
        $line = trim($this->getFile()->read());
42
        list($tmp, $this->release, $this->date, $this->time, $this->zone, $this->hash) = explode(' ', $line);
0 ignored issues
show
Unused Code introduced by
The assignment to $tmp is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
43
        $this->parseRelease();
44
    }
45
46
    public function parseRelease()
47
    {
48
        if (preg_match('/^(\w+)-([0-9\.]+)-(\S+)/', $this->release, $matches)) {
49
            $this->release  = $matches[1];
50
            $this->branch   = $matches[2];
51
        }
52
    }
53
54
    public function renderFile()
55
    {
56
        return implode(' ', [$this->getName(), $this->renderRelease(), $this->date, $this->time, $this->zone, $this->hash]);
57
    }
58
59
    public function renderRelease()
60
    {
61
        if ($this->branch) {
62
            return implode('-', [$this->release, $this->branch, substr($this->hash, 0, 7)]);
63
        } else {
64
            return $this->release;
65
        }
66
    }
67
68
    public function update($release)
69
    {
70
        $this->load();
71
        $this->setRelease($release);
72
        $this->save();
73
    }
74
75
    public function load()
76
    {
77
        $line = trim($this->exec('git', ['log', '-n', '1', '--pretty=%ai %H %s'])[0]);
0 ignored issues
show
Documentation introduced by
array('log', '-n', '1', '--pretty=%ai %H %s') is of type array<integer,string,{"0..."string","3":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        list($this->date, $this->time, $this->zone, $this->hash, $this->commit) = explode(' ', $line, 5);
79
        if ($this->hash !== $this->fileHash) {
80
            $this->branch  = $this->release;
81
            $this->release = 'dev';
82
        }
83
    }
84
85
    public function save()
86
    {
87
        $this->getFile()->write($this->renderFile() . "\n");
88
    }
89
90
    public function setRelease($release = null, $branch = null)
91
    {
92
        $this->setBranch($branch);
93
        $this->release = $this->getRelease($release);
94
    }
95
96
    public function getRelease($release = null)
97
    {
98
        return $release ?: $this->release ?: 'dev';
99
    }
100
101
    public function setBranch($branch)
102
    {
103
        if (isset($branch)) {
104
            $this->branch = $branch;
105
        }
106
    }
107
108
    public function getAbspath()
109
    {
110
        return $this->getFile()->getAbspath();
111
    }
112
113
    public function getName()
114
    {
115
        return $this->take('package')->name;
116
    }
117
}
118