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

Update 2020-03-23-doctrine-entity-typed-properties-with-php74.md #1065

Merged
merged 1 commit into from
Oct 13, 2020
Merged

Update 2020-03-23-doctrine-entity-typed-properties-with-php74.md #1065

merged 1 commit into from
Oct 13, 2020

Conversation

ThomasLandauer
Copy link
Contributor

With @var Collection<Trainer>, PhpStan gives me (on level 6) - see https://stackoverflow.com/a/64242837/1668200

Property ... with generic interface Doctrine\Common\Collections\Collection does not specify its types: TKey, T

Question/suggestion: Why do you suggest Collection here instead of ArrayCollection? What's the difference?

With `@var Collection<Trainer>`, PhpStan gives me (on level 6) - see https://stackoverflow.com/a/64242837/1668200
> Property ... with generic interface Doctrine\Common\Collections\Collection does not specify its types: TKey, T
@TomasVotruba
Copy link
Owner

TomasVotruba commented Oct 12, 2020

Hi, thanks for PR. Beware, PHPStan is not always right. It's still an assumption tool.

How do you know there is int key?

Question/suggestion: Why do you suggest Collection here instead of ArrayCollection? What's the difference?

Because there can be any class of Collection interface. ArrayCollection is just one of them

@ThomasLandauer
Copy link
Contributor Author

How do you know there is int key?

By dumping it - see the stackoverflow link above. However, I didn't double check Doctrine's source code ;-)

Because there can be any class of Collection interface. ArrayCollection is just one of them

Well, there could be any class, but in fact it is ArrayCollection - isn't it?

Other question: The next thing that's going to happen in this regard is the switch from today's annotations to PHP8's attributes - do you agree? If yes: When people are updating all their entities to PHP7.4's types, is there anything they could do to "prepare" for PHP8?

@TomasVotruba
Copy link
Owner

I see. Thanks for explaining.
I thought collections are like arrays, so the have int keys by default.

Let's ship it then.

@TomasVotruba TomasVotruba merged commit 96d2b42 into TomasVotruba:master Oct 13, 2020
@TomasVotruba
Copy link
Owner

Well, there could be any class, but in fact it is ArrayCollection - isn't it?

I'd go with SOLID dependency inversion principle here.
But I'm not Doctrine expert, it's just common sense.

@TomasVotruba
Copy link
Owner

As for prepare for PHP 8, I'd use all the Rector sets from 5.6 to 7.4 and put them in CI.

@ThomasLandauer
Copy link
Contributor Author

ThomasLandauer commented Oct 13, 2020

I'd go with SOLID dependency inversion principle here.
But I'm not Doctrine expert, it's just common sense.

Well, symfony/maker-bundle v1.21.1 sees it like you :-) It produces:

/**
 * @return Collection|Bar[]
 */
public function getBars(): Collection
{
    return $this->bars;
}

@ThomasLandauer ThomasLandauer deleted the patch-2 branch October 13, 2020 11:16
@TomasVotruba
Copy link
Owner

😃

That's actually incorrect, as array of Bar[] is not a Collection object.

@TomasVotruba
Copy link
Owner

TomasVotruba commented Oct 18, 2020

To my last comment... I see the type must be technically invalid, so it works in both PHPStorm and PHPStan

Saying that, you might like this @rectorphp rule... 😃
rectorphp/rector#4442

@ThomasLandauer
Copy link
Contributor Author

Sorry, haven't ever used rector, so I can't say something here ;-)

@TomasVotruba
Copy link
Owner

TomasVotruba commented Oct 18, 2020

No problem. This is the important part:

image

Basically what your pull-request was about, just automated :)

@TomasVotruba
Copy link
Owner

image

@ThomasLandauer
Copy link
Contributor Author

Great :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants