@@ -45,22 +45,23 @@ $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
45
45
foreach ($ files as $ file ) {
46
46
if (strpos ($ file , '<?php ' ) === 0 ) {
47
47
$ code = $ file ;
48
- echo "====> Code $ code \n" ;
48
+ fwrite ( STDERR , "====> Code $ code \n" ) ;
49
49
} else {
50
50
if (!file_exists ($ file )) {
51
- die ("File $ file does not exist. \n" );
51
+ fwrite (STDERR , "File $ file does not exist. \n" );
52
+ exit (1 );
52
53
}
53
54
54
55
$ code = file_get_contents ($ file );
55
- echo "====> File $ file: \n" ;
56
+ fwrite ( STDERR , "====> File $ file: \n" ) ;
56
57
}
57
58
58
59
if ($ attributes ['with-recovery ' ]) {
59
60
$ errorHandler = new PhpParser \ErrorHandler \Collecting ;
60
61
$ stmts = $ parser ->parse ($ code , $ errorHandler );
61
62
foreach ($ errorHandler ->getErrors () as $ error ) {
62
63
$ message = formatErrorMessage ($ error , $ code , $ attributes ['with-column-info ' ]);
63
- echo $ message . "\n" ;
64
+ fwrite ( STDERR , $ message . "\n" ) ;
64
65
}
65
66
if (null === $ stmts ) {
66
67
continue ;
@@ -70,25 +71,26 @@ foreach ($files as $file) {
70
71
$ stmts = $ parser ->parse ($ code );
71
72
} catch (PhpParser \Error $ error ) {
72
73
$ message = formatErrorMessage ($ error , $ code , $ attributes ['with-column-info ' ]);
73
- die ($ message . "\n" );
74
+ fwrite (STDERR , $ message . "\n" );
75
+ exit (1 );
74
76
}
75
77
}
76
78
77
79
foreach ($ operations as $ operation ) {
78
80
if ('dump ' === $ operation ) {
79
- echo "==> Node dump: \n" ;
81
+ fwrite ( STDERR , "==> Node dump: \n" ) ;
80
82
echo $ dumper ->dump ($ stmts , $ code ), "\n" ;
81
83
} elseif ('pretty-print ' === $ operation ) {
82
- echo "==> Pretty print: \n" ;
84
+ fwrite ( STDERR , "==> Pretty print: \n" ) ;
83
85
echo $ prettyPrinter ->prettyPrintFile ($ stmts ), "\n" ;
84
86
} elseif ('json-dump ' === $ operation ) {
85
- echo "==> JSON dump: \n" ;
87
+ fwrite ( STDERR , "==> JSON dump: \n" ) ;
86
88
echo json_encode ($ stmts , JSON_PRETTY_PRINT ), "\n" ;
87
89
} elseif ('var-dump ' === $ operation ) {
88
- echo "==> var_dump(): \n" ;
90
+ fwrite ( STDERR , "==> var_dump(): \n" ) ;
89
91
var_dump ($ stmts );
90
92
} elseif ('resolve-names ' === $ operation ) {
91
- echo "==> Resolved names. \n" ;
93
+ fwrite ( STDERR , "==> Resolved names. \n" ) ;
92
94
$ stmts = $ traverser ->traverse ($ stmts );
93
95
}
94
96
}
@@ -104,9 +106,9 @@ function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
104
106
105
107
function showHelp ($ error = '' ) {
106
108
if ($ error ) {
107
- echo $ error . "\n\n" ;
109
+ fwrite ( STDERR , $ error . "\n\n" ) ;
108
110
}
109
- die ( <<<OUTPUT
111
+ fwrite ( $ error ? STDERR : STDOUT , <<<OUTPUT
110
112
Usage: php-parse [operations] file1.php [file2.php ...]
111
113
or: php-parse [operations] "<?php code"
112
114
Turn PHP source code into an abstract syntax tree.
@@ -131,6 +133,7 @@ Example:
131
133
132
134
OUTPUT
133
135
);
136
+ exit ($ error ? 1 : 0 );
134
137
}
135
138
136
139
function parseArgs ($ args ) {
0 commit comments