KeepAChangelogRenderer::detectAction()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 9
nc 7
nop 1
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 30
rs 9.6111
c 2
b 1
f 0
1
<?php
2
/**
3
 * Changelog keeper
4
 *
5
 * @link      https://github.com/hiqdev/chkipper
6
 * @package   chkipper
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\lib\changelog;
12
13
use hiqdev\chkipper\lib\Note;
14
use hiqdev\chkipper\lib\Tag;
15
16
/**
17
 * Keep a Changelog renderer.
18
 *
19
 * [Keep a Changelog](http://keepachangelog.com/)
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class KeepAChangelogRenderer extends MarkdownRenderer
24
{
25
    public $defaultAction = 'changed';
26
27
    protected $normalization = Normalization::class;
28
29
    protected $actions;
30
31
    public function renderTag(Tag $tag)
32
    {
33
        $this->actions = [];
34
35
        return
36
            $this->renderTagHead($tag) .
37
            $this->renderObjects('scanNote', $tag->getNotes()) .
38
            $this->renderObjects('renderAction', $this->actions, false);
39
    }
40
41
    public function renderAction($notes, $action)
42
    {
43
        return
44
            $this->renderActionHead($action) .
45
            $this->renderObjects('renderNote', $notes, true) . "\n";
46
    }
47
48
    public function renderActionHead($action)
49
    {
50
        return '### ' . ucfirst($action) . "\n";
51
    }
52
53
    public function scanNote(Note $note)
54
    {
55
        $str = $this->renderNoteHead($note);
56
        if (!$str) {
57
            return;
58
        }
59
60
        $action = $this->detectAction($note);
61
        $this->actions[$action][] = $note;
62
63
        return null;
64
    }
65
66
    protected $words = [
67
        'removed'   => 'removed',
68
        'deleted'   => 'removed',
69
        'changed'   => 'changed',
70
        'added'     => 'added',
71
        'fixed'     => 'fixed',
72
73
        'remove'    => 'removed',
74
        'delete'    => 'removed',
75
        'change'    => 'changed',
76
        'add'       => 'added',
77
        'fix'       => 'fixed',
78
79
        'update'    => 'changed',
80
        'fixes'     => 'fixed',
81
    ];
82
83
    public function detectAction(Note $note)
84
    {
85
        $str = $note->getNote();
86
87
        $first = preg_split('/[^a-zA-Z]+/', $str, 2)[0];
88
        foreach ($this->words as $word) {
89
            if ($word === strtolower($first)) {
90
                return $word;
91
            }
92
        }
93
94
        foreach ($this->words as $word) {
95
            if (preg_match("/\b$word\b/", $str)) {
96
                return $word;
97
            }
98
        }
99
100
        return $this->defaultAction;
101
    }
102
}
103