phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Return of mixed in `$this->createQueryBuilder()->getQuery()->getResult()` when alias is `ev` or `event`

Open ruudk opened this issue 1 year ago • 9 comments

I have no idea what this is, or what causes it.

 // This all works: list<Event>
dumpType($this->createQueryBuilder('e')->getQuery()->getResult());
dumpType($this->createQueryBuilder('my_name')->getQuery()->getResult());
dumpType($this->createQueryBuilder('oo')->getQuery()->getResult());
dumpType($this->createQueryBuilder('abcdef')->getQuery()->getResult());
dumpType($this->createQueryBuilder('user')->getQuery()->getResult());
dumpType($this->createQueryBuilder('av')->getQuery()->getResult());
dumpType($this->createQueryBuilder('eve')->getQuery()->getResult());
dumpType($this->createQueryBuilder('even')->getQuery()->getResult());

// This returns: mixed
dumpType($this->createQueryBuilder('ev')->getQuery()->getResult());
dumpType($this->createQueryBuilder('event')->getQuery()->getResult());

I also tried this on a different entity repository, for an entity named differently: Type. There, it gives the same problem.

So what is the cause for ev and event to behave weird?

ruudk avatar Sep 12 '24 14:09 ruudk

Replicated: https://github.com/phpstan/phpstan-doctrine/pull/609

janedbal avatar Sep 12 '24 14:09 janedbal

I was just using Xdebug to find the issue:

It's throwing here: https://github.com/phpstan/phpstan-doctrine/blob/be66017631e5289e8a3bd8973e59781642da40fd/src/Type/Doctrine/ArgumentsProcessor.php#L61-L66

ruudk avatar Sep 12 '24 14:09 ruudk

It seems that $className is the alias ev now: Screenshot 2024-09-12 at 16 39 15@2x

ruudk avatar Sep 12 '24 14:09 ruudk

Interesting: Screenshot 2024-09-12 at 16 40 16@2x It thinks this ConstantStringType is a ClassStringType

ruudk avatar Sep 12 '24 14:09 ruudk

It seems that I have a class called ev and event. The last one makes sense, somewhere. But the first one? Interesting.

https://github.com/phpstan/phpstan-src/blob/5480d27383ffee399037c31d461c5a85344128ca/src/Type/Constant/ConstantStringType.php#L83-L92

ruudk avatar Sep 12 '24 14:09 ruudk

Yes, because reflector see Event class as existing due to this stub:

  • phar:///app/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/SourceLocator/SourceStubber/../../../../../jetbrains/phpstorm-stubs/event/event.stub

image

janedbal avatar Sep 12 '24 14:09 janedbal

@ondrejmirtes added this condition here. I think we should be more careful where we do this check. Imo only in ->from(class-name) etc. Not in such a generic way for every argument.

janedbal avatar Sep 12 '24 14:09 janedbal

Yeah, it should throw only in arguments that expect class-string. Which is reasonable to assume in Doctrine that it must not be transient.

ondrejmirtes avatar Sep 12 '24 14:09 ondrejmirtes

I believe it would be the same issue as https://github.com/phpstan/phpstan-doctrine/issues/566

janedbal avatar Sep 12 '24 15:09 janedbal