Completed
Push — master ( fe52e0...52ea7e )
by Andrii
04:42
created

GitController::actionPush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use Yii;
15
16
/**
17
 * Controller for Git.
18
 */
19
class GitController extends VcsController
20
{
21
    protected $_before = ['.gitignore'];
22
23
    /**
24
     * @var array VCS tags
25
     */
26
    protected $_tags = [];
27
28
    public function getTags()
29
    {
30
        return $this->_tags;
31
    }
32
33
    public function loadTags()
34
    {
35
        exec("git log --date=short --tags --simplify-by-decoration --pretty='format:%cd %d'", $logs);
36
        foreach ($logs as $log) {
0 ignored issues
show
Bug introduced by
The expression $logs of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
37
            preg_match('/^([0-9-]+)\s*(\(.*\))?$/', $log, $m);
38
            $this->_tags[$this->matchTag($m[2]) ?: $this->initTag] = $m[1];
39
        }
40
    }
41
42
    /**
43
     * @var string current tag
44
     */
45
    protected $tag;
46
47
    /**
48
     * @var array VCS history
49
     */
50
    protected $_history = [];
51
52
    /**
53
     * @var array all the commits
54
     */
55
    protected $_commits = [];
56
57
    public function getCommits()
58
    {
59
        return $this->_commits;
60
    }
61
62
    public function getHistory()
63
    {
64
        return $this->_history;
65
    }
66
67
    public function addHistory($commit)
68
    {
69
        $this->tag                         = $this->matchTag($commit['tag']) ?: $this->tag;
70
        $commit['tag']                     = $this->tag;
71
        $hash                              = (string) $commit['hash'];
72
        $this->_commits[$hash]             = $commit;
73
        $this->_history[$this->tag][$hash] = $commit;
74
    }
75
76
    public function loadHistory()
77
    {
78
        exec("git log --date=short --pretty='format:%h %ad %ae %s |%d'", $logs);
79
        $this->tag = $this->lastTag;
80
        foreach ($logs as $log) {
0 ignored issues
show
Bug introduced by
The expression $logs of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
81
            if (!preg_match('/^(\w+) ([0-9-]+) (\S+) (.*?)\s+\| ?(\([^\(\)]+\))?$/', $log, $m)) {
82
                Yii::error('failed parse git log');
83
                die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method loadHistory() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
84
            }
85
            $this->addHistory([
86
                'hash'    => $m[1],
87
                'date'    => $m[2],
88
                'email'   => $m[3],
89
                'comment' => $m[4],
90
                'tag'     => $m[5],
91
            ]);
92
        }
93
    }
94
95
    public function matchTag($str)
96
    {
97
        preg_match('/^\((.*?)\)$/', $str, $m);
98
        $refs = explode(', ', $m[1]);
99
        foreach ($refs as $ref) {
100
            if (preg_match('/^tag: (.*)$/', $ref, $m)) {
101
                return $m[1];
102
            }
103
        }
104
105
        return false;
106
    }
107
108
    public function actionLoad()
109
    {
110
        $this->loadTags();
111
        $this->loadHistory();
112
    }
113
114
    public function actionPush()
115
    {
116
        return $this->passthru('git', 'push');
117
    }
118
119
    public function getUserName()
120
    {
121
        return trim(`git config --get user.name`);
122
    }
123
124
    public function getUserEmail()
125
    {
126
        return trim(`git config --get user.email`);
127
    }
128
129
    public function getYear()
130
    {
131
        $tags = $this->getTags();
132
        if ($tags) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tags of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133
            $date = array_pop($this->getTags());
0 ignored issues
show
Bug introduced by
$this->getTags() cannot be passed to array_pop() as the parameter $array expects a reference.
Loading history...
134
        } else {
135
            $date = `git log --reverse --pretty=%ai | head -n 1`;
136
        }
137
        $year = $date ? date('Y', strtotime($date)) : '';
138
139
        return $year;
140
    }
141
}
142