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

Psalm 5 fixes #10248

Merged
merged 2 commits into from Nov 24, 2022
Merged

Psalm 5 fixes #10248

merged 2 commits into from Nov 24, 2022

Conversation

greg0ire
Copy link
Member

Found while trying out Psalm 5 the other day. It does not fixes all new issues, but it's a start. Maybe we'll go through the others, maybe we'll add them to the baseline.

Comment on lines 1843 to 1848
default:
throw new UnexpectedValueException(sprintf(
'Unexpected entity state: %s. %s',
$entityState,
self::objToStr($entity)
));
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't remove these defaults. They're technically dead code, but I'd consider them a defense against a misbehaving getEntityState() implementation. Let's baseline or suppress the error.

On 3.0.x, we could turn the switch into a match and rely on the runtime to error if none of the cases matched.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's baseline or suppress the error.

That will only be doable when using Psalm 5, but OK 👍

Comment on lines 1916 to 1921
default:
throw new UnexpectedValueException(sprintf(
'Unexpected entity state: %s. %s',
$entityState,
self::objToStr($entity)
));
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@@ -15,6 +15,8 @@ interface CollectionPersister
/**
* Deletes the persistent state represented by the given collection.
*
* @param PersistentCollection<array-key, object> $collection
Copy link
Member

Choose a reason for hiding this comment

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

Why's this necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think template parameters should always be specified, shouldn't they?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but you're narrowing the second type parameter from mixed to object.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't remember, and my commit message doesn't help, sorry.
When I try running Psalm v5 with all the errors baselined and then changing object to mixed, I get this error:

ERROR: InvalidArgument - lib/Doctrine/ORM/UnitOfWork.php:425:94 - Argument 1 of Doctrine\ORM\Persisters\Collection\CollectionPersister::delete expects Doctrine\ORM\PersistentCollection<array-key, mixed>, but Doctrine\ORM\PersistentCollection<array-key, object> provided (see https://psalm.dev/004)
                    $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete);

Maybe the right fix would be to use @template-covariant here… also, I wonder if we should use T of object for as a template for PersistentCollection… a PersistentCollection is always going to be a collection of entities, isn't it?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe the right fix would be to use @template-covariant here…

We can't for the same reasons we've introduced the ReadableCollection. 😕

also, I wonder if we should use T of object for as a template for PersistentCollection… a PersistentCollection is always going to be a collection of entities, isn't it?

Probably, but then again, do CollectionPersister implementations actually care about the type of the values?

Did you try to declare the methods as generic methods instead, like…

/**
 * @template TKey of array-key
 * @template TValue
 *
 * @param PersistentCollection<TKey, TValue> $collection
 *
 * @return void
 */
public function delete(PersistentCollection $collection);

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't for the same reasons we've introduced the ReadableCollection.

Ah yeah right, I should know 😓

Probably, but then again, do CollectionPersister implementations actually care about the type of the values?

I suppose they don't, since specifying that we have an object here does not improve the baseline more than silencing the aforementioned error.

Did you try to declare the methods as generic methods instead, like…

That appears to work, thanks a lot! I don't understand why we have this issue only with delete() though. I didn't notice the methods below before noticing the plural to "methods" in your previous message, and now I have mixed feeling about this… probably just going to drop that commit as well, so that we find a better solution later.

@derrabus derrabus merged commit 3038f6a into doctrine:2.13.x Nov 24, 2022
@greg0ire greg0ire deleted the psalm-5-fixes branch November 24, 2022 22:11
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.

None yet

2 participants