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
|
|
|
public function __construct(bool $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
|
4 |
|
public function setSubjectBlacklist(array $list) |
68
|
|
|
{ |
69
|
4 |
|
$this->setBlacklist($list, 'subject'); |
70
|
4 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Blacklist setter. |
74
|
|
|
* |
75
|
|
|
* @param array $list |
76
|
|
|
* @param string $type |
77
|
|
|
*/ |
78
|
4 |
|
protected function setBlacklist(array $list, string $type) |
79
|
|
|
{ |
80
|
4 |
|
$this->blacklist[$type] = $list; |
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if the message contains blacklisted words. |
85
|
|
|
* |
86
|
|
|
* @param \SebastianFeldmann\Git\CommitMessage $msg |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
3 |
|
public function pass(CommitMessage $msg) : bool |
90
|
|
|
{ |
91
|
3 |
|
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
|
3 |
|
protected function isSubjectValid(CommitMessage $msg) : bool |
101
|
|
|
{ |
102
|
3 |
|
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
|
2 |
|
protected function isBodyValid(CommitMessage $msg) : bool |
112
|
|
|
{ |
113
|
2 |
|
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
|
3 |
|
protected function containsBlacklistedWord(array $list, string $content) : bool |
124
|
|
|
{ |
125
|
3 |
|
if (!$this->isCaseSensitive) { |
126
|
3 |
|
$content = strtolower($content); |
127
|
3 |
|
$list = array_map('strtolower', $list); |
128
|
|
|
} |
129
|
3 |
|
foreach ($list as $term) { |
130
|
3 |
|
if (strpos($content, $term) !== false) { |
131
|
1 |
|
$this->hint .= PHP_EOL . 'Invalid use of \'' . $term . '\''; |
132
|
1 |
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
2 |
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|