Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4481284

Browse files
author
Bartłomiej Nowak
committed
updated README.md
1 parent 3f589fd commit 4481284

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

‎README.md‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ This extension provides following features:
2121
* Provides correct return type for `InputBag::get()` method based on the `$default` parameter.
2222
* Provides correct return type for `InputBag::all()` method based on the `$key` parameter.
2323
* Provides correct return types for `TreeBuilder` and `NodeDefinition` objects.
24+
* Provides correct return type for Messenger `HandleTrait::handle()` method based on the message type.
25+
* Provides configurable return type resolution for methods that internally use Messenger `HandleTrait`.
2426
* Notifies you when you try to get an unregistered service from the container.
2527
* Notifies you when you try to get a private service from the container.
2628
* Optionally correct return types for `InputInterface::getArgument()`, `::getOption`, `::hasArgument`, and `::hasOption`.
@@ -168,3 +170,94 @@ Call the new env in your `console-application.php`:
168170
```php
169171
$kernel = new \App\Kernel('phpstan_env', (bool) $_SERVER['APP_DEBUG']);
170172
```
173+
174+
## Messenger HandleTrait Wrappers
175+
176+
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.
177+
178+
### Configuration
179+
180+
```neon
181+
parameters:
182+
symfony:
183+
messenger:
184+
handleTraitWrappers:
185+
- App\Bus\QueryBus::dispatch
186+
- App\Bus\QueryBus::execute
187+
- App\Bus\QueryBusInterface::dispatch
188+
```
189+
190+
### Message Handlers
191+
192+
```php
193+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
194+
195+
// Product handler that returns Product
196+
#[AsMessageHandler]
197+
class GetProductQueryHandler
198+
{
199+
public function __invoke(GetProductQuery $query): Product
200+
{
201+
return $this->productRepository->get($query->productId);
202+
}
203+
}
204+
```
205+
206+
### PHP Examples
207+
208+
```php
209+
use Symfony\Component\Messenger\HandleTrait;
210+
use Symfony\Component\Messenger\MessageBusInterface;
211+
212+
// Basic query bus implementation
213+
class QueryBus
214+
{
215+
use HandleTrait;
216+
217+
public function __construct(MessageBusInterface $messageBus)
218+
{
219+
$this->messageBus = $messageBus;
220+
}
221+
222+
public function dispatch(object $query): mixed
223+
{
224+
return $this->handle($query); // Return type will be inferred
225+
}
226+
227+
// Multiple methods per class example
228+
public function execute(object $message): mixed
229+
{
230+
return $this->handle($message);
231+
}
232+
}
233+
234+
// Interface-based configuration example
235+
interface QueryBusInterface
236+
{
237+
public function dispatch(object $query): mixed;
238+
}
239+
240+
class QueryBusWithInterface implements QueryBusInterface
241+
{
242+
use HandleTrait;
243+
244+
public function __construct(MessageBusInterface $queryBus)
245+
{
246+
$this->messageBus = $queryBus;
247+
}
248+
249+
public function dispatch(object $query): mixed
250+
{
251+
return $this->handle($query);
252+
}
253+
}
254+
255+
// Usage examples with proper type inference
256+
$query = new GetProductQuery($productId);
257+
$queryBus = new QueryBus($messageBus);
258+
$queryBusWithInterface = new QueryBusWithInterface($messageBus);
259+
260+
$product = $queryBus->dispatch($query); // Returns: Product
261+
$product2 = $queryBus->execute($query); // Returns: Product
262+
$product3 = $queryBusWithInterface->dispatch($query); // Returns: Product
263+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /