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

Doctrine's Collection not recognized as array #2176

Closed
Aerendir opened this issue Jun 3, 2019 · 5 comments
Closed

Doctrine's Collection not recognized as array #2176

Aerendir opened this issue Jun 3, 2019 · 5 comments

Comments

@Aerendir
Copy link

Aerendir commented Jun 3, 2019

I have this code:

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{
...
    /**
     * @return Collection|Email[]
     */
    public function getEmails(): Collection
    {
        return $this->emails;
    }
...
}

class OrderVoter extends Voter
{
...
    /**
     * @param Order $order
     * @param User        $user
     *
     * @return bool
     */
    private function isBillee(Order $order, User $user): bool
    {
        return in_array($order->getSrcBillingEmail(), $user->getEmails(), true);
    }

    /**
     * @param Order $order
     * @param User        $user
     *
     * @return bool
     */
    private function isConsignee(Order $order, User $user): bool
    {
        return in_array($order->getSrcShippingEmail(), $user->getEmails(), true);
    }
...
}

The errors I receive are those:

 ------ ------------------------------------------------------------------------------------------------------------------------------------- 
  Line   Security/RemoteOrderVoter.php                                                                                                        
 ------ ------------------------------------------------------------------------------------------------------------------------------------- 
  127    Parameter #2 $haystack of function in_array expects array, Doctrine\Common\Collections\Collection&iterable<App\Entity\Email> given.  
  138    Parameter #2 $haystack of function in_array expects array, Doctrine\Common\Collections\Collection&iterable<App\Entity\Email> given.  
 ------ ------------------------------------------------------------------------------------------------------------------------------------- 

Lines refers to the two methods OrderVoter::isBillee() and OrderVoter::isConsignee().

I've tried to reproduce (for what is possible) the issue here:

https://phpstan.org/r/b7dfa46b-ba40-4f10-9720-093c0258ff19

Any ideas about how to solve those errors?

I'm using version 0.11.6 of PHPStan.

@ondrejmirtes
Copy link
Member

PHPStan is correct here. You really cannot pass Doctrine\Common\Collections\Collection into in_array.

You also really shouldn't check for objects using in_array (and third parameter true) because it will only find exactly the same instances of objects. It's equivalent to ===.

new Email('foo@foo.com') === new Email('foo@foo.com') is false.

Your code is broken a lot.

@Aerendir
Copy link
Author

Aerendir commented Jun 3, 2019

Ok, you are right: I've just refactored with Rector and it changed my code this way... I didn't really think about it in a hurry: I've to come back to the old code... Thank you!

@ondrejmirtes
Copy link
Member

cc @TomasVotruba

@Aerendir
Copy link
Author

Aerendir commented Jun 3, 2019

Anyway the code is not broken as they are entities and so are always the same object once fetched from the database 😅

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jun 3, 2019 via email

@lock lock bot locked as resolved and limited conversation to collaborators Dec 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants