Skip to content

Commit 46e0cc8

Browse files
Bartłomiej Nowakbnowak
Bartłomiej Nowak
authored andcommitted
get rid of todo comments in \PHPStan\Symfony\MessageMapFactory::guessHandledMessages
1 parent 8a44be8 commit 46e0cc8

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/Symfony/MessageMapFactory.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
namespace PHPStan\Symfony;
44

55
use PHPStan\Reflection\ClassReflection;
6+
use PHPStan\Reflection\MissingMethodFromReflectionException;
67
use PHPStan\Reflection\ParametersAcceptorSelector;
78
use PHPStan\Reflection\ReflectionProvider;
8-
use RuntimeException;
99
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
1010
use function count;
1111
use function is_int;
1212
use function is_null;
1313
use function is_string;
1414

15-
// todo add tests
1615
final class MessageMapFactory
1716
{
1817

@@ -94,23 +93,30 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
9493
return;
9594
}
9695

97-
// todo handle if doesn't exists
98-
$methodReflection = $reflectionClass->getNativeMethod(self::DEFAULT_HANDLER_METHOD);
96+
try {
97+
$methodReflection = $reflectionClass->getNativeMethod(self::DEFAULT_HANDLER_METHOD);
98+
} catch (MissingMethodFromReflectionException $e) {
99+
return;
100+
}
101+
102+
$variants = $methodReflection->getVariants();
103+
if (count($variants) !== 1) {
104+
return;
105+
}
99106

100-
$variant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
101-
$parameters = $variant->getParameters();
107+
$parameters = $variants[0]->getParameters();
102108

103109
if (count($parameters) !== 1) {
104-
// todo handle error
105-
throw new RuntimeException('invalid handler');
110+
return;
106111
}
107112

108-
$type = $parameters[0]->getType();
113+
$classNames = $parameters[0]->getType()->getObjectClassNames();
109114

110-
// todo many class names?
111-
foreach ($type->getObjectClassNames() as $className) {
112-
yield $className => ['method' => self::DEFAULT_HANDLER_METHOD];
115+
if (count($classNames) !== 1) {
116+
return;
113117
}
118+
119+
yield $classNames[0] => ['method' => self::DEFAULT_HANDLER_METHOD];
114120
}
115121

116122
}

src/Type/Symfony/MessengerHandleTraitReturnTypeExtension.php

-9
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ public function __construct(MessageMapFactory $symfonyMessageMapFactory)
3333

3434
public function getType(Expr $expr, Scope $scope): ?Type
3535
{
36-
// todo handle different cases:
37-
// - [X] regular message classes
38-
// - [ ] interfaces for message classes
39-
// - [ ] many handlers for one message? it would throw exception in HandleTrait anyway
40-
// - [x] many messages for one handler
41-
// - [partially] cover MessageSubscriberInterface
42-
// - [partially] custom method names for handlers (different than default "__invoke" magic method)
43-
// - [] read SF doc to determine any other cases to covers
44-
4536
if ($this->isSupported($expr, $scope)) {
4637
$arg = $expr->getArgs()[0]->value;
4738
$argClassNames = $scope->getType($arg)->getObjectClassNames();

0 commit comments

Comments
 (0)