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

wrongly reporting error on non existing array keys #6783

Closed
quazardous opened this issue Mar 10, 2022 · 6 comments · Fixed by phpstan/phpstan-src#1657
Closed

wrongly reporting error on non existing array keys #6783

quazardous opened this issue Mar 10, 2022 · 6 comments · Fixed by phpstan/phpstan-src#1657
Labels
Milestone

Comments

@quazardous
Copy link

Bug report

PPHStan is reporting error on non existing array keys.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

/**
* @return array<string, mixed>
*/
function foo(): array
{
	// something from elsewhere (not in the scope of PHPStan)
	return [
                // removing or keeping those lines does/should not change the reporting
		'foo' => [
			'bar' => true,
		]
        ];
}

$data = foo();
$data = $data['foo'] ?? []; // <<< removing this line suppress the error
$data += [
	'default' => true,
];
foreach (['formatted'] as $field) {
	$data[$field] = empty($data[$field]) ? false : true;
}

$bar = $data['bar'];

https://phpstan.org/r/f765334e-f883-4721-a742-7ea798cf9d4c

Expected output

No error

@ondrejmirtes
Copy link
Member

Added dumped types to see where it goes wrong: https://phpstan.org/r/2a1fef8b-0e9e-4d3c-bead-ab21416254be (after the += operator)

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Mar 10, 2022
@quazardous
Copy link
Author

quazardous commented Mar 10, 2022

maybe it's not an "wrong" error (it depends on the confidence you have in the returned data)

// no error with
$data = (array)($data['foo'] ?? []);

@phpstan-bot
Copy link
Contributor

@quazardous After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-26: Offset 'bar' does not exist on array{formatted: bool}.
+No errors

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
 18: Dumped type: array<string, mixed>
 20: Dumped type: mixed
-24: Dumped type: *ERROR*
-28: Dumped type: array{formatted: bool}
-30: Offset 'bar' does not exist on array{formatted: bool}.
+24: Dumped type: non-empty-array
+28: Dumped type: hasOffsetValue('formatted', bool)&non-empty-array
Full report
Line Error
18 Dumped type: array<string, mixed>
20 Dumped type: mixed
24 Dumped type: non-empty-array
28 Dumped type: hasOffsetValue('formatted', bool)&non-empty-array

@ondrejmirtes
Copy link
Member

@staabm Regression test please.

@github-actions
Copy link

github-actions bot commented Oct 1, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants