Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_str() might return arrays by-ref #1994

Merged
merged 3 commits into from Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved parse_str and mb_parse_str next to each other, and equalized the type signatures


/**
* @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.",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this PR, the rule reported
Offset 'x' does not exist on array|string.
.. now we get ..
Offset 'x' might not exist on array|string.

when running this test without $this->bleedingEdge = true; the signature changes in the stub do not have any effect and we still get Offset 'x' does not exist on array|string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bot only goes through triaged issues labeled as bug or feature-request.

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'];
}