Repository navigation
Expand file tree
/
Copy pathCheck.php
More file actions
233 lines (206 loc) · 6.03 KB
/
Copy pathCheck.php
File metadata and controls
233 lines (206 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* PHP Composter Regular Expression Check.
*
* @package PHPComposter\RegularExpression
* @author Alain Schlesser <alain.schlesser@gmail.com>
* @license MIT
* @link https://www.brightnucleus.com/
* @copyright 2017 Alain Schlesser, Bright Nucleus
*/
namespace PHPComposter\RegularExpression;
use PHPComposter\PHPComposter\BaseAction;
use PHPComposter\PHPComposter\Hook;
use PHPComposter\PHPComposter\Paths;
/**
* Class Check.
*
* @since 0.1.0
*
* @package PHPComposter\RegularExpression
* @author Alain Schlesser <alain.schlesser@gmail.com>
*/
final class Check extends BaseAction
{
const KEY_CONFIGURATION = 'php-composter-regular-expression';
const KEY_COMMIT_MESSAGE = 'commit-message';
const ELEMENT_SUBJECT = 'subject';
const ELEMENT_BODY = 'body';
const HOOK_HANDLERS = [
Hook::COMMIT_MSG => 'commitMessage',
];
const CONTENT_RETRIEVERS = [
self::ELEMENT_SUBJECT => 'getSubject',
self::ELEMENT_BODY => 'getBody',
];
const FILTER_DISPLAY_LENGTH = 40;
/**
* Dispatch the regular expression checks.
*
* @since 0.1.0
*
* @param array ...$args Array of arguments.
*/
public function dispatch(...$args)
{
$config = $this->getExtraKey(self::KEY_CONFIGURATION);
$check = self::HOOK_HANDLERS[$this->hook];
$this->$check($config, ...$args);
}
/**
* Check a commit message.
*
* @since 0.1.0
*
* @param array $config Configuration array.
*/
protected function commitMessage(array $config, $messageFile)
{
$messageFile = Paths::getPath('pwd') . $messageFile;
if (! is_readable($messageFile)
|| false === $message = file_get_contents($messageFile)) {
$this->error("Commit message is not readable from file \"{$messageFile}\"", 1);
}
$checks = array_key_exists(self::KEY_COMMIT_MESSAGE, $config)
? $config[self::KEY_COMMIT_MESSAGE]
: [];
foreach ($checks as $element => $rules) {
$contentRetriever = self::CONTENT_RETRIEVERS[$element];
$content = $this->$contentRetriever($message);
$this->checkRules($element, $content, $rules);
}
}
/**
* Get the subject line of the commit message.
*
* @since 0.1.0
*
* @param string $message Commit message.
*
* @return string
*/
protected function getSubject($message)
{
$message = $this->stripComments($message);
return strstr($message, "\n", true);
}
/**
* Get the body of the commit message.
*
* @since 0.1.0
*
* @param string $message Commit message.
*
* @return string
*/
protected function getBody($message)
{
$message = $this->stripComments($message);
return preg_replace('/^.+\n/', '', $message);
}
/**
* Strip the comments of a commit message.
*
* @since 0.1.0
*
* @param string $message Message to the strip the comments from.
*
* @return string Commit message without comments.
*/
protected function stripComments($message)
{
return preg_replace('/^\#.*$\n?/m', '', $message);
}
/**
* Check the rules against a given piece of content.
*
* @since 0.1.0
*
* @param string $element Element that is being checked.
* @param string $content Content to check the rules against.
* @param array $rules Rules to check.
*/
protected function checkRules($element, $content, $rules)
{
foreach ($rules as $condition => $filter) {
$matches = [];
$filter = chr(1) . $filter . chr(1);
$condition = $this->normalizeCondition($condition);
$result = preg_match($filter, $content, $matches);
$passed = (bool)$this->$condition($result, $matches);
$filterDisplay = $this->truncateFilter($filter);
$message = "<comment>{$element}</comment> \"{$condition}\" \"<comment>{$filterDisplay}</comment>\".";
if (! $passed) {
$this->error(
"Failed to verify that {$message}",
1
);
}
$this->success(
$message,
false
);
}
}
/**
* Truncate the filter for display.
*
* @since 0.1.0
*
* @param string $filter Filter to truncate.
*
* @return string Truncated filter.
*/
protected function truncateFilter($filter)
{
if (strlen($filter) < static::FILTER_DISPLAY_LENGTH) {
return $filter;
}
return substr($filter, 0, static::FILTER_DISPLAY_LENGTH - 3) . '...';
}
/**
* Normalizes the condition to be used as a method name.
*
* @since 0.1.0
*
* @param string $condition Condition to normalize.
*
* @return string Normalized condition that can be used as a method name.
*/
protected function normalizeCondition($condition)
{
return str_replace('-', '_', $condition);
}
/**
* Whether the regular expression has a match.
*
* This method can be used as a condition in the `composer.json` file.
*
* @since 0.1.0
*
* @param mixed $result Result of the preg_match call.
* @param array $matches Array of found matches.
*
* @return bool Whether the regular expression has a match.
*/
protected function has($result, $matches)
{
return $result === 1;
}
/**
* Whether the regular expression does not have a match.
*
* This method can be used as a condition in the `composer.json` file.
*
* @since 0.1.0
*
* @param mixed $result Result of the preg_match call.
* @param array $matches Array of found matches.
*
* @return bool Whether the regular expression does not have a match.
*/
protected function has_not($result, $matches)
{
return $result !== 1;
}
}