Skip to content

Commit

Permalink
parse_str() might return nested arrays by-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 15, 2022
1 parent b428afc commit b0ad827
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
14 changes: 7 additions & 7 deletions stubs/core.stub
Expand Up @@ -70,10 +70,16 @@ function bin2hex(string $string): string {}

/**
* @param array<mixed> $result
* @param-out array<int|string, string|list<string>> $result
* @param-out array<int|string, array|string> $result
*/
function parse_str(string $string, array &$result): void {}

/**
* @param array<mixed> $result
* @param-out array<string, array|string> $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 {}

Expand Down Expand Up @@ -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<string, string|array<string>> $result
*/
function mb_parse_str(string $string, &$result = []): bool {}

/**
* @param-out callable-string $callable_name
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/data/param-out.php
Expand Up @@ -330,7 +330,7 @@ function testParseStr() {
echo $output['arr'][1];//baz
*/

\PHPStan\Testing\assertType('array<int|string, list<string>|string>', $output);
\PHPStan\Testing\assertType('array<int|string, array|string>', $output);
}

function fooSimilar() {
Expand Down Expand Up @@ -407,10 +407,10 @@ function fooHeadersSent() {

function fooMbParseStr() {
mb_parse_str("foo=bar", $output);
assertType('array<string, array<string>|string>', $output);
assertType('array<string, array|string>', $output);

mb_parse_str('email=mail@example.org&city=town&x=1&y[g]=3&f=1.23', $output);
assertType('array<string, array<string>|string>', $output);
assertType('array<string, array|string>', $output);
}

function fooPreg()
Expand Down
Expand Up @@ -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,
],
]);
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-8356.php
@@ -0,0 +1,9 @@
<?php

namespace Bug8356;

function foo(): void {
parse_str('filter[x][y]=0', $output);
print $output['filter']['x']['y'];
}

0 comments on commit b0ad827

Please sign in to comment.