Skip to content

Commit 78d9985

Browse files
cyrus-andnikic
authored andcommitted
Print messages to stderr in bin/php-parse and fix exit status
Close nikic#605.
1 parent 57b8673 commit 78d9985

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

bin/php-parse

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,23 @@ $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
4545
foreach ($files as $file) {
4646
if (strpos($file, '<?php') === 0) {
4747
$code = $file;
48-
echo "====> Code $code\n";
48+
fwrite(STDERR, "====> Code $code\n");
4949
} else {
5050
if (!file_exists($file)) {
51-
die("File $file does not exist.\n");
51+
fwrite(STDERR, "File $file does not exist.\n");
52+
exit(1);
5253
}
5354

5455
$code = file_get_contents($file);
55-
echo "====> File $file:\n";
56+
fwrite(STDERR, "====> File $file:\n");
5657
}
5758

5859
if ($attributes['with-recovery']) {
5960
$errorHandler = new PhpParser\ErrorHandler\Collecting;
6061
$stmts = $parser->parse($code, $errorHandler);
6162
foreach ($errorHandler->getErrors() as $error) {
6263
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
63-
echo $message . "\n";
64+
fwrite(STDERR, $message . "\n");
6465
}
6566
if (null === $stmts) {
6667
continue;
@@ -70,25 +71,26 @@ foreach ($files as $file) {
7071
$stmts = $parser->parse($code);
7172
} catch (PhpParser\Error $error) {
7273
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
73-
die($message . "\n");
74+
fwrite(STDERR, $message . "\n");
75+
exit(1);
7476
}
7577
}
7678

7779
foreach ($operations as $operation) {
7880
if ('dump' === $operation) {
79-
echo "==> Node dump:\n";
81+
fwrite(STDERR, "==> Node dump:\n");
8082
echo $dumper->dump($stmts, $code), "\n";
8183
} elseif ('pretty-print' === $operation) {
82-
echo "==> Pretty print:\n";
84+
fwrite(STDERR, "==> Pretty print:\n");
8385
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
8486
} elseif ('json-dump' === $operation) {
85-
echo "==> JSON dump:\n";
87+
fwrite(STDERR, "==> JSON dump:\n");
8688
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
8789
} elseif ('var-dump' === $operation) {
88-
echo "==> var_dump():\n";
90+
fwrite(STDERR, "==> var_dump():\n");
8991
var_dump($stmts);
9092
} elseif ('resolve-names' === $operation) {
91-
echo "==> Resolved names.\n";
93+
fwrite(STDERR, "==> Resolved names.\n");
9294
$stmts = $traverser->traverse($stmts);
9395
}
9496
}
@@ -104,9 +106,9 @@ function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
104106

105107
function showHelp($error = '') {
106108
if ($error) {
107-
echo $error . "\n\n";
109+
fwrite(STDERR, $error . "\n\n");
108110
}
109-
die(<<<OUTPUT
111+
fwrite($error ? STDERR : STDOUT, <<<OUTPUT
110112
Usage: php-parse [operations] file1.php [file2.php ...]
111113
or: php-parse [operations] "<?php code"
112114
Turn PHP source code into an abstract syntax tree.
@@ -131,6 +133,7 @@ Example:
131133
132134
OUTPUT
133135
);
136+
exit($error ? 1 : 0);
134137
}
135138

136139
function parseArgs($args) {

0 commit comments

Comments
 (0)