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