Completed
Push — master ( 7efae6...e19db8 )
by Sebastian
03:17
created

Blacklist::setSubjectBlacklist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\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 2
    public function __construct(bool $caseSensitive = false)
47
    {
48 2
        $this->isCaseSensitive = $caseSensitive;
49 2
        $this->hint            = 'Commit message should not contain blacklisted words';
50 2
    }
51
52
    /**
53
     * Set body blacklist.
54
     *
55
     * @param array $list
56
     */
57 2
    public function setBodyBlacklist(array $list)
58
    {
59 2
        $this->setBlacklist($list, 'body');
60 2
    }
61
62
    /**
63
     * Set subject blacklist.
64
     *
65
     * @param array $list
66
     */
67 6
    public function setSubjectBlacklist(array $list)
68
    {
69 6
        $this->setBlacklist($list, 'subject');
70 6
    }
71
72
    /**
73
     * Blacklist setter.
74
     *
75
     * @param array  $list
76
     * @param string $type
77
     */
78 6
    protected function setBlacklist(array $list, string $type)
79
    {
80 6
        $this->blacklist[$type] = $list;
81 6
    }
82
83
    /**
84
     * Check if the message contains blacklisted words.
85
     *
86
     * @param  \SebastianFeldmann\Git\CommitMessage $msg
87
     * @return bool
88
     */
89 5
    public function pass(CommitMessage $msg) : bool
90
    {
91 5
        return $this->isSubjectValid($msg) && $this->isBodyValid($msg);
92
    }
93
94
    /**
95
     * Check commit message subject for blacklisted words.
96
     *
97
     * @param \SebastianFeldmann\Git\CommitMessage $msg
98
     * @return bool
99
     */
100 5
    protected function isSubjectValid(CommitMessage $msg) : bool
101
    {
102 5
        return !$this->containsBlacklistedWord($this->blacklist['subject'], $msg->getSubject());
103
    }
104
105
    /**
106
     * Check commit message body for blacklisted words.
107
     *
108
     * @param \SebastianFeldmann\Git\CommitMessage $msg
109
     * @return bool
110
     */
111 4
    protected function isBodyValid(CommitMessage $msg) : bool
112
    {
113 4
        return !$this->containsBlacklistedWord($this->blacklist['body'], $msg->getBody());
114
    }
115
116
    /**
117
     * Contains blacklisted word.
118
     *
119
     * @param  array  $list
120
     * @param  string $content
121
     * @return bool
122
     */
123 5
    protected function containsBlacklistedWord(array $list, string $content) : bool
124
    {
125 5
        if (!$this->isCaseSensitive) {
126 4
            $content = strtolower($content);
127 4
            $list    = array_map('strtolower', $list);
128
        }
129 5
        foreach ($list as $term) {
130 5
            if (strpos($content, $term) !== false) {
131 2
                $this->hint .= PHP_EOL . 'Invalid use of \'' . $term . '\'';
132 5
                return true;
133
            }
134
        }
135 4
        return false;
136
    }
137
}
138