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

TypeSpecifier - support specifying variable types in Assign #3106

Closed
zeleznypa opened this issue Mar 22, 2020 · 10 comments
Closed

TypeSpecifier - support specifying variable types in Assign #3106

zeleznypa opened this issue Mar 22, 2020 · 10 comments
Labels
Milestone

Comments

@zeleznypa
Copy link

When the return type of method is array|null and I check it via is_array, I want to use it as array.

https://phpstan.org/r/c32ca1b7-b1e8-42c0-9bfd-56ba031b887c

@zeleznypa
Copy link
Author

Until the bug will be fixed, additional type-hint can help:
https://phpstan.org/r/6ff03363-9435-4fe4-a560-d5175e679ad5

@zeleznypa
Copy link
Author

Very strange is, that when you want just echo the variable $match[1], it works.

Unfortunately when you want to assign it somewhere, it still not work

https://phpstan.org/r/cdac300e-68c4-44d8-90bb-0357b200cc06

@zeleznypa
Copy link
Author

But when you move the check elsewhere, it is always array :)))
https://phpstan.org/r/3c686f4e-6406-4a93-a75a-8e7a941154e0

@ondrejmirtes ondrejmirtes changed the title is_array() should help PhpStan understand, that array|null is array TypeSpecifier - support specifying variable types in Assign Mar 22, 2020
@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Mar 22, 2020
@phpstan-bot
Copy link
Contributor

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

@@ @@
+PHP 8.0 (1 error)
+==========
+
+25: Parameter #2 $array of function array_key_exists expects array, array<int, string>|null given.
+
+PHP 7.1 – 7.4 (1 error)
+==========
+
 25: Parameter #2 $search of function array_key_exists expects array, array<int, string>|null given.
Full report

PHP 8.0 (1 error)

Line Error
25 Parameter #2 $search of function array_key_exists expects array, array<int, string>

PHP 7.1 – 7.4 (1 error)

Line Error
25 Parameter #2 $array of function array_key_exists expects array, array<int, string>

@phpstan-bot
Copy link
Contributor

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

@@ @@
-No errors
+27: PHPDoc tag @var for variable $match has no value type specified in iterable type array.
Full report
Line Error
27 PHPDoc tag @var for variable $match has no value type specified in iterable type array.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-30: Offset 1 does not exist on array<int, string>|null.
 30: PHPDoc tag @var for variable $match has no value type specified in iterable type array.
Full report
Line Error
30 PHPDoc tag @var for variable $match has no value type specified in iterable type array.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-25: Parameter #2 $search of function array_key_exists expects array, array<int, string>|null given.
+PHP 8.0 (3 errors)
+==========
+
+ 6: Property Foo::$expression has no typehint specified.
+11: Method Foo::match() return type has no value type specified in iterable type array.
+25: Parameter #2 $search of function array_key_exists expects array, array|null given.
+
+PHP 7.4 (3 errors)
+==========
+
+ 6: Property Foo::$expression has no typehint specified.
+11: Method Foo::match() return type has no value type specified in iterable type array.
+25: Parameter #2 $array of function array_key_exists expects array, array|null given.
+
+PHP 7.1 – 7.3 (3 errors)
+==========
+
+ 6: Property Foo::$expression has no typehint specified.
+11: Method Foo::match() return type has no value type specified in iterable type array.
+25: Parameter #2 $search of function array_key_exists expects array, array|null given.
Full report

PHP 8.0 (3 errors)

Line Error
6 Parameter #2 $search of function array_key_exists expects array, array
11 Parameter #2 $search of function array_key_exists expects array, array
25 Parameter #2 $search of function array_key_exists expects array, array

PHP 7.4 (3 errors)

Line Error
6 Parameter #2 $search of function array_key_exists expects array, array
11 Parameter #2 $search of function array_key_exists expects array, array
25 Parameter #2 $search of function array_key_exists expects array, array

PHP 7.1 – 7.3 (3 errors)

Line Error
6 Parameter #2 $array of function array_key_exists expects array, array
11 Parameter #2 $array of function array_key_exists expects array, array
25 Parameter #2 $array of function array_key_exists expects array, array

@phpstan-bot
Copy link
Contributor

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

@@ @@
-25: Parameter #2 $search of function array_key_exists expects array, array<int, string>|null given.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@b2bf703

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue May 11, 2021
@github-actions
Copy link

github-actions bot commented Jun 9, 2021

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 Jun 9, 2021
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