From 550bd2c95c8864b8e658108c3fc0d891de8800f9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 13 Nov 2022 20:30:10 +0100 Subject: [PATCH] parse_str() might return arrays by-ref --- stubs/core.stub | 14 +++++++------- tests/PHPStan/Analyser/data/param-out.php | 6 +++--- .../NonexistentOffsetInArrayDimFetchRuleTest.php | 11 +++++++++++ tests/PHPStan/Rules/Arrays/data/bug-8356.php | 9 +++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 tests/PHPStan/Rules/Arrays/data/bug-8356.php diff --git a/stubs/core.stub b/stubs/core.stub index 01d26d6dd17..ef01d8a8af0 100644 --- a/stubs/core.stub +++ b/stubs/core.stub @@ -70,10 +70,16 @@ function bin2hex(string $string): string {} /** * @param array $result - * @param-out array> $result + * @param-out array|array> $result */ function parse_str(string $string, array &$result): void {} +/** + * @param array $result + * @param-out array|array> $result + */ +function mb_parse_str(string $string, array &$result): bool {} + /** @param-out float $percent */ function similar_text(string $string1, string $string2, float &$percent = null) : int {} @@ -300,12 +306,6 @@ function fsockopen(string $hostname, int $port = -1, int &$error_code = null, st */ function headers_sent(string &$filename = null, int &$line = null): bool {} -/** - * @param mixed $result - * @param-out array> $result - */ -function mb_parse_str(string $string, &$result = []): bool {} - /** * @param-out callable-string $callable_name */ diff --git a/tests/PHPStan/Analyser/data/param-out.php b/tests/PHPStan/Analyser/data/param-out.php index 09a10bb8d8a..5609151bcff 100644 --- a/tests/PHPStan/Analyser/data/param-out.php +++ b/tests/PHPStan/Analyser/data/param-out.php @@ -330,7 +330,7 @@ function testParseStr() { echo $output['arr'][1];//baz */ - \PHPStan\Testing\assertType('array|string>', $output); + \PHPStan\Testing\assertType('array', $output); } function fooSimilar() { @@ -407,10 +407,10 @@ function fooHeadersSent() { function fooMbParseStr() { mb_parse_str("foo=bar", $output); - assertType('array|string>', $output); + assertType('array', $output); mb_parse_str('email=mail@example.org&city=town&x=1&y[g]=3&f=1.23', $output); - assertType('array|string>', $output); + assertType('array', $output); } function fooPreg() diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index 6a12a4a920a..1447c6c9f68 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -700,4 +700,15 @@ public function testBug6243(): void $this->analyse([__DIR__ . '/data/bug-6243.php'], []); } + public function testBug8356(): void + { + $this->bleedingEdge = true; + $this->analyse([__DIR__ . '/data/bug-8356.php'], [ + [ + "Offset 'x' might not exist on array|string.", + 7, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Arrays/data/bug-8356.php b/tests/PHPStan/Rules/Arrays/data/bug-8356.php new file mode 100644 index 00000000000..192a71d3630 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-8356.php @@ -0,0 +1,9 @@ +