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

Array (array<string, string|null>) does not accept key string. #2970

Closed
Kocal opened this issue Feb 11, 2020 · 15 comments · Fixed by phpstan/phpstan-src#1941
Closed

Array (array<string, string|null>) does not accept key string. #2970

Kocal opened this issue Feb 11, 2020 · 15 comments · Fixed by phpstan/phpstan-src#1941
Labels
Milestone

Comments

@Kocal
Copy link
Contributor

Kocal commented Feb 11, 2020

Bug report

I have an array ['html' => null, 'htmlsource' => null, 'txt' => null, 'raw' => null, 'eml' => null] of type array{html: ?string, htmlsource: ?string, txt: ?string, raw: ?string, eml: ?string}.

I'm trying to change $array[$format] value (where $format is either html, htmlsource, txt, raw, or eml), but PHPStan is throwing error Array (array<string, string|null>) does not accept key string..

There is also another error Method Message::getBody() should return string but returns string|null.. I don't know if it's related, but this is super weird since I assign a string to $this->body[$format] if it's equals to null.

Code snippet that reproduces the problem

https://phpstan.org/r/ec4cb784-4886-4bb8-83b8-e034fd042f55

Expected output

The static analytics should not claim the two errors.

Thanks!

@muglug
Copy link
Contributor

muglug commented Feb 11, 2020

The array{...} annotation is not intended to be exhaustive (there can be other keys as well) – it describes the minimum-acceptable keys in the array, so I don't think this is a bug.

@ondrejmirtes
Copy link
Member

At least the message could be better...

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 11, 2020
@Kocal
Copy link
Contributor Author

Kocal commented Feb 11, 2020

Note that I also check if key $format exists on $this-body, the following code should be working properly, right? :

if (!array_key_exists($format, $this->body)) {
    throw new \InvalidArgumentException(sprintf('Format "%s" is not a valid body format.', $format));
}

if ($this->body[$format] === null) {
    $this->body[$format] = 'foobar'; // Array (array<string, string|null>) does not accept key string
}

@phpstan-bot
Copy link
Contributor

@Kocal PHPStan now reports different result with your code snippet:

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+14: Array (array<string, string|null>) does not accept key string.
Full report
Line Error
14 Array (array<string, string

@phpstan-bot
Copy link
Contributor

@Kocal PHPStan now reports different result with your code snippet:

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+ 5: Property Message::$body has no typehint specified.
Full report
Line Error
5 Property Message::$body has no typehint specified.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+14: Array (array('html' => string|null, 'htmlsource' => string|null, 'txt' => string|null, 'raw' => string|null, 'eml' => string|null)) does not accept key string.
Full report
Line Error
14 `Array (array('html' => string

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+14: Array (array{html: string|null, htmlsource: string|null, txt: string|null, raw: string|null, eml: string|null}) does not accept key string.
Full report
Line Error
14 `Array (array{html: string

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+14: Property Message::$body (array{html: string|null, htmlsource: string|null, txt: string|null, raw: string|null, eml: string|null}) does not accept non-empty-array<string, 'foobar'>.
Full report
Line Error
14 `Property Message::$body (array{html: string

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+14: Property Message::$body (array{html: string|null, htmlsource: string|null, txt: string|null, raw: string|null, eml: string|null}) does not accept non-empty-array<string, string|null>.
Full report
Line Error
14 `Property Message::$body (array{html: string

@VincentLanglet
Copy link
Contributor

This works fine https://phpstan.org/r/784dfc05-6cd8-47a8-bfa2-e51f751fa1de

So it seems like PHPStan is understanding better

!in_array($format, array_keys($this->body), true)

than

!array_key_exists($format, $this->body)

It is something to improve in ArrayKeyExistsFunctionTypeSpecifyingExtension I think, I'll take a look.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: Array (array<string, string|null>) does not accept key string.
-17: Method Message::getBody() should return string but returns string|null.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#1941

@Kocal
Copy link
Contributor Author

Kocal commented Jan 17, 2023

Nice, thanks :)

@github-actions
Copy link

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 Feb 18, 2023
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.

5 participants