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

Solve some PHPStan baseline errors #10242

Merged
merged 2 commits into from
Nov 21, 2022
Merged

Solve some PHPStan baseline errors #10242

merged 2 commits into from
Nov 21, 2022

Conversation

VincentLanglet
Copy link
Contributor

This is some things I saw in the baseline which could be easily solve I think.

@VincentLanglet VincentLanglet changed the base branch from 2.13.x to 2.14.x November 20, 2022 21:50
@@ -675,11 +675,12 @@ public function matching(Criteria $criteria): Collection
/**
* Retrieves the wrapped Collection instance.
*
* @return Collection<TKey, T>
* @return Collection<TKey, T>&Selectable<TKey, T>
*/
public function unwrap(): Collection
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the matching method, there is the code

public function matching(Criteria $criteria): Collection
    {
        if ($this->isDirty) {
            $this->initialize();
        }

        if ($this->initialized) {
            return $this->unwrap()->matching($criteria);
        }

So, unwrap need to return a Selectable if we want to call matching on it.

@@ -97,7 +97,7 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
*
* @param EntityManagerInterface $em The EntityManager the collection will be associated with.
* @param ClassMetadata $class The class descriptor of the entity type of this collection.
* @psalm-param Collection<TKey, T> $collection The collection elements.
* @psalm-param Collection<TKey, T>&Selectable<TKey, T> $collection The collection elements.
*/
public function __construct(EntityManagerInterface $em, $class, Collection $collection)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since unwrap need to return a Selectable (cf next comment), we need to only accepts selectable.

@@ -419,12 +419,6 @@ public function commit($entity = null)
try {
// Collection deletions (deletions of complete collections)
foreach ($this->collectionDeletions as $collectionToDelete) {
if (! $collectionToDelete instanceof PersistentCollection) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not possible since collectionDeletions is always containing PersistentCollection.

  • when looking at the added value they are always persistent collection
  • it it was possible to go in this if, we would have called $collectionToDelete->getMapping() but getMapping only exists for PersistentCollection.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was deliberately added in #7861, which is a bit worrying, but also, the test that was added in that PR still passes, so I guess it's fine?

Copy link
Contributor Author

@VincentLanglet VincentLanglet Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I would say that the developer wanted to be BC

  • If not PersistentCollection do like before
  • If PersistentCollection add the bugfix

But it's not possible to have something else than PersistentCollection ^^
(You can look at the place where collectionDeletions is updated)

@@ -222,14 +222,14 @@ class UnitOfWork implements PropertyChangedListener
/**
* All pending collection deletions.
*
* @psalm-var array<int, Collection<array-key, object>>
* @psalm-var array<int, PersistentCollection<array-key, object>>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only add PersistentCollection to this array

if ($orgValue instanceof PersistentCollection) {
                    // A PersistentCollection was de-referenced, so delete it.
                    $coid = spl_object_id($orgValue);

                    if (isset($this->collectionDeletions[$coid])) {
                        continue;
                    }

                    $this->collectionDeletions[$coid] = $orgValue;
                    $changeSet[$propName]             = $orgValue; // Signal changeset, to-many assocs will be ignored.

                    continue;
                }

*/
private $collectionDeletions = [];

/**
* All pending collection updates.
*
* @psalm-var array<int, Collection<array-key, object>>
* @psalm-var array<int, PersistentCollection<array-key, object>>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only add PersistentCollection to this array

if ($value instanceof PersistentCollection && $value->isDirty()) {
            $coid = spl_object_id($value);

            $this->collectionUpdates[$coid]  = $value;
            $this->visitedCollections[$coid] = $value;
        }

@@ -238,7 +238,7 @@ class UnitOfWork implements PropertyChangedListener
* At the end of the UnitOfWork all these collections will make new snapshots
* of their data.
*
* @psalm-var array<int, Collection<array-key, object>>
* @psalm-var array<int, PersistentCollection<array-key, object>>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only add PersistentCollection to this array

if ($value instanceof PersistentCollection && $value->isDirty()) {
            $coid = spl_object_id($value);

            $this->collectionUpdates[$coid]  = $value;
            $this->visitedCollections[$coid] = $value;
        }

@VincentLanglet
Copy link
Contributor Author

I just have one error to solve for phpstan https://github.com/doctrine/orm/actions/runs/3509913825/jobs/5879333229
What do you recommend @greg0ire ?

It's related to the code

foreach ($class->reflFields as $name => $refProp) {
            $value = $refProp->getValue($entity);

            if ($class->isCollectionValuedAssociation($name) && $value !== null) {
                if ($value instanceof PersistentCollection) {
                    if ($value->getOwner() === $entity) {
                        continue;
                    }

                    $value = new ArrayCollection($value->getValues());
                }

                // If $value is not a Collection then use an ArrayCollection.
                if (! $value instanceof Collection) {
                    $value = new ArrayCollection($value);
                }

                $assoc = $class->associationMappings[$name];

                // Inject PersistentCollection
                $value = new PersistentCollection(
                    $this->em,
                    $this->em->getClassMetadata($assoc['targetEntity']),
                    $value
                );

we're only checking value is an instanceof Collection before passing the value to the PersistentCollection, but as explained in the comment of the PR, Persistent require we pass a Selectable&Collection or the matching method won't work.

Should I just add this error to the baseline ? (So it would be 5 less, for 1 more in the baseline)

@greg0ire
Copy link
Member

Should I just add this error to the baseline ? (So it would be 5 less, for 1 more in the baseline)

Yes, it sounds like a fair tradeoff 👍

@VincentLanglet VincentLanglet changed the title Solve some baseline errors Solve some PHPStan baseline errors Nov 20, 2022
@VincentLanglet VincentLanglet marked this pull request as ready for review November 20, 2022 22:59
@greg0ire greg0ire merged commit f9d5a89 into doctrine:2.14.x Nov 21, 2022
@greg0ire
Copy link
Member

Thanks @VincentLanglet !

@derrabus derrabus added this to the 2.14.0 milestone Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants