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\File\Action; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Config\Options; |
16
|
|
|
use CaptainHook\App\Console\IO; |
17
|
|
|
use CaptainHook\App\Exception\ActionFailed; |
18
|
|
|
use CaptainHook\App\Hook\Action; |
19
|
|
|
use Exception; |
20
|
|
|
use SebastianFeldmann\Git\Repository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class NotContainsRegex |
24
|
|
|
* |
25
|
|
|
* @package CaptainHook |
26
|
|
|
* @author Felix Edelmann <[email protected]> |
27
|
|
|
* @link https://github.com/captainhookphp/captainhook |
28
|
|
|
* @since Class available since Release 5.4.0 |
29
|
|
|
*/ |
30
|
|
|
class DoesNotContainRegex implements Action |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Executes the action |
34
|
|
|
* |
35
|
|
|
* @param \CaptainHook\App\Config $config |
36
|
|
|
* @param \CaptainHook\App\Console\IO $io |
37
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
38
|
|
|
* @param \CaptainHook\App\Config\Action $action |
39
|
|
|
* @return void |
40
|
|
|
* @throws \Exception |
41
|
|
|
*/ |
42
|
|
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void |
43
|
|
|
{ |
44
|
|
|
$options = $action->getOptions(); |
45
|
|
|
$regex = $options->get('regex'); |
46
|
|
|
if ($regex === null) { |
47
|
|
|
throw new Exception('Missing option "regex" for DoesNotContainRegex action'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$files = $this->getFiles($options, $repository); |
51
|
|
|
|
52
|
|
|
$failedFiles = 0; |
53
|
|
|
$totalMatchesCount = 0; |
54
|
|
|
foreach ($files as $file) { |
55
|
|
|
$fileContent = file_get_contents($file); |
56
|
|
|
$matchCount = preg_match_all($regex, $fileContent, $matches); |
57
|
|
|
|
58
|
|
|
if ($matchCount > 0) { |
59
|
|
|
$io->write('- <error>FAIL</error> ' . $file . ' - ' . $matchCount . ' matches', true); |
60
|
|
|
$failedFiles++; |
61
|
|
|
$totalMatchesCount += $matchCount; |
62
|
|
|
} else { |
63
|
|
|
$io->write('- <info>OK</info> ' . $file, true, IO::VERBOSE); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($failedFiles > 0) { |
68
|
|
|
$regexName = $options->get('regexName', $regex); |
69
|
|
|
throw new ActionFailed( |
70
|
|
|
'<error>Regex \'' . $regexName . '\' failed:</error> ' |
71
|
|
|
. $totalMatchesCount . ' matches in ' |
72
|
|
|
. $failedFiles . ' files' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$io->write('<info>No regex matches found</info>'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the files that need to be checked |
81
|
|
|
* |
82
|
|
|
* @param \CaptainHook\App\Config\Options $options |
83
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
84
|
|
|
* @return string[] |
85
|
|
|
*/ |
86
|
|
|
private function getFiles(Options $options, Repository $repository): array |
87
|
|
|
{ |
88
|
|
|
$index = $repository->getIndexOperator(); |
89
|
|
|
$fileExtensions = $this->getFileExtensions($options); |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
if (!empty($fileExtensions)) { |
93
|
|
|
$files = []; |
94
|
|
|
foreach ($fileExtensions as $ext) { |
95
|
|
|
$files = array_merge($files, $index->getStagedFilesOfType($ext)); |
96
|
|
|
} |
97
|
|
|
return $files; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $index->getStagedFiles(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the configured file extensions |
105
|
|
|
* |
106
|
|
|
* @param \CaptainHook\App\Config\Options $options |
107
|
|
|
* @return string[] |
108
|
|
|
*/ |
109
|
|
|
private function getFileExtensions(Options $options): array |
110
|
|
|
{ |
111
|
|
|
$fileExtensions = $options->get('fileExtensions'); |
112
|
|
|
|
113
|
|
|
if (!is_array($fileExtensions)) { |
|
|
|
|
114
|
|
|
return []; |
115
|
|
|
} |
116
|
|
|
return $fileExtensions; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|