Permalink
Browse files

tasks: added phpSyntaxChecker

  • Loading branch information...
dg committed Mar 26, 2018
1 parent 4f7a7d0 commit 55c057d35fbd73e02bdb69c0fd292bc6b27637c7
Showing with 53 additions and 0 deletions.
  1. +24 −0 src/Tasks.php
  2. +1 −0 src/bootstrap.php
  3. +28 −0 tests/Tasks.phpSyntaxChecker.phpt
@@ -135,6 +135,30 @@ public static function trailingPhpTagRemover(&$contents, Result $result)
}
public static function phpSyntaxChecker($contents, Result $result)
{
$php = defined('PHP_BINARY') ? PHP_BINARY : 'php';
$stdin = tmpfile();
fwrite($stdin, $contents);
fseek($stdin, 0);
$process = proc_open(
$php . ' -l',
[$stdin, ['pipe', 'w'], ['pipe', 'w']],
$pipes,
null, null, ['bypass_shell' => true]
);
if (!is_resource($process)) {
$result->warning('Unable to lint generated template');
return;
}
$error = stream_get_contents($pipes[1]);
if (proc_close($process)) {
$error = strip_tags(explode("\n", $error)[1]);
$result->error('Invalid PHP code: ' . $error);
}
}
public static function latteSyntaxChecker($contents, Result $result)
{
try {
@@ -72,6 +72,7 @@
$checker->addTask([$tasks, 'controlCharactersChecker']);
$checker->addTask([$tasks, 'bomFixer']);
$checker->addTask([$tasks, 'utf8Checker']);
$checker->addTask([$tasks, 'phpSyntaxChecker'], '*.php,*.phpt');
$checker->addTask([$tasks, 'invalidPhpDocChecker'], '*.php,*.phpt');
if (isset($options['--short-arrays'])) {
@@ -0,0 +1,28 @@
<?php
use Nette\CodeChecker\Result;
use Nette\CodeChecker\Tasks;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
test(function () {
$result = new Result;
Tasks::phpSyntaxChecker('', $result);
Assert::same([], $result->getMessages());
});
test(function () {
$result = new Result;
Tasks::phpSyntaxChecker('<?php echo 1;', $result);
Assert::same([], $result->getMessages());
});
test(function () {
$result = new Result;
Tasks::phpSyntaxChecker('<?php if', $result);
Assert::count(1, $result->getMessages());
Assert::same(Result::ERROR, $result->getMessages()[0][0]);
Assert::contains('syntax error, unexpected end of file', $result->getMessages()[0][1]);
});

0 comments on commit 55c057d

Please sign in to comment.