Skip to content

Commit e4e21c7

Browse files
staabmmvorisek
andauthored
Added proc_open() stub for param-out
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
1 parent 8996d39 commit e4e21c7

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

stubs/core.stub

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,18 @@ function header_register_callback(callable $callback): bool {}
362362
* @param-later-invoked-callable $callback
363363
*/
364364
function register_tick_function(callable $callback, mixed ...$args): bool {}
365+
366+
/**
367+
* @template P of int
368+
*
369+
* @param string|list<string> $command
370+
* @param array<P, list<string>|resource> $descriptor_spec
371+
* @param mixed $pipes
372+
* @param null|array<string, mixed> $env_vars
373+
* @param null|array<string, bool> $options
374+
*
375+
* @param-out array<P, resource> $pipes
376+
*
377+
* @return resource|false
378+
*/
379+
function proc_open($command, array $descriptor_spec, &$pipes, ?string $cwd = null, ?array $env_vars = null, ?array $options = null) {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13197;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @return array{string, string}|null STDOUT & STDERR tuple
9+
*/
10+
function execute(string $command): ?array
11+
{
12+
if (!function_exists('proc_open')) {
13+
return null;
14+
}
15+
16+
$pipes = [];
17+
18+
$process = @proc_open(
19+
$command,
20+
[
21+
['pipe', 'rb'],
22+
['pipe', 'wb'], // stdout
23+
['pipe', 'wb'], // stderr
24+
],
25+
$pipes
26+
);
27+
28+
assertType('array<0|1|2, resource>', $pipes);
29+
30+
if (!is_resource($process)) {
31+
return null;
32+
}
33+
34+
fclose($pipes[0]);
35+
36+
$stdout = (string) stream_get_contents($pipes[1]);
37+
$stderr = (string) stream_get_contents($pipes[2]);
38+
39+
proc_close($process);
40+
41+
return [$stdout, $stderr];
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13197b;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function execute(string $command): void
8+
{
9+
if (!function_exists('proc_open')) {
10+
return;
11+
}
12+
13+
$pipes = [];
14+
15+
$process = @proc_open(
16+
$command,
17+
[
18+
['pipe', 'rb'],
19+
3 => ['pipe', 'wb'], // https://stackoverflow.com/questions/28909347/is-it-possible-to-connect-more-than-the-two-standard-streams-to-a-terminal-in-li#28909376
20+
5 => ['pipe', 'wb'],
21+
],
22+
$pipes
23+
);
24+
25+
assertType('array<0|3|5, resource>', $pipes);
26+
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,4 +2317,11 @@ public function testBug12317(): void
23172317
]);
23182318
}
23192319

2320+
public function testBug13197(): void
2321+
{
2322+
$this->checkExplicitMixed = true;
2323+
$this->checkImplicitMixed = true;
2324+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-13197.php'], []);
2325+
}
2326+
23202327
}

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,4 +928,11 @@ public function testBug11241b(): void
928928
]);
929929
}
930930

931+
public function testBug11777(): void
932+
{
933+
$this->checkExplicitMixed = true;
934+
$this->checkImplicitMixed = true;
935+
$this->analyse([__DIR__ . '/data/bug-11777.php'], []);
936+
}
937+
931938
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Bug11777;
4+
5+
class HelloWorld {
6+
/** @var array<int, resource> */
7+
private array $pipes;
8+
9+
public function sayHello(string $cmd): void {
10+
proc_open($cmd, [0 => ['pipe', 'r']], $this->pipes);
11+
}
12+
}

0 commit comments

Comments
 (0)