Skip to content

Commit 86e4507

Browse files
Bartłomiej Nowakbnowak
authored andcommitted
updated README.md
1 parent 452e3da commit 86e4507

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This extension provides following features:
2929
* Provides correct return type for `Extension::getConfiguration()` method.
3030
* Provides correct return type for `CacheInterface::get()` method based on the callback return type.
3131
* Provides correct return type for `BrowserKitAssertionsTrait::getClient()` method.
32+
* Provides configurable return type resolution for methods that internally use Messenger `HandleTrait`.
3233
* Notifies you when you try to get an unregistered service from the container.
3334
* Notifies you when you try to get a private service from the container.
3435
* Notifies you when you access undefined console command arguments or options.
@@ -180,3 +181,94 @@ Call the new env in your `console-application.php`:
180181
```php
181182
$kernel = new \App\Kernel('phpstan_env', (bool) $_SERVER['APP_DEBUG']);
182183
```
184+
185+
## Messenger HandleTrait Wrappers
186+
187+
The extension provides advanced type inference for methods that internally use Symfony Messenger's `HandleTrait`. This feature is particularly useful for query bus implementations (in CQRS pattern) that use/wrap the `HandleTrait::handle()` method.
188+
189+
### Configuration
190+
191+
```neon
192+
parameters:
193+
symfony:
194+
messenger:
195+
handleTraitWrappers:
196+
- App\Bus\QueryBus::dispatch
197+
- App\Bus\QueryBus::execute
198+
- App\Bus\QueryBusInterface::dispatch
199+
```
200+
201+
### Message Handlers
202+
203+
```php
204+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
205+
206+
// Product handler that returns Product
207+
#[AsMessageHandler]
208+
class GetProductQueryHandler
209+
{
210+
public function __invoke(GetProductQuery $query): Product
211+
{
212+
return $this->productRepository->get($query->productId);
213+
}
214+
}
215+
```
216+
217+
### PHP Examples
218+
219+
```php
220+
use Symfony\Component\Messenger\HandleTrait;
221+
use Symfony\Component\Messenger\MessageBusInterface;
222+
223+
// Basic query bus implementation
224+
class QueryBus
225+
{
226+
use HandleTrait;
227+
228+
public function __construct(MessageBusInterface $messageBus)
229+
{
230+
$this->messageBus = $messageBus;
231+
}
232+
233+
public function dispatch(object $query): mixed
234+
{
235+
return $this->handle($query); // Return type will be inferred
236+
}
237+
238+
// Multiple methods per class example
239+
public function execute(object $message): mixed
240+
{
241+
return $this->handle($message);
242+
}
243+
}
244+
245+
// Interface-based configuration example
246+
interface QueryBusInterface
247+
{
248+
public function dispatch(object $query): mixed;
249+
}
250+
251+
class QueryBusWithInterface implements QueryBusInterface
252+
{
253+
use HandleTrait;
254+
255+
public function __construct(MessageBusInterface $queryBus)
256+
{
257+
$this->messageBus = $queryBus;
258+
}
259+
260+
public function dispatch(object $query): mixed
261+
{
262+
return $this->handle($query);
263+
}
264+
}
265+
266+
// Usage examples with proper type inference
267+
$query = new GetProductQuery($productId);
268+
$queryBus = new QueryBus($messageBus);
269+
$queryBusWithInterface = new QueryBusWithInterface($messageBus);
270+
271+
$product = $queryBus->dispatch($query); // Returns: Product
272+
$product2 = $queryBus->execute($query); // Returns: Product
273+
$product3 = $queryBusWithInterface->dispatch($query); // Returns: Product
274+
```

0 commit comments

Comments
 (0)