Skip to content

wrongly reporting error on non existing array keys #6783

@quazardous

Description

@quazardous

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions