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

Fix templated phpdoc return type #10849

Closed
wants to merge 4 commits into from

Conversation

mvorisek
Copy link
Contributor

@mvorisek mvorisek commented Jul 23, 2023

When split between @param and @psalm-param, only the @psalm-param is used by psalm/phpstan, ie class-string<T> is used instead of (too wide) class|class-string<T>.

In the 2nd commit, I fixed all other simillar problems in the whole repo. I used @.*(string.*\|class-s|class-string.*\|.*string) regex to find the lines to check/fix.

@mvorisek mvorisek marked this pull request as draft July 23, 2023 11:53
@mvorisek mvorisek marked this pull request as ready for review July 23, 2023 12:00
@mvorisek
Copy link
Contributor Author

I regenerated the psalm baseline locally, but the CI is still failing. Please regenerate it before merge.

@derrabus
Copy link
Member

I don't believe that this change is correct. Can you elaborate what issue you're trying to fix here?

@mvorisek
Copy link
Contributor Author

mvorisek commented Jul 23, 2023

phpstan cannot otherwise infer the resulting type for EntityManagerInterface::getClassMetadata() method, see the no longer needed baseline ignore [1]

@derrabus why do you think this change is not correct?

see other methods phpdocs in EntityManagerInterface class, they are all, except this one, in the form I fixed this one to

[1] and in my usecase, I needed to add Unable to resolve the template type T in call to method static method Doctrine\\Common\\Util\\ClassUtils::getClass\(\) phpstan ignore /wo this PR

@derrabus
Copy link
Member

see the no longer needed baseline ignore
[…]
why do you think this change is not correct?

See all the new Psalm errors that pop up after your change. 🙂

@mvorisek
Copy link
Contributor Author

see the no longer needed baseline ignore
[…]
why do you think this change is not correct?

See all the new Psalm errors that pop up after your change. 🙂

@derrabus looking into the psalm errors -> code: https://github.com/doctrine/orm/blob/2.15.4/lib/Doctrine/ORM/Cache.php#L42 it seems the are many places that accept a class name but are annotated as string instead of class-string.

I am not paid to maintain this project so I would be happy if this PR can be accepted and the psalm baseline regenerated (I tried myself on Windows, but the updated baseline did not fix all errors).

@derrabus
Copy link
Member

I am not paid to maintain this project

Neither am I. 🤷🏻‍♂️

I would be happy if this PR can be accepted

I cannot merge a PR with a red CI and I won't fix other people's PRs in my freetime.

@SenseException
Copy link
Member

SenseException commented Jul 24, 2023

@mvorisek What kind of problems do you have that you want to fix with this PR? Your last comment doesn't sound like that this is relevant to your daily work with ORM but more of an aid. We appreciate the effort people put into PRs as users (hopefully) appreciate the time we spent into the Doctrine project repositories on a regular basis.

We would like to know how this should be handled, because, as @derrabus already mentioned, this would rather be a hinderance to our also unpaid freetime if the failed build doesn't get fixed. I'd like to ask you to go the second half of what you've started.

Copy link
Member

@greg0ire greg0ire left a comment

Choose a reason for hiding this comment

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

All 4 of your commit messages are unhelpful, or sometimes even plain wrong, it seems. As I probably have already told you in the past, you should read the contribution guidelines to figure out what to write, they're fairly extensive on the topic.

As for the the CI build, if you want to contribute to Doctrine, you should setup a proper development environment.

@@ -335,7 +335,8 @@ public function hasFilters();
/**
* {@inheritDoc}
*
* @psalm-param string|class-string<T> $className
* @param string $className
* @psalm-param class-string<T> $className
Copy link
Member

Choose a reason for hiding this comment

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

EntityManager::getClassMetadata() also supports aliased names and needs to do so until Persistence 2 support is removed:

* The class name must be the fully-qualified class name without a leading backslash
* (as it is returned by get_class($obj)) or an aliased class name.
*
* Examples:
* MyProject\Domain\User
* sales:PriceRequest
*
* Internal note: Performance-sensitive method.
*
* {@inheritDoc}
*/
public function getClassMetadata($className)

Should the same be expected of any EntityManagerInterface implementation? If yes I'd say you're not so much fixing stuff as you are breaking it.

Fix templated phpdoc return type

Did you mean to write "parameter type" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@greg0ire see #10849 (comment) for repro, I meant "return type", as the "return type" is currently not correctly narrowed as the "parameter type" does allow any string

Copy link
Member

Choose a reason for hiding this comment

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

Related: #10853

Copy link
Contributor

Choose a reason for hiding this comment

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

* @param string $className
     * @psalm-param class-string<T> $className

Might be a good way to deprecate string param for static analysis user
and solve phpstan/phpstan#5175

@mvorisek
Copy link
Contributor Author

What kind of problems do you have that you want to fix with this PR?

here is a phpstan repro: https://phpstan.org/r/b2cbe847-a748-4094-bc49-44bd3e200e12 explaining the need

notice the dumped type change when l6 is changed to @psalm-param class-string<T> $className

@derrabus
Copy link
Member

This function accepts strings that are not FQCN, so changing the parameter type to class-string would be wrong. But if we a class-string<SomeClass> is passed, the return type to expect is ClassMetadata<SomeClass>.

Can we solve this with a conditional return type?

@VincentLanglet
Copy link
Contributor

This function accepts strings that are not FQCN, so changing the parameter type to class-string would be wrong. But if we a class-string<SomeClass> is passed, the return type to expect is ClassMetadata<SomeClass>.

Can we solve this with a conditional return type?

There was an issue about this in phpstan
phpstan/phpstan#5175

@mvorisek
Copy link
Contributor Author

Closing as it seems this change is not wanted, at least for ORM 2.x.

@mvorisek mvorisek closed this Mar 31, 2024
@mvorisek mvorisek deleted the minor_phpdoc_fix branch March 31, 2024 12:30
@VincentLanglet
Copy link
Contributor

Closing as it seems this change is not wanted, at least for ORM 2.x.

I think the string support was deprecated so maybe the issue does not exist in 3.x anymore (or the phpdoc could be updated).

Need to check

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.

5 participants