phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

Support for Symfony HandleTrait

Open alexander-schranz opened this issue 4 years ago • 4 comments

I think it would be great if the extension could automatically recognize the type of HandleTrait of symfony.

The given message to the handle trait example:

$this->handle(new CreateUserMessage());

does only have 1 Handler which return type the return type of the handle trait will be. Example:

class CreateUserMessageHandler {
    public function __invoke(CreateUserMessage $message): User
    {
        return new User($message->getUserName());
    }
}

The bin/console debug:messenger already shows the mapping between Message and Handlers.

The HandleStamp contains under the getResult method the result of every handler: https://github.com/symfony/symfony/blob/8eed84bb2df613101949ca2e786ac383d2f349fd/src/Symfony/Component/Messenger/HandleTrait.php#L45. The HandleTrait only allows one handler per message, that make the result recognize able. Not sure if its even possible to overwrite the result of a trait. Sadly my skillset for providing this myself is to low, but would think it would be a great feature.

Currenty workaround is:

/** @var User */
$user = $this->handle(new CreateUserMessage());

The first parameter of console.command.messenger_debug service would contain the mapping between message and handler so we can guess the type from there.

alexander-schranz avatar Nov 11 '21 23:11 alexander-schranz

Did anybody take a look on that so far to give some insights? Today, I'm going to start working on this.

bnowak avatar Apr 05 '24 08:04 bnowak

So far, I researched that in current version not having of this feature request, it could be a blocker.

but I'm keep digging :wink:

bnowak avatar Apr 05 '24 13:04 bnowak

I'm happy to announce that PR for that is done and ready for review here :)

Feel free to review and give your feedback, thank you :)

bnowak avatar Oct 17 '24 14:10 bnowak

There's also another QueryBus-classes related topic which I wanted to cover with reusing this feature. It's described in this comment. If you have idea how to solve it, please share it here :) Thanks, again

bnowak avatar Oct 17 '24 14:10 bnowak

Implemented https://github.com/phpstan/phpstan-symfony/pull/404

ondrejmirtes avatar Jan 04 '25 13:01 ondrejmirtes

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Feb 05 '25 00:02 github-actions[bot]