Skip to content

Commit 1788f5b

Browse files
committed
add proc_close special case, see #424
1 parent 5a853de commit 1788f5b

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

generator/config/specialCasesFunctions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
'posix_getpgid',
1818
'fputcsv',
1919
'fgetcsv', // This function need to return false when iterating on an end of file.
20+
'proc_close', // returns -1 on error
2021
];

lib/special_cases.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Safe;
1010

11+
use Safe\Exceptions\ExecException;
1112
use Safe\Exceptions\MiscException;
1213
use Safe\Exceptions\PosixException;
1314
use Safe\Exceptions\SocketsException;
@@ -420,3 +421,31 @@ function fgetcsv($stream, ?int $length = null, string $separator = ",", string $
420421
}
421422
return $safeResult;
422423
}
424+
425+
426+
/**
427+
* proc_close is similar to pclose
428+
* except that it only works on processes opened by
429+
* proc_open.
430+
* proc_close waits for the process to terminate, and
431+
* returns its exit code. Open pipes to that process are closed
432+
* when this function is called, in
433+
* order to avoid a deadlock - the child process may not be able to exit
434+
* while the pipes are open.
435+
*
436+
* @param resource $process The proc_open resource that will
437+
* be closed.
438+
* @return int Returns the termination status of the process that was run. In case of
439+
* an error then -1 is returned.
440+
* @throws ExecException
441+
*
442+
*/
443+
function proc_close($process): int
444+
{
445+
error_clear_last();
446+
$safeResult = \proc_close($process);
447+
if ($safeResult === -1) {
448+
throw ExecException::createFromPhpError();
449+
}
450+
return $safeResult;
451+
}

tests/SpecialCasesTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use PHPUnit\Framework\TestCase;
6+
use Safe\Exceptions\ExecException;
67
use Safe\Exceptions\FilesystemException;
78
use Safe\Exceptions\PcreException;
89

@@ -52,4 +53,10 @@ public function testFgetcsvReturnFalseonEndOfFile(): void
5253
echo var_export($data, true);
5354
}
5455
}*/
56+
57+
public function testProcClose(): void
58+
{
59+
$resource = fopen('php://temp', 'r+');
60+
proc_close($resource); // how to test this failing??
61+
}
5562
}

0 commit comments

Comments
 (0)