Completed
Push — master ( 5bb4f6...c375b5 )
by Andrii
03:37
created

KeepAChangelogRenderer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 80
ccs 0
cts 58
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A renderTag() 0 10 1
A renderAction() 0 7 1
A renderActionHead() 0 4 1
A scanNote() 0 12 2
B detectAction() 0 19 5
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
    protected $normalization = Normalization::class;
26
27
    protected $actions;
28
29
    public function renderTag(Tag $tag)
30
    {
31
        $this->actions = [];
32
33
        return
34
            $this->renderTagHead($tag) .
35
            $this->renderObjects('scanNote', $tag->getNotes()) .
36
            $this->renderObjects('renderAction', $this->actions, false)
37
        ;
38
    }
39
40
    public function renderAction($notes, $action)
41
    {
42
        return 
43
            $this->renderActionHead($action) .
44
            $this->renderObjects('renderNote', $notes, true) . "\n"
45
        ;
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("/[\s,]+/", $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 'changed';
101
    }
102
}
103