Psalm: Feature request: public-method-string<O>

Created on 22 Dec 2019  路  4Comments  路  Source: vimeo/psalm

Could something like this be achieved?

/**
  * @template O of object
  * @psalm-param O $object
  * @psalm-param method-name<O> $method
  */
function invoke($object, $method) {
    $object->$method();
}

Most helpful comment

it would have to be something like public-method-string<O>, right?

All 4 comments

it would have to be something like public-method-string<O>, right?

Right!

@muglug referring you back to earlier this year:

<?php
declare(strict_types=1);

/**
* @template T
* 
* @param ReflectionClass<T> $class
*
* @return array<int, method-string<T>>
*/
function MethodNames(ReflectionClass $class, int $filter = null) : array {
    return array_map(
        /**
        * @return method-string<T>
        */
        function (ReflectionMethod $method) : string {
            return $method->name;
        },
        $class->getMethods($filter)
    );
}

Would also help to properly type EventSubscribers, e.g. the one from Symfony:

interface EventSubscriberInterface
{
    /**
      * @return list<class-string|string, public-method-string<static>|array{0: public-method-string, 1?: int}>
      */
    public static function getSubscribedEvents(): array;
}

class EventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [Event::class => 'onEvent', 'event_name' => ['onAnotherEvent', 100]]
    }

   public function onEvent() {}
   public function onAnotherEvent() {}
}
Was this page helpful?
0 / 5 - 0 ratings