From 23d0a78a5c047edd13364fc9dd78bfa2783bdd3c Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 5 Nov 2016 01:50:45 -0400 Subject: [PATCH] Add support for piping STDIN --- bin/psalm | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/bin/psalm b/bin/psalm index 109e47b0c..b62877d64 100755 --- a/bin/psalm +++ b/bin/psalm @@ -66,26 +66,34 @@ else { $paths_to_check = null; if ($input_paths) { + $filtered_input_paths = []; + + foreach ($input_paths as $i => $input_path) { + if (($input_path[0] === '-' && strlen($input_path) > 1) || realpath($input_path) === __FILE__) { + continue; + } + + $filtered_input_paths = array_slice($input_paths, $i); + break; + } + + stream_set_blocking(STDIN, 0); + + if ($filtered_input_paths === ['-'] && $stdin = fgets(STDIN)) { + $filtered_input_paths = preg_split('/\s+/', trim($stdin)); + } + $paths_to_check = []; - foreach ($input_paths as $i => $path_to_check) { + foreach ($filtered_input_paths as $i => $path_to_check) { if ($path_to_check[0] === '-') { - if ($paths_to_check) { - die('Invalid usage, expecting psalm [options] [file...]' . PHP_EOL); - } - - $paths_to_check = []; - continue; + die('Invalid usage, expecting psalm [options] [file...]' . PHP_EOL); } if (!file_exists($path_to_check)) { die('Cannot locate ' . $path_to_check . PHP_EOL); } - if (realpath($path_to_check) === __FILE__) { - continue; - } - $paths_to_check[] = realpath($path_to_check); }