Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit c745523

Browse files
committed
Merge branch 'fprochazka-feature/files-can-be-skipped'
Conflicts: src/Manager.php
2 parents eee71ae + 7e0f8d4 commit c745523

File tree

11 files changed

+237
-55
lines changed

11 files changed

+237
-55
lines changed

bin/skip-linting.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
array_shift($_SERVER['argv']);
3+
$files = $_SERVER['argv'];
4+
5+
foreach ($files as $file) {
6+
$skip = false;
7+
$f = fopen($file, 'r');
8+
$firstLine = fgets($f);
9+
@fclose($f);
10+
11+
if (!preg_match('~<?php\\s*\\/\\/\s*lint\s*([^\d\s]+)\s*([^\s]+)\s*~i', $firstLine, $m)) {
12+
$skip = false;
13+
}
14+
15+
$skip = isset($m[2]) && version_compare(PHP_VERSION, $m[2], $m[1]);
16+
17+
echo "$file;" . ($skip ? '1' : '0') . "\n";
18+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"php": ">=5.3.3"
88
},
99
"require-dev": {
10-
"nette/tester": "v1.1.0"
10+
"nette/tester": "v1.1.0",
11+
"jakub-onderka/php-console-highlighter": "@dev"
1112
},
1213
"suggest": {
1314
"jakub-onderka/php-console-highlighter": "Highlight syntax in code snippet"

src/Manager.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public function run(Settings $settings = null)
7474
$parallelLint->setProcessCallback(function ($status, $file) use ($output) {
7575
if ($status === ParallelLint::STATUS_OK) {
7676
$output->ok();
77-
} else if ($status === ParallelLint::STATUS_ERROR) {
77+
} elseif ($status === ParallelLint::STATUS_SKIP) {
78+
$output->skip();
79+
} elseif ($status === ParallelLint::STATUS_ERROR) {
7880
$output->error();
7981
} else {
8082
$output->fail();
@@ -87,6 +89,13 @@ public function run(Settings $settings = null)
8789

8890
$testTime = round($result->getTestTime(), 1);
8991
$message = "Checked {$result->getCheckedFiles()} files in $testTime second, ";
92+
93+
if ($result->getSkippedFiles() > 0) {
94+
$message .= "skipped {$result->getSkippedFiles()} ";
95+
$message .= ($result->getSkippedFiles() === 1 ? 'file' : 'files');
96+
$message .= ", ";
97+
}
98+
9099
if (!$result->hasSyntaxError()) {
91100
$message .= "no syntax error found";
92101
} else {
@@ -169,7 +178,7 @@ protected function getFilesFromPaths(array $paths, array $extensions, array $exc
169178
foreach ($paths as $path) {
170179
if (is_file($path)) {
171180
$files[] = $path;
172-
} else if (is_dir($path)) {
181+
} elseif (is_dir($path)) {
173182
$iterator = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS);
174183
if (!empty($excluded)) {
175184
$iterator = new RecursiveDirectoryFilterIterator($iterator, $excluded);

src/Output.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
class Output
3434
{
3535
const TYPE_DEFAULT = 'default',
36+
TYPE_SKIP = 'skip',
3637
TYPE_ERROR = 'error',
3738
TYPE_OK = 'ok';
3839

@@ -62,6 +63,12 @@ public function ok()
6263
$this->progress();
6364
}
6465

66+
public function skip()
67+
{
68+
$this->write('S', self::TYPE_SKIP);
69+
$this->progress();
70+
}
71+
6572
public function error()
6673
{
6774
$this->write('X', self::TYPE_ERROR);
@@ -160,6 +167,10 @@ public function write($string, $type = self::TYPE_DEFAULT)
160167
parent::write($this->colors->apply('bg_green', $string));
161168
break;
162169

170+
case self::TYPE_SKIP:
171+
parent::write($this->colors->apply('bg_yellow', $string));
172+
break;
173+
163174
case self::TYPE_ERROR:
164175
parent::write($this->colors->apply('bg_red', $string));
165176
break;

src/ParallelLint.php

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class Result
4141
/** @var int */
4242
private $filesWithSyntaxError;
4343

44+
/** @var int */
45+
private $skippedFiles;
46+
4447
/** @var float */
4548
private $testTime;
4649

@@ -50,11 +53,12 @@ class Result
5053
* @param int $filesWithSyntaxError
5154
* @param float $testTime
5255
*/
53-
public function __construct(array $errors, $checkedFiles, $filesWithSyntaxError, $testTime)
56+
public function __construct(array $errors, $checkedFiles, $filesWithSyntaxError, $skippedFiles, $testTime)
5457
{
5558
$this->errors = $errors;
5659
$this->checkedFiles = $checkedFiles;
5760
$this->filesWithSyntaxError = $filesWithSyntaxError;
61+
$this->skippedFiles = $skippedFiles;
5862
$this->testTime = $testTime;
5963
}
6064

@@ -82,6 +86,14 @@ public function getCheckedFiles()
8286
return $this->checkedFiles;
8387
}
8488

89+
/**
90+
* @return int
91+
*/
92+
public function getSkippedFiles()
93+
{
94+
return $this->skippedFiles;
95+
}
96+
8597
/**
8698
* @return int
8799
*/
@@ -110,6 +122,7 @@ public function getTestTime()
110122
class ParallelLint
111123
{
112124
const STATUS_OK = 'ok',
125+
STATUS_SKIP = 'skip',
113126
STATUS_FAIL = 'fail',
114127
STATUS_ERROR = 'error';
115128

@@ -140,34 +153,50 @@ public function __construct($phpExecutable, $parallelJobs = 10)
140153
*/
141154
public function lint(array $files)
142155
{
143-
$processCallback = is_callable($this->processCallback) ? $this->processCallback : function() {};
156+
$startTime = microtime(true);
144157

145-
/** @var LintProcess[] $running */
146-
$running = array();
147-
$errors = array();
148-
$checkedFiles = 0;
149-
$filesWithSyntaxError = 0;
158+
$skipLintProcess = new SkipLintProcess($this->phpExecutable, $files);
150159

151-
$startTime = microtime(true);
160+
$processCallback = is_callable($this->processCallback) ? $this->processCallback : function() {};
161+
162+
/**
163+
* @var LintProcess[] $running
164+
* @var LintProcess[] $waiting
165+
*/
166+
$errors = $running = $waiting = array();
167+
$skippedFiles = $checkedFiles = $filesWithSyntaxError = 0;
152168

153169
while ($files || $running) {
154170
for ($i = count($running); $files && $i < $this->parallelJobs; $i++) {
155171
$file = array_shift($files);
156-
$running[$file] = new LintProcess(
157-
$this->phpExecutable,
158-
$file,
159-
$this->aspTagsEnabled,
160-
$this->shortTagEnabled
161-
);
172+
173+
if ($skipLintProcess->isSkipped($file) === true) {
174+
$skippedFiles++;
175+
$processCallback(self::STATUS_SKIP, $file);
176+
} else {
177+
$running[$file] = new LintProcess(
178+
$this->phpExecutable,
179+
$file,
180+
$this->aspTagsEnabled,
181+
$this->shortTagEnabled
182+
);
183+
}
162184
}
163185

186+
$skipLintProcess->getChunk();
164187
usleep(100);
165188

166189
foreach ($running as $file => $process) {
167190
if ($process->isFinished()) {
168191
unset($running[$file]);
169192

170-
if ($process->isFail()) {
193+
$skipStatus = $skipLintProcess->isSkipped($file);
194+
if ($skipStatus === null) {
195+
$waiting[$file] = $process;
196+
} elseif ($skipStatus === true) {
197+
$skippedFiles++;
198+
$processCallback(self::STATUS_SKIP, $file);
199+
} elseif ($process->isFail()) {
171200
$errors[] = new Error($file, $process->getErrorOutput());
172201
$processCallback(self::STATUS_FAIL, $file);
173202
} else {
@@ -184,9 +213,37 @@ public function lint(array $files)
184213
}
185214
}
186215

216+
if (!empty($waiting)) {
217+
while (!$skipLintProcess->isFinished()) {
218+
usleep(100);
219+
}
220+
221+
foreach ($waiting as $file => $process) {
222+
$skipStatus = $skipLintProcess->isSkipped($file);
223+
if ($skipStatus === null) {
224+
throw new \Exception("File $file has empty skip status. Please contact PHP Parallel Lint author.");
225+
} elseif ($skipStatus === true) {
226+
$skippedFiles++;
227+
$processCallback(self::STATUS_SKIP, $file);
228+
} elseif ($process->isFail()) {
229+
$errors[] = new Error($file, $process->getErrorOutput());
230+
$processCallback(self::STATUS_FAIL, $file);
231+
} else {
232+
$checkedFiles++;
233+
if ($process->hasSyntaxError()) {
234+
$errors[] = new SyntaxError($file, $process->getSyntaxError());
235+
$filesWithSyntaxError++;
236+
$processCallback(self::STATUS_ERROR, $file);
237+
} else {
238+
$processCallback(self::STATUS_OK, $file);
239+
}
240+
}
241+
}
242+
}
243+
187244
$testTime = microtime(true) - $startTime;
188245

189-
return new Result($errors, $checkedFiles, $filesWithSyntaxError, $testTime);
246+
return new Result($errors, $checkedFiles, $filesWithSyntaxError, $skippedFiles, $testTime);
190247
}
191248

192249
/**
@@ -283,4 +340,4 @@ public function setShortTagEnabled($shortTagEnabled)
283340

284341
return $this;
285342
}
286-
}
343+
}

0 commit comments

Comments
 (0)