bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.62k stars 94 forks source link

Template return is not prioritized over mixed #3032

Closed jamesdettmartin closed 1 month ago

jamesdettmartin commented 2 months ago

Describe the bug When a method is described to @return mixed|T, it is always being interpreted as mixed instead of the class-string being passed in, not providing autocomplete. This was working previously, as many projects I'm working on utilize php-di and the get method was consistently giving me the class passed in, providing useful autocompletes. Within the past month, it stopped working and now every variable from the container is detected as "mixed" and nullifies the benefits of Intelephense's autocomplete features. This is working as intended in the 1.10.4 version

To Reproduce

<?php

/**
 * Simulate dependency container
 *
 * @template T
 * @param string|class-string<T> $class
 * @return mixed|T
 */
function createInstance(string $class)
{
    return new $class();
}

class Car {
  public function getName();
}

$car = createInstance(Car::class);

Expected behavior The variable should be detected as the template class for autocompletes

Screenshots 1.12.4 image 1.10.4 image

Platform and version WSL: Ubuntu-20.04, Intelephense 1.12.4

bmewburn commented 2 months ago

A workaround would be to add your own stub for that method with mixed removed. The template can't be prioritised over mixed as mixed is the super type in the union. 1.10.4 may not have collapsed redundant types as eagerly which is why it worked differently. I'll see if the previous behaviour can be reinstated but the real problem is typing something as mixed|T defeats the purpose of using templates.