1212namespace Ahc \Cli \Helper ;
1313
1414use Ahc \Cli \Exception \RuntimeException ;
15+
1516use function fclose ;
1617use function function_exists ;
1718use function fwrite ;
2324use function proc_terminate ;
2425use function stream_get_contents ;
2526use function stream_set_blocking ;
26- use const DIRECTORY_SEPARATOR ;
2727
2828/**
2929 * A thin proc_open wrapper to execute shell commands.
@@ -109,7 +109,7 @@ public function __construct(protected string $command, protected ?string $input
109109
110110 protected function prepareDescriptors (?array $ stdin = null , ?array $ stdout = null , ?array $ stderr = null ): array
111111 {
112- $ win = $ this -> isWindows ();
112+ $ win = Terminal:: isWindows ();
113113 if (!$ stdin ) {
114114 $ stdin = $ win ? self ::DEFAULT_STDIN_WIN : self ::DEFAULT_STDIN_NIX ;
115115 }
@@ -127,16 +127,6 @@ protected function prepareDescriptors(?array $stdin = null, ?array $stdout = nul
127127 ];
128128 }
129129
130- protected function isWindows (): bool
131- {
132- // If PHP_OS is defined, use it - More reliable:
133- if (defined ('PHP_OS ' )) {
134- return 'WIN ' === strtoupper (substr (PHP_OS , 0 , 3 )); // May be 'WINNT' or 'WIN32' or 'Windows'
135- }
136-
137- return '\\' === DIRECTORY_SEPARATOR ; // Fallback - Less reliable (Windows 7...)
138- }
139-
140130 protected function setInput (): void
141131 {
142132 //Make sure the pipe is a stream resource before writing to it to avoid a warning
@@ -263,12 +253,9 @@ public function execute(bool $async = false, ?array $stdin = null, ?array $stdou
263253
264254 private function setOutputStreamNonBlocking (): bool
265255 {
266- // Make sure the pipe is a stream resource before setting it to non-blocking to avoid a warning
267- if (!is_resource ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ])) {
268- return false ;
269- }
256+ $ isRes = is_resource ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ]);
270257
271- return stream_set_blocking ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ], false );
258+ return $ isRes ? stream_set_blocking ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ], false ) : false ;
272259 }
273260
274261 public function getState (): string
@@ -278,21 +265,16 @@ public function getState(): string
278265
279266 public function getOutput (): string
280267 {
281- // Make sure the pipe is a stream resource before reading it to avoid a warning
282- if (!is_resource ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ])) {
283- return '' ;
284- }
268+ $ isRes = is_resource ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ]);
285269
286- return stream_get_contents ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ]);
270+ return $ isRes ? stream_get_contents ($ this ->pipes [self ::STDOUT_DESCRIPTOR_KEY ]) : '' ;
287271 }
288272
289273 public function getErrorOutput (): string
290274 {
291- if (!is_resource ($ this ->pipes [self ::STDERR_DESCRIPTOR_KEY ])) {
292- return '' ;
293- }
275+ $ isRes = is_resource ($ this ->pipes [self ::STDERR_DESCRIPTOR_KEY ]);
294276
295- return stream_get_contents ($ this ->pipes [self ::STDERR_DESCRIPTOR_KEY ]);
277+ return $ isRes ? stream_get_contents ($ this ->pipes [self ::STDERR_DESCRIPTOR_KEY ]) : '' ;
296278 }
297279
298280 public function getExitCode (): ?int
0 commit comments