vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Allow method overloading at method level.

Open rzvc opened this issue 3 years ago • 6 comments

Currently overloading methods via @method docblock appears to only be supported if done in the class's docblock.

/**
 * @method void test()
 * @method void test(string $name)
*/
class Foo
{
	public function test(?string $name) : void
	{

	}
}

It would be good if the overloading could also be declared at method level:

class Foo
{
	/**
	 * @method void test()
	 * @method void test(string $name)
	*/
	public function test(?string $name) : void
	{

	}
}

rzvc avatar May 12 '22 06:05 rzvc

@method applies to Class only since it refers to its methods, why would you need this? 🤔 If you need to customize the method itself, you can just override its PHPDoc

danilopolani avatar May 17 '22 12:05 danilopolani

@danilopolani, I think the only reason @method works at class level is because it's meant to describe magic method (that aren't actually there, so you can't declare it in the method's docblock).

If you allow for method overloading, then there's no reason to declare the overloads anywhere else than where the main method is declared.

rzvc avatar May 17 '22 13:05 rzvc

If you allow for method overloading, then there's no reason to declare the overloads anywhere else than where the main method is declared.

But why can't you just do something like this? Maybe I'm misunderstanding your needs, sorry!

       /**
	 * @param string $name
	 * @return void
	 */
	public function test(?string $name) : void
	{

	}

danilopolani avatar May 17 '22 13:05 danilopolani

Ah, the example was oversimplified I think. I'm talking about having multiple signatures like this:

/**
 * @param string|mixed $key_or_value
 * @param ?mixed $value
 * @return void
 * @method void test(string $key, mixed $value)
 * @method void test(mixed $value)
*/
public function test($key_or_value, $value)
{
}

Or for return type overloading:

/**
 * @param Something|Other $value
 * @return Something[]|Other[]
 * @method Something[] test(Something $value)
 * @method Other[] test(Other $value)
*/
public function test($value)
{
}

rzvc avatar May 17 '22 13:05 rzvc

Oh got it! I have the suspect that the @method syntax on methods is not anyway accepted by static analysis tools such as PHPStan and Psalm, though.

Maybe it could be written with Generics/Union types, depending on the scenario? Just trying to figure out a solution that already works 😄 Hopefully a maintainer will answer in the near future

danilopolani avatar May 17 '22 13:05 danilopolani

Psalm doesn't support method overloading at the moment. Instead it relies on conditional return types, which gets you half way there. Full support seems to be on the roadmap tho.

Don't know about the other static analyzers.

rzvc avatar May 17 '22 13:05 rzvc

There's no plans to support @method annotations anywhere other than from within the class docblock.

bmewburn avatar Apr 07 '24 07:04 bmewburn