Completed
Push — master ( a50ce0...3b5091 )
by Sebastian
02:40
created

Blacklist   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 116
ccs 21
cts 21
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setBodyBlacklist() 0 4 1
A setSubjectBlacklist() 0 4 1
A setBlacklist() 0 4 1
A pass() 0 4 2
A isSubjectValid() 0 4 1
A isBodyValid() 0 4 1
A containsBlacklistedWord() 0 14 4
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace sebastianfeldmann\CaptainHook\Hook\Message\Rule;
11
12
use sebastianfeldmann\CaptainHook\Git\CommitMessage;
13
14
/**
15
 * Class UseImperativeMood
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/captainhook
20
 * @since   Class available since Release 0.9.0
21
 */
22
class Blacklist extends Base
23
{
24
    /**
25
     * Case sensitivity
26
     *
27
     * @var bool
28
     */
29
    protected $isCaseSensitive;
30
31
    /**
32
     * Blacklisted words
33
     *
34
     * @var array
35
     */
36
    protected $blacklist = [
37
        'subject' => [],
38
        'body'    => [],
39
    ];
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param bool $caseSensitive
45
     */
46
    public function __construct($caseSensitive = false)
47
    {
48
        $this->isCaseSensitive = $caseSensitive;
49
        $this->hint            = 'Commit message should not contain blacklisted words';
50
    }
51
52
    /**
53
     * Set body blacklist.
54
     *
55
     * @param array $list
56
     */
57
    public function setBodyBlacklist(array $list)
58
    {
59
        $this->setBlacklist($list, 'body');
60
    }
61
62
    /**
63
     * Set subject blacklist.
64
     *
65
     * @param array $list
66
     */
67 5
    public function setSubjectBlacklist(array $list)
68
    {
69 5
        $this->setBlacklist($list, 'subject');
70 5
    }
71
72
    /**
73
     * Blacklist setter.
74
     *
75
     * @param array  $list
76
     * @param string $type
77
     */
78 5
    protected function setBlacklist(array $list, $type)
79
    {
80 5
        $this->blacklist[$type] = $list;
81 5
    }
82
83
    /**
84
     * Check if the message contains blacklisted words.
85
     *
86
     * @param  \sebastianfeldmann\CaptainHook\Git\CommitMessage $msg
87
     * @return bool
88
     */
89 4
    public function pass(CommitMessage $msg)
90
    {
91 4
        return $this->isSubjectValid($msg) && $this->isBodyValid($msg);
92
    }
93
94
    /**
95
     * Check commit message subject for blacklisted words.
96
     *
97
     * @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $msg
98
     * @return bool
99
     */
100 4
    protected function isSubjectValid(CommitMessage $msg)
101
    {
102 4
        return !$this->containsBlacklistedWord($this->blacklist['subject'], $msg->getSubject());
103
    }
104
105
    /**
106
     * Check commit message body for blacklisted words.
107
     *
108
     * @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $msg
109
     * @return bool
110
     */
111 3
    protected function isBodyValid(CommitMessage $msg)
112
    {
113 3
        return !$this->containsBlacklistedWord($this->blacklist['subject'], $msg->getBody());
114
    }
115
116
    /**
117
     * Contains blacklisted word.
118
     *
119
     * @param  array  $list
120
     * @param  string $content
121
     * @return bool
122
     */
123 4
    protected function containsBlacklistedWord(array $list, $content)
124
    {
125 4
        if (!$this->isCaseSensitive) {
126 4
            $content = strtolower($content);
127 4
            $list    = array_map('strtolower', $list);
128
        }
129 4
        foreach ($list as $term) {
130 4
            if (strpos($content, $term) !== false) {
131 1
                $this->hint .= PHP_EOL . 'Invalid use of \'' . $term . '\'';
132 4
                return true;
133
            }
134
        }
135 3
        return false;
136
    }
137
}
138