Hi,
<?php
interface InterfaceA
{
}
class ImplemX implements InterfaceA
{
public function getID(): int
{
return 12;
}
}
/**
* @template T of InterfaceA
*/
interface DoStuff
{
/**
* @psalm-param T $object
*/
public function stuff(InterfaceA $object): void;
}
/**
* @template-implements DoStuff<ImplemX>
*/
final class DoStuffButChangeTemplatedParameterName implements DoStuff
{
public function stuff(InterfaceA $object_1) : void
{
echo $object_1->getID(); // UndefinedInterfaceMethod - 33:25 - Method InterfaceA::getid does not exist
}
}
/**
* @template-implements DoStuff<ImplemX>
*/
final class DoStuffKeepTemplatedParameterName implements DoStuff
{
public function stuff(InterfaceA $object) : void
{
echo $object->getID();
}
}
https://psalm.dev/r/0ac808b64d
This can be confusing when you encounter the issue because it looks like the templating does not work at all. Also this force some semantic coupling that should probably not be enforced.
This isn't related to templates - it happens without them, too:
interface InterfaceA {}
class ImplemX implements InterfaceA {
public function getID(): int {
return 12;
}
}
interface DoStuff {
/** @param ImplemX $object */
public function stuff(InterfaceA $object): void;
}
class DoStuffButChangeParameterName implements DoStuff {
public function stuff(InterfaceA $object_1) : void {
// doesn’t work
echo $object_1->getID();
}
}
class DoStuffImplementer implements DoStuff {
public function stuff(InterfaceA $object) : void {
// works
echo $object->getID();
}
}
https://psalm.dev/r/3d27a47ae2
I agree it can be a little confusing, but it’s not clear that users want to inherit docblock params if they also change the name.
Yup I did see that but while it feels quite natural for docblock not using
template information it feels a bit more confusing when templates are
involved (at least to me).
Template annotations are a way to deal with generics and people used to
generic in other languages might not expect this limitation. Also the
@template-inherits and @template-extends works with the information
collected from the class upper in the hierarchy which besides the explicit
@inheritDoc is already quite unique (unless I'm mistaken or not aware of
something) so not having direct inheritance like other docblocks would not
be surprising.
Anyway, I'm completely fine with the won't fix, would you be open to a PR
adding a quick note in the documentation about this?
Thanks for your awesome work!
Le mar. 3 déc. 2019 à 19:34, Matthew Brown notifications@github.com a
écrit :
This isn't related to templates - it happens without them, too:
interface InterfaceA {}
class ImplemX implements InterfaceA {
public function getID(): int {
return 12;
}
}interface DoStuff {
/** @param ImplemX $object */
public function stuff(InterfaceA $object): void;
}class DoStuffButChangeParameterName implements DoStuff {
public function stuff(InterfaceA $object_1) : void {
echo $object_1->getID();
}
}class DoStuffImplementer implements DoStuff {
public function stuff(InterfaceA $object) : void {
echo $object->getID();
}
}https://psalm.dev/r/3d27a47ae2
I agree it can be a little confusing, but it’s not clear that users want
to inherit docblock params if they also change the name.—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/vimeo/psalm/issues/2407?email_source=notifications&email_token=AAFUDZ3OJ5SECBEBT2ARGCTQW2RDDA5CNFSM4JUYSVK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEF2LYKI#issuecomment-561298473,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAFUDZ5A6ZNZVH5NMGWVZFDQW2RDDANCNFSM4JUYSVKQ
.
I'm always happy for better documentation!