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

False positive: optional array keys and null coalescing operator #4926

Closed
MidnightDesign opened this issue Apr 30, 2021 · 4 comments
Closed
Labels
Milestone

Comments

@MidnightDesign
Copy link
Contributor

Bug report

Code snippet that reproduces the problem

https://phpstan.org/r/0499d145-4d75-4d7f-8e13-168d3562738c

<?php declare(strict_types = 1);

/**
 * @phpstan-type Data array{customer?: array{first_name: string}}
 */
class Foo
{
	/** @var Data */
	private array $data = [];
	
	public function getFirstName(): ?string
	{
		return $this->data['customer']['first_name'] ?? null;
	}
}

Expected output

No errors should be reported.

#3171 and #4592 are maybe related, but not exactly the same.

@ondrejmirtes
Copy link
Member

Also very similar to #4903

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Apr 30, 2021
@phpstan-bot
Copy link
Contributor

@MidnightDesign After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-13: Offset 'customer' does not exist on array(?'customer' => array('first_name' => string)).
+13: Offset 'customer' does not exist on array{customer?: array{first_name: string}}.
Full report
Line Error
13 Offset 'customer' does not exist on array{customer?: array{first_name: string}}.

@phpstan-bot
Copy link
Contributor

@MidnightDesign After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-13: Offset 'customer' does not exist on array(?'customer' => array('first_name' => string)).
+No errors

@github-actions
Copy link

github-actions bot commented Mar 3, 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 Mar 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants