-
Notifications
You must be signed in to change notification settings - Fork 555
Fix phpstan/phpstan#6799: wrongly reported empty array #5206
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
base: 2.1.x
Are you sure you want to change the base?
Changes from 13 commits
c548c52
5ed6c61
8fbc944
58e5d14
2cb5e98
d285cf5
0a988f2
037a1c6
9697ad0
47445ba
1a312aa
3c97119
e013a69
bc76dc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug6799; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param string[] $where | ||
| * @param string $sqlTableName | ||
| * @param mixed[] $filter | ||
| * @param string $value | ||
| */ | ||
| protected function listingAddWhereFilterAtableDefault(array &$where, string $sqlTableName, array $filter, string $value): void | ||
| { | ||
| if ($value != "" && !empty($filter) && !empty($filter['sql']) && is_string($filter['sql'])) { | ||
| $where[] = "`" . $sqlTableName . "`.`" . (string)$filter['sql'] . "` = '" . $value . "'"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $filterValues | ||
| * @param string[] $where | ||
| * @param string[] $tables | ||
| * @param mixed[] $filters | ||
| */ | ||
| protected function listingAddWhereFilterAtable(array $filterValues, array &$where, array &$tables, array $filters): void | ||
| { | ||
| if (!empty($filterValues) && !empty($filters)) { | ||
| $whereFilter = array(); | ||
| foreach ($filterValues as $type => $value) { | ||
| call_user_func_array(array($this, 'listingAddWhereFilterAtableDefault'), array(&$whereFilter, 'xxxx', $filters[$type], $value)); | ||
| } | ||
| assertType('mixed', $whereFilter); | ||
| if (count($whereFilter) > 0) { | ||
| $where[] = "(" . implode(" AND ", $whereFilter) . ")"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed $foo | ||
| */ | ||
| function foo($foo): void {} | ||
|
|
||
| function testByRefInArray(): void | ||
| { | ||
| $a = []; | ||
| assertType('array{}', $a); | ||
|
|
||
| $b = [&$a]; | ||
| assertType('array{}', $a); | ||
|
|
||
| foo($b); | ||
| assertType('array{}', $a); | ||
| } | ||
|
|
||
| function testByRefInArrayWithKey(): void | ||
| { | ||
| $a = 'hello'; | ||
| assertType("'hello'", $a); | ||
|
|
||
| $b = ['key' => &$a]; | ||
| assertType("'hello'", $a); | ||
|
|
||
| $b['key'] = 42; | ||
| assertType("'hello'", $a); | ||
|
||
| } | ||
|
|
||
| function testMultipleByRefInArray(): void | ||
| { | ||
| $a = 1; | ||
| $c = 'test'; | ||
|
|
||
| $b = [&$a, 'normal', &$c]; | ||
| assertType('1', $a); | ||
| assertType("'test'", $c); | ||
|
|
||
| $b[0] = 2; | ||
| $b[1] = 'foo'; | ||
| $b[2] = 'bar'; | ||
|
|
||
| assertType('1', $a); | ||
| assertType("'test'", $c); | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug6799b; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
|
|
||
|
|
||
| /** | ||
| * listingAddWhereFilterAtableRoleCategory | ||
| * | ||
| * @param string[] $where | ||
| * @param string $sqlTableName | ||
| * @param mixed[] $filter | ||
| * @param string $value | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function listingAddWhereFilterAtableDefault(array &$where, string $sqlTableName, array $filter, string $value): void | ||
| { | ||
| if ($value != "" && !empty($filter) && !empty($filter['sql']) && is_string($filter['sql'])) { | ||
| $where[] = "`" . $sqlTableName . "`.`" . (string)$filter['sql'] . "` = '" . $value . "'"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * listingAddWhereFilterAtableFilter | ||
| * | ||
| * @param string[] $filterValues | ||
| * @param string[] $where | ||
| * @param string[] $tables | ||
| * @param mixed[] $filters | ||
| * @return void | ||
| */ | ||
| protected function listingAddWhereFilterAtable(array $filterValues, array &$where, array &$tables, array $filters): void | ||
| { | ||
| if (!empty($filterValues) && !empty($filters)) { | ||
| $whereFilter = array(); | ||
| foreach ($filterValues as $type => $value) { | ||
| $this->listingAddWhereFilterAtableDefault($whereFilter, 'xxxxx', $filters[$type], $value); | ||
| } | ||
| assertType('array<string>', $whereFilter); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug6799C; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // https://3v4l.org/g5UjS | ||
|
|
||
| $a = [&$x]; | ||
| assertType('mixed', $x); | ||
|
|
||
| function doFoo(array &$arr) { | ||
| $arr[0] = 'string'; | ||
| } | ||
|
|
||
| doFoo($a); | ||
| assertType('mixed', $x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be modified since $b was passed to foo and is related to $a