-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·182 lines (140 loc) · 4.99 KB
/
pre-commit
File metadata and controls
executable file
·182 lines (140 loc) · 4.99 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
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Application;
class CodeQualityTool extends Application
{
private $output;
private $input;
const PHP_FILES = '/(\.php)$/';
const PHP_FILES_IN_SRC = '/^src\/(.*)(\.php)$/';
const PHP_FILES_IN_CLASSES = '/^classes\/(.*)(\.php)$/';
public function __construct()
{
parent::__construct('Code Quality Tool', '1.0.0');
}
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$output->writeln('<fg=white;options=bold;bg=red>Intaro Code Quality Tool</fg=white;options=bold;bg=red>');
$output->writeln('<info>Fetching files</info>');
$files = $this->extractCommitedFiles();
$output->writeln('<info>Check composer</info>');
//$this->checkComposer($files);
$output->writeln('<info>Running PHPLint</info>');
if (!$this->phpLint($files)) {
throw new Exception('There are some PHP syntax errors!');
}
$output->writeln('<info>Checking code style</info>');
if (!$this->codeStyle($files)) {
throw new Exception(sprintf('There are coding standards violations!'));
}
$output->writeln('<info>Checking code style with PHPCS</info>');
if (!$this->codeStylePsr($files)) {
throw new Exception(sprintf('There are PHPCS coding standards violations!'));
}
$output->writeln('<info>Good job dude!</info>');
}
private function checkComposer($files)
{
$composerJsonDetected = false;
$composerLockDetected = false;
foreach ($files as $file) {
if ($file === 'composer.json') {
$composerJsonDetected = true;
}
if ($file === 'composer.lock') {
$composerLockDetected = true;
}
}
if ($composerJsonDetected && !$composerLockDetected) {
throw new Exception('composer.lock must be commited if composer.json is modified!');
}
}
private function extractCommitedFiles()
{
$output = array();
$rc = 0;
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $rc);
$against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
if ($rc == 0) {
$against = 'HEAD';
}
exec("git diff-index --cached --name-status $against | egrep '^(A|M)' | awk '{print $2;}'", $output);
return $output;
}
private function phpLint($files)
{
$needle = '/(\.php)|(\.inc)$/';
$succeed = true;
foreach ($files as $file) {
if (!preg_match($needle, $file)) {
continue;
}
$process = new Process(array('php', '-l', $file));
$process->run();
if (!$process->isSuccessful()) {
$this->output->writeln($file);
$this->output->writeln(sprintf('<error>%s</error>', trim($process->getErrorOutput())));
if ($succeed) {
$succeed = false;
}
}
}
return $succeed;
}
private function codeStyle(array $files)
{
$succeed = true;
foreach ($files as $file) {
$phpFile = preg_match(self::PHP_FILES, $file);
if (!$phpFile) {
continue;
}
$phpCsFixer = new Process(array(
'php',
__DIR__ . '/vendor/bin/php-cs-fixer',
'--dry-run',
'--verbose',
'--using-cache=no',
'fix',
$file,
));
$phpCsFixer->setWorkingDirectory(getcwd());
$phpCsFixer->run();
if (!$phpCsFixer->isSuccessful()) {
$this->output->writeln(sprintf('<error>%s</error>', trim($phpCsFixer->getOutput())));
if ($succeed) {
$succeed = false;
}
}
}
return $succeed;
}
private function codeStylePsr(array $files)
{
$succeed = true;
$needle = self::PHP_FILES_IN_SRC;
foreach ($files as $file) {
if (!preg_match($needle, $file)) {
continue;
}
$phpCsFixer = new Process(array('php', __DIR__ . '/vendor/bin/phpcs', '--encoding=utf-8', '--standard=PSR2', $file));
$phpCsFixer->setWorkingDirectory(getcwd());
$phpCsFixer->run();
if (!$phpCsFixer->isSuccessful()) {
$this->output->writeln(sprintf('<error>%s</error>', trim($phpCsFixer->getOutput())));
if ($succeed) {
$succeed = false;
}
}
}
return $succeed;
}
}
$console = new CodeQualityTool();
$console->run();