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

Wrong assignation to ArrayAccess&SomeOtherType not reported #6184

Closed
julienfalque opened this issue Dec 10, 2021 · 9 comments
Closed

Wrong assignation to ArrayAccess&SomeOtherType not reported #6184

julienfalque opened this issue Dec 10, 2021 · 9 comments
Labels
Milestone

Comments

@julienfalque
Copy link

Bug report

When assigning a wrong new value to a variable of type ArrayAccess, the error is not reported if the type of the variable includes an intersection with another type.

Code snippet that reproduces the problem

class Foo
{
    /** @var \ArrayAccess<int, string> */
    private $collection1;

    /** @var \ArrayAccess<int, string>&\Countable */
    private $collection2;

    public function foo(): void
    {
        $this->collection1[] = 1;
        $this->collection2[] = 2;
    }
}

https://phpstan.org/r/8142895e-0a04-44b6-82c9-e1eed898da54

Expected output

Both assignations should be reported as they don't accept int.

@phpstan-bot
Copy link
Contributor

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

@@ @@
+ 6: Property Foo::$collection1 is never written, only read.
+ 9: Property Foo::$collection2 is never written, only read.
 13: ArrayAccess<int, string> does not accept int.
Full report
Line Error
6 Property Foo::$collection1 is never written, only read.
9 Property Foo::$collection2 is never written, only read.
13 ArrayAccess<int, string> does not accept int.

@phpstan-bot
Copy link
Contributor

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

@@ @@
+ 6: Property Foo::$collection1 is never read, only written.
+ 9: Property Foo::$collection2 is never read, only written.
 13: ArrayAccess<int, string> does not accept int.
Full report
Line Error
6 Property Foo::$collection1 is never read, only written.
9 Property Foo::$collection2 is never read, only written.
13 ArrayAccess<int, string> does not accept int.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-13: ArrayAccess<int, string> does not accept int.
+ 6: Property Foo::$collection1 is never read, only written.
+ 9: Property Foo::$collection2 is never read, only written.
+13: ArrayAccess<int, string> does not accept int.
+13: Property Foo::$collection1 (ArrayAccess<int, string>) does not accept array{1}.
+14: Property Foo::$collection2 (ArrayAccess<int, string>&Countable) does not accept array{2}.
Full report
Line Error
6 Property Foo::$collection1 is never read, only written.
9 Property Foo::$collection2 is never read, only written.
13 ArrayAccess<int, string> does not accept int.
13 Property Foo::$collection1 (ArrayAccess<int, string>) does not accept array{1}.
14 Property Foo::$collection2 (ArrayAccess<int, string>&Countable) does not accept array{2}.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-13: ArrayAccess<int, string> does not accept int.
+ 6: Property Foo::$collection1 is never read, only written.
+ 9: Property Foo::$collection2 is never read, only written.
+13: ArrayAccess<int, string> does not accept int.
+14: Property Foo::$collection2 (ArrayAccess<int, string>&Countable) does not accept Countable.
Full report
Line Error
6 Property Foo::$collection1 is never read, only written.
9 Property Foo::$collection2 is never read, only written.
13 ArrayAccess<int, string> does not accept int.
14 Property Foo::$collection2 (ArrayAccess<int, string>&Countable) does not accept Countable.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@3b5fd94

@julienfalque
Copy link
Author

Thank you @ondrejmirtes!

@julienfalque
Copy link
Author

Shouldn't the last error be

14: ArrayAccess<int, string>&Countable does not accept int.

?

@ondrejmirtes
Copy link
Member

Yes, it should, but for complicated reasons this is the best we can do right now.

@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 14, 2022
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.

3 participants