Completed
Push — master ( a2db3f...116d3d )
by Andrii
03:06 queued 50s
created

CommitsHandler::hasHistory()   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-2016, 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 setHistory($value)
149
    {
150
        $this->_history = $value;
151
    }
152
153
    public function getHistory()
154
    {
155
        return $this->_history;
156
    }
157
158
    public function render($data)
159
    {
160
        $res = static::renderHeader('commits history');
161
162
        foreach (array_reverse(static::getVcs()->commits, true) as $hash => $commit) {
163
            if ($this->hasCommit($hash)) {
164
                continue;
165
            }
166
            $this->addHistory($commit, true);
167
        }
168
        if (!$this->hasHistory(static::getVcs()->initTag)) {
169
            $this->addHistory(['tag' => static::getVcs()->initTag]);
170
        }
171
172
        /// TODO
173
        /// $this->cleanupHistory();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
174
175
        foreach ($this->_history as $tag => $notes) {
176
            $prev = $res;
177
            $tag = static::arrayPop($notes, 'tag') ?: $tag;
178
            $new = static::arrayPop($notes, '') ?: [];
179
            $res .= static::renderTag($tag);
180
            $save = $res;
181
            foreach ($new as $hash => $lines) {
182
                $res .= static::renderLines($lines);
183
            }
184
            foreach ($notes as $note => $cs) {
185
                $note = static::arrayPop($cs, 'note');
186
                $res .= static::renderNote($note);
187
                foreach ($cs as $hash => $lines) {
188
                    $res .= static::renderLines($lines);
189
                }
190
            }
191
            /// TODO redo with cleanupHistory
192
            /// skip empty Under development section
193
            if ($save === $res && stripos($tag, static::getVcs()->lastTag) !== false) {
194
                $res = $prev;
195
                unset($this->_history[$tag]);
196
            }
197
        }
198
199
        return $res;
200
    }
201
202
    public static function arrayPop(&$array, $key)
203
    {
204
        $res = $array[$key];
205
        unset($array[$key]);
206
207
        return $res;
208
    }
209
210
    public static function renderNote($note)
211
    {
212
        return "- $note\n";
213
    }
214
215
    public static function renderLines(array $lines)
216
    {
217
        $res = implode("\n", array_filter($lines));
218
219
        return $res ? $res . "\n" : '';
220
    }
221
222
    public static function renderCommit($commit)
223
    {
224
        return static::skipCommit($commit) ? '' : "    - $commit[hash] $commit[date] $commit[comment] ($commit[email])";
225
    }
226
227
    public static function skipCommit($commit)
228
    {
229
        $comment = $commit['comment'];
230
231
        static $equals = [
232
            ''      => 1,
233
            'minor' => 1,
234
        ];
235
        if ($equals[$comment]) {
236
            return true;
237
        }
238
239
        static $starts = [
240
            'version bump',
241
            'bumped version',
242
            "merge branch 'master'",
243
        ];
244
        foreach ($starts as $start) {
245
            if (strtolower(substr($comment, 0, strlen($start))) === $start) {
246
                return true;
247
            }
248
        }
249
250
        return false;
251
    }
252
253
    public static function renderTag($tag)
254
    {
255
        if (strpos($tag, ' ') === false || $tag === static::getVcs()->initTag) {
256
            $tag .= static::renderDate(static::getVcs()->tags[$tag]);
257
        }
258
259
        return "\n## $tag\n\n";
260
    }
261
262
    public static function renderDate($date)
263
    {
264
        return $date ? date(' Y-m-d', strtotime($date)) : '';
265
    }
266
267
    public static function renderHeader($string)
268
    {
269
        $header = Yii::$app->config->package->fullName . ' ' . $string;
270
271
        return $header . "\n" . str_repeat('-', mb_strlen($header, Yii::$app->charset)) . "\n";
272
    }
273
274
    public static function getVcs()
275
    {
276
        return Yii::$app->get('config')->getVcs();
277
    }
278
}
279