forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·173 lines (135 loc) · 4.21 KB
/
pre-commit
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
#!/usr/bin/env php
<?php
use Symfony\Component\Process\PhpExecutableFinder;
/**
* This script fixes code style for files which are going to be committed.
* If errors were found commit will be interrupted and errors will be fixed.
* Then you need to manually stage your changes and run commit again
*
* To install this hook just run "pre-commit -i"
*/
include_once __DIR__.'/../vendor/autoload.php';
$projectRootDir = realpath(__DIR__ . '/..');
if (!$projectRootDir) {
throw new \LogicException('The project root dir was not guessed');
}
$phpExecutableFinder = new PhpExecutableFinder();
$phpBin = $phpExecutableFinder->find();
if (!$phpBin) {
echo 'Php executable could not be found.'.PHP_EOL;
}
$options = getopt('i', array('install'));
if (isset($options['i']) || isset($options['install'])) {
install();
echo "Git hook successfully installed" . PHP_EOL;
exit(1);
}
$options = getopt('u', array('uninstall'));
if (isset($options['u']) || isset($options['uninstall'])) {
uninstall();
echo "Git hook successfully uninstalled" . PHP_EOL;
exit(1);
}
function install()
{
global $projectRootDir;
uninstall();
//create link
$link = $projectRootDir . '/.git/hooks/pre-commit';
exec(sprintf('ln -s %s %s', __FILE__, $link));
}
function uninstall()
{
global $projectRootDir;
//create link
$link = $projectRootDir . '/.git/hooks/pre-commit';
@unlink($link);
}
function getFilesToFix()
{
$files = array();
exec('git diff-index --name-only --cached --diff-filter=ACMR HEAD --', $files);
$stagedFiles = array_filter($files, function ($file) {
return (bool) preg_match('/\.(php|twig|translations\/*.yml)$/', $file);
});
$stagedFiles = array_filter($stagedFiles, function ($file) {
return (bool) preg_match('/^pkg\//', $file);
});
return $stagedFiles;
}
function runPhpLint()
{
global $phpBin, $projectRootDir;
$output = [];
foreach (getFilesToFix() as $file) {
$commandOutput = null;
$returnCode = null;
exec(sprintf('%s -l %s', $phpBin, $projectRootDir.'/'.$file), $commandOutput, $returnCode);
if ($returnCode) {
$output[] = $commandOutput;
}
}
return $output;
}
function runPhpCsFixer()
{
global $phpBin, $projectRootDir;
$phpCsFixerBin = $projectRootDir . '/bin/php-cs-fixer';
if (!file_exists($phpCsFixerBin)) {
echo $phpCsFixerBin.' File could not be found. Make sure you run command from project\'s root directory'.PHP_EOL;
}
$filesWithErrors = array();
$output = '';
$returnCode = null;
exec(sprintf(
'%s %s fix --config=.php_cs.php --dry-run --no-interaction --path-mode=intersection -- %s',
$phpBin,
$phpCsFixerBin,
implode(' ', getFilesToFix())
), $output, $returnCode);
if ($returnCode) {
exec(sprintf(
'%s %s fix --config=.php_cs.php --no-interaction -v --path-mode=intersection -- %s',
$phpBin,
$phpCsFixerBin,
implode(' ', getFilesToFix())
), $output);
}
return $returnCode;
}
function runPhpstan()
{
$output = '';
$returnCode = null;
exec(sprintf(
'docker run --workdir="/mqdev" -v "`pwd`:/mqdev" --rm enqueue/dev:latest php -d memory_limit=1024M bin/phpstan analyse -l 1 -c phpstan.neon %s',
implode(' ', getFilesToFix())
), $output, $returnCode);
return $returnCode ? $output : false;
}
echo sprintf('Found %s staged files', count(getFilesToFix())).PHP_EOL;
$phpSyntaxErrors = runPhpLint();
if ($phpSyntaxErrors) {
echo "Php syntax errors were found in next files:" . PHP_EOL;
foreach ($phpSyntaxErrors as $phpSyntaxErrors) {
echo array_walk_recursive($phpSyntaxErrors, function($item) {
echo $item.PHP_EOL;
}) . PHP_EOL;
}
exit(1);
}
$phpCSFixed = runPhpCsFixer();
if ($phpCSFixed) {
echo "Incorrect coding standards were detected and fixed." . PHP_EOL;
echo "Please stash changes and run commit again." . PHP_EOL;
exit(1);
}
$phpStanErrors = runPhpstan();
if ($phpStanErrors) {
echo array_walk_recursive($phpStanErrors, function($item) {
echo $item.PHP_EOL;
}) . PHP_EOL;
echo PHP_EOL;
exit(1);
}
exit(0);