nextcloud_composer icon indicating copy to clipboard operation
nextcloud_composer copied to clipboard

Incompatible types found for T

Open sualko opened this issue 4 years ago • 2 comments

I get an error from psalm regarding the event listener.

$context->registerEventListener(AddFeaturePolicyEvent::class, AddFeaturePolicyListener::class);

will produce

ERROR: InvalidArgument - lib/AppInfo/Application.php:60:13 - Incompatible types found for T (OCP\Security\FeaturePolicy\AddFeaturePolicyEvent is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)
		$context->registerEventListener(AddFeaturePolicyEvent::class, AddFeaturePolicyListener::class);

My listener looks like this:

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent;

class AddFeaturePolicyListener implements IEventListener
{
	public function handle(Event $event): void
	{
		if (!($event instanceof AddFeaturePolicyEvent)) {
			return;
		}

		//...
	}
}

Changing the $event type to AddFeaturePolicyEvent does not work. Do you have an idea how this can be resolved?

sualko avatar Jan 04 '22 14:01 sualko

Yeah, this is unfortunately known. We had a discussion and a few attempts of fixes at https://github.com/nextcloud/server/pull/28338. I think unless Psalm got wiser in the meantime we should possibly drop the generics from the listener. After all it's possibly to use one listener to react to more than one dynamic type.

ChristophWurst avatar Jan 26 '22 12:01 ChristophWurst

See https://github.com/vimeo/psalm/issues/7549

come-nc avatar Feb 14 '22 10:02 come-nc