Completed
Push — master ( 82d2b1...d9ad16 )
by Andrii
04:10
created

CommitsHandler::hasCommit()   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
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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) 2014-2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\handlers;
13
14
use Yii;
15
16
/**
17
 * Handler for commits.md file.
18
 */
19
class CommitsHandler extends BaseHandler
20
{
21
    public $type = 'commits';
22
23
    protected $_tag;
24
    protected $_note;
25
    protected $_hash;
26
    protected $_history = [];
27
    protected $_commits = [];
28
29
    public function getTag()
30
    {
31
        return $this->_tag;
32
    }
33
34
    public function getNote()
35
    {
36
        return $this->_note;
37
    }
38
39
    public function getHash()
40
    {
41
        return $this->_hash;
42
    }
43
44
    public function setTag($tag)
45
    {
46
        $this->_tag  = $tag;
47
        $this->_note = '';
48
        $this->_hash = '';
49
    }
50
51
    public function setNote($note)
52
    {
53
        $this->_note = $note;
54
        $this->_hash = '';
55
    }
56
57
    public function setHash($hash)
58
    {
59
        $this->_hash = $hash;
60
    }
61
62
    public function addTag($tag, $label = null)
63
    {
64
        $this->tag = $tag;
0 ignored issues
show
Documentation introduced by
The property tag does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
        $ref       = &$this->_history[$tag]['tag'];
66
        $ref       = $label ?: $ref ?: $tag;
67
    }
68
69
    public function addNote($note, $label = null)
70
    {
71
        $this->note = $note;
0 ignored issues
show
Documentation introduced by
The property note does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
        $ref        = &$this->_history[$this->tag][$note]['note'];
0 ignored issues
show
Documentation introduced by
The property tag does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
        $ref        = $label ?: $ref ?: $note;
74
    }
75
76
    public function addHash($hash, $label)
77
    {
78
        $this->_hash                    = $hash;
79
        $this->_commits[(string) $hash] = $label;
80
    }
81
82
    public function hasCommit($hash)
83
    {
84
        return array_key_exists((string) $hash, $this->_commits);
85
    }
86
87
    public function parsePath($path, $minimal = null)
88
    {
89
        $this->tag      = static::getVcs()->lastTag;
0 ignored issues
show
Documentation introduced by
The property tag does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
        $this->_history = [
91
            $this->tag => [],
0 ignored issues
show
Documentation introduced by
The property tag does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
        ];
93
        $lines = is_file($path) ? $this->readArray($path) : [];
94
        foreach ($lines as $str) {
0 ignored issues
show
Bug introduced by
The expression $lines of type array|string 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...
95
            $str = rtrim($str);
96
            ++$no;
0 ignored issues
show
Bug introduced by
The variable $no does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
97
            if (!$str || $no < 3) {
98
                continue;
99
            }
100
            if (preg_match('/^# /', $str)) {
101
                continue;
102
            };
103
            if (preg_match('/^## (.*)$/', $str, $m)) {
104
                $label = $m[1];
105
                foreach ([static::getVcs()->lastTag, static::getVcs()->initTag] as $z) {
106
                    if (stripos($label, $z) !== false) {
107
                        $this->addTag($z, $label);
108
                        continue 2;
109
                    }
110
                }
111
                preg_match('/^(\S+)\s*(.*)$/', $label, $m);
112
                $this->addTag($m[1], $label);
113
                continue;
114
            }
115
            if (preg_match('/^- (.*)$/', $str, $m)) {
116
                $this->addNote($m[1]);
117
                continue;
118
            }
119
            if (preg_match('/^\s+- ([0-9a-fA-F]{7})/', $str, $m)) {
120
                $this->addHash($m[1], $str);
121
            }
122
            $this->_history[$this->tag][$this->note][$this->hash][] = $str;
0 ignored issues
show
Documentation introduced by
The property tag does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property note does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property hash does not exist on object<hidev\handlers\CommitsHandler>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
        }
124
125
        return $this->_history;
126
    }
127
128
    public function addHistory($commit, $front = false)
129
    {
130
        $tag    = $commit['tag'];
131
        $note   = $commit['note'];
132
        $hash   = $commit['hash'];
133
        $render = static::renderCommit($commit);
134
        $hashes = &$this->_history[$tag][$note];
135
        $hashes = (array) $hashes;
136
        if ($front) {
137
            $hashes = [$hash => [$render]] + $hashes;
138
        } else {
139
            $hashes[$hash][] = $render;
140
        }
141
    }
142
143
    public function hasHistory($tag)
144
    {
145
        return array_key_exists($tag, $this->_history);
146
    }
147
148
    public function getHistory()
149
    {
150
        return $this->_history;
151
    }
152
153
    public function render($data)
154
    {
155
        $res = static::renderHeader('commits history');
156
157
        foreach (array_reverse(static::getVcs()->commits, true) as $hash => $commit) {
158
            if ($this->hasCommit($hash)) {
159
                continue;
160
            }
161
            $this->addHistory($commit, true);
162
        }
163
        if (!$this->hasHistory(static::getVcs()->initTag)) {
164
            $this->addHistory(['tag' => static::getVcs()->initTag]);
165
        }
166
167
        foreach ($this->_history as $tag => $notes) {
168
            $tag = static::arrayPop($notes, 'tag') ?: $tag;
169
            $new = static::arrayPop($notes, '') ?: [];
170
            $res .= static::renderTag($tag);
171
            foreach ($new as $hash => $lines) {
172
                $res .= static::renderLines($lines);
173
            }
174
            foreach ($notes as $note => $cs) {
175
                $note = static::arrayPop($cs, 'note');
176
                $res .= static::renderNote($note);
177
                foreach ($cs as $hash => $lines) {
178
                    $res .= static::renderLines($lines);
179
                }
180
            }
181
        }
182
183
        return $res;
184
    }
185
186
    public static function arrayPop(&$array, $key)
187
    {
188
        $res = $array[$key];
189
        unset($array[$key]);
190
191
        return $res;
192
    }
193
194
    public static function renderNote($note)
195
    {
196
        return "- $note\n";
197
    }
198
199
    public static function renderLines(array $lines)
200
    {
201
        $res = implode("\n", array_filter($lines));
202
203
        return $res ? $res . "\n" : '';
204
    }
205
206
    public static function renderCommit($commit)
207
    {
208
        return static::skipCommit($commit) ? '' : "    - $commit[hash] $commit[date] $commit[comment] ($commit[email])";
209
    }
210
211
    public static function skipCommit($commit)
212
    {
213
        $comment = $commit['comment'];
214
215
        static $equals = [
216
            ''      => 1,
217
            'minor' => 1,
218
        ];
219
        if ($equals[$comment]) {
220
            return true;
221
        }
222
223
        static $starts = [
224
            'version bump',
225
            'bumped version',
226
            "merge branch 'master'",
227
        ];
228
        foreach ($starts as $start) {
229
            if (strtolower(substr($comment, 0, strlen($start))) === $start) {
230
                return true;
231
            }
232
        }
233
234
        return false;
235
    }
236
237
    public static function renderTag($tag)
238
    {
239
        if (strpos($tag, ' ') === false || $tag === static::getVcs()->initTag) {
240
            $tag .= static::renderDate(static::getVcs()->tags[$tag]);
241
        }
242
243
        return "\n## $tag\n\n";
244
    }
245
246
    public static function renderDate($date)
247
    {
248
        return $date ? date(' Y-m-d', strtotime($date)) : '';
249
    }
250
251
    public static function renderHeader($string)
252
    {
253
        $header = Yii::$app->config->package->fullName . ' ' . $string;
254
255
        return $header . "\n" . str_repeat('-', mb_strlen($header, Yii::$app->charset)) . "\n";
256
    }
257
258
    public static function getVcs()
259
    {
260
        return Yii::$app->config->vcs;
261
    }
262
}
263