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

Invalid map in collection. #621

Closed
laurentmuller opened this issue Oct 28, 2024 · 12 comments · Fixed by #622
Closed

Invalid map in collection. #621

laurentmuller opened this issue Oct 28, 2024 · 12 comments · Fixed by #622

Comments

@laurentmuller
Copy link

laurentmuller commented Oct 28, 2024

After update PHPStan to version 1.5.4, the map function of collection does not work anymore.

Example:

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

class Item {

    private ?Parent  $parent = null;
    
    public function setParent(?Parent $parent): void
    {
        $this->parent = $parent;
    }
}

class Parent {

    /**
     * @var Collection<int, Item>
     */
    private Collection $items;

    public function __construct()
    {
        $this->items = new ArrayCollection();
    }

    public function __clone()
    {
        $this->items = $this->items->map(
            fn (Item $item): Item => (clone $item)->setParent($this)
        );
    }
}

The follwing error is raised:

------ -------------------------------------------------------------------------------------------------
  Line   src\Parent.php
 ------ -------------------------------------------------------------------------------------------------
  81     Property Parent::$items (Doctrine\Common\Collections\Collection<int, Item>)
         does not accept Doctrine\Common\Collections\ReadableCollection<int, Item>.
         🪪  assign.propertyType
 ------ ------------------------------------------------------------------------------------------------- 

Maybe, the Collection and ArrayCollection stubs must be also updated.

@ondrejmirtes
Copy link
Member

/cc @xificurk Is it possible you broke this with your stubs? Thanks.

@laurentmuller
Copy link
Author

I does not use any custom stubs.

@ondrejmirtes
Copy link
Member

You use phpstan-doctrine which includes a lot of them.

@laurentmuller
Copy link
Author

Yes. But before commit #620, my code working without error.

@xificurk
Copy link
Contributor

Hm, I might be missing some phpstan internals knowledge, but (to my surprise), it looks like the stub phpdoc from ReadableCollection::map takes precedence not only over original ReadableCollection::map phpdoc, but even original Collection::map.

@mvhirsch
Copy link
Contributor

I'm running into the same issue, using ::filter():

    /**
     * @return Collection<int, TopicItem>
     */
    public function getItems(): Collection
    {
        $livestreamStartLimiter = new \DateTimeImmutable(self::LIVESTREAM_START_LIMITER);

        return $this->items->filter(function (TopicItem $topicItem) use ($livestreamStartLimiter): bool {
            // simplified code here
            return true;
        });
    }
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/TopicBundle/Entity/Topic.php                                                                                                                                                                             
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  280    Method App\TopicBundle\Entity\Topic::getItems() should return Doctrine\Common\Collections\Collection<int, App\TopicBundle\Entity\TopicItem> but returns Doctrine\Common\Collections\ReadableCollection<int,  
         App\TopicBundle\Entity\TopicItem>.                                                                                                                                                                           
         ✏️  src/TopicBundle/Entity/Topic.php                                                                                                                                                                         
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

This happens while upgrading phpstan/phpstan-doctrine from 1.5.3 => 1.5.4.

@laurentmuller
Copy link
Author

It'seem that we have to update the *.stub files in Collections folder to reflect the php doc.

What happens if we remove all this collection stub files ?

@Jean85
Copy link

Jean85 commented Oct 29, 2024

I'm running into the same issue, using ::filter():

[...]

This happens while upgrading phpstan/phpstan-doctrine from 1.5.3 => 1.5.4.

Exact same situation here.

@ondrejmirtes
Copy link
Member

Fixed #622

@laurentmuller
Copy link
Author

I just downloaded the last version (1.5.5) and it is work as expected !

Great job !!!!

@mvhirsch
Copy link
Contributor

Thank you, 1.5.5 works for me, too!

Copy link

github-actions bot commented Dec 2, 2024

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 Dec 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants