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

Null coalescing operator throwing access property error #3171

Closed
Krunch opened this issue Apr 15, 2020 · 8 comments · Fixed by phpstan/phpstan-src#1234 or phpstan/phpstan-src#1237
Closed
Labels
Milestone

Comments

@Krunch
Copy link

Krunch commented Apr 15, 2020

Bug report

Code snippet that reproduces the problem

https://phpstan.org/r/c03dea3a-c5d2-4fa7-85e5-2e58f5f43cd1

Error message:
Cannot access property $someArray on PropertyClass|null.

Expected output

No phpstan error. Null coalesce already properly handles nested nulls, and array access to missing keys properly. There is no ambiguity in the code, no dynamic properties are being used so this should be properly handled by phpstan.

Interesting note: If you remove |null from the @var notation then no phpstan error is thrown. It seems to be only triggered when $property can also be null.

Also, if $someArray happened to be something other than an array (like string|null) it would also properly work and phpstan would throw no error.

@Krunch Krunch changed the title Null coalesce operator throwing access property error Search Results Web results Null coalescing operator throwing access property error Apr 15, 2020
@Krunch Krunch changed the title Search Results Web results Null coalescing operator throwing access property error Null coalescing operator throwing access property error Apr 15, 2020
@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Apr 15, 2020
@shmax
Copy link

shmax commented May 13, 2020

Here's one using array shapes:

https://phpstan.org/r/14dff9d6-32e4-4553-bb2b-18924a60a02a

@phpstan-bot
Copy link
Contributor

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

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

@phpstan-bot
Copy link
Contributor

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

@@ @@
+PHP 8.0 (1 error)
+==========
+
+15: Cannot access property $someArray on PropertyClass|null.
+
+PHP 7.3 – 7.4 (2 errors)
+==========
+
+ 5: Property PropertyClass::$someArray has no typehint specified.
+11: Property HelloWorld::$property has no typehint specified.
+
+PHP 7.1 – 7.2 (1 error)
+==========
+
 15: Cannot access property $someArray on PropertyClass|null.
Full report

PHP 8.0 (1 error)

Line Error
15 Cannot access property $someArray on PropertyClass

PHP 7.3 – 7.4 (2 errors)

Line Error
5 Cannot access property $someArray on PropertyClass
11 Cannot access property $someArray on PropertyClass

PHP 7.1 – 7.2 (1 error)

Line Error
15 Property HelloWorld::$property has no typehint specified.

@phpstan-bot
Copy link
Contributor

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

@@ @@
+11: Property HelloWorld::$property is never written, only read.
 15: Cannot access property $someArray on PropertyClass|null.
Full report
Line Error
11 Property HelloWorld::$property is never written, only read.
15 `Cannot access property $someArray on PropertyClass

@phpstan-bot
Copy link
Contributor

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

@@ @@
+11: Property HelloWorld::$property is never written, only read.
 15: Cannot access property $someArray on PropertyClass|null.
Full report
Line Error
11 Property HelloWorld::$property is never written, only read.
15 `Cannot access property $someArray on PropertyClass

@phpstan-bot
Copy link
Contributor

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

@@ @@
-15: Cannot access property $someArray on PropertyClass|null.
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

@phpstan-bot
Copy link
Contributor

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

@@ @@
-15: Cannot access property $someArray on PropertyClass|null.
+15: Cannot access property $someArray on PropertyClass|null.
+11: Property HelloWorld::$property is never written, only read.
Full report
Line Error
15 `Cannot access property $someArray on PropertyClass
11 Property HelloWorld::$property is never written, only read.

@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 May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
4 participants