Skip to content

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartłomiej Nowak committed Dec 23, 2024
1 parent d62ec03 commit 89c7534
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/tests/tmp
/build-cs
/vendor
/.idea
/composer.lock
.phpunit.result.cache
5 changes: 2 additions & 3 deletions src/Symfony/MessageMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
final class MessageMap
{

/** @var array<class-string, Type> */
/** @var array<string, Type> */
private $messageMap;

/** @param array<class-string, Type> $messageMap */
/** @param array<string, Type> $messageMap */
public function __construct(array $messageMap)
{
$this->messageMap = $messageMap;
}

/** @param class-string $class */
public function getTypeForClass(string $class): ?Type
{
return $this->messageMap[$class] ?? null;
Expand Down
49 changes: 32 additions & 17 deletions src/Symfony/MessageMapFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PHPStan\Symfony;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
Expand Down Expand Up @@ -47,9 +46,14 @@ public function create(): MessageMap
continue;
}

if (!$this->reflectionProvider->hasClass($serviceClass)) {
continue;
}

$reflectionClass = $this->reflectionProvider->getClass($serviceClass);

/** @var array{handles?: class-string, method?: string} $tagAttributes */
$tagAttributes = $tag->getAttributes();
$reflectionClass = $this->reflectionProvider->getClass($serviceClass);

if (isset($tagAttributes['handles'])) {
$handles = [$tagAttributes['handles'] => ['method' => $tagAttributes['method'] ?? self::DEFAULT_HANDLER_METHOD]];
Expand All @@ -58,7 +62,13 @@ public function create(): MessageMap
}

foreach ($handles as $messageClassName => $options) {
$methodReflection = $reflectionClass->getNativeMethod($options['method'] ?? self::DEFAULT_HANDLER_METHOD);
$methodName = $options['method'] ?? self::DEFAULT_HANDLER_METHOD;

if (!$reflectionClass->hasNativeMethod($methodName)) {
continue;
}

$methodReflection = $reflectionClass->getNativeMethod($methodName);

foreach ($methodReflection->getVariants() as $variant) {
$returnTypesMap[$messageClassName][] = $variant->getReturnType();
Expand All @@ -79,27 +89,33 @@ public function create(): MessageMap
return new MessageMap($messageMap);
}

/** @return array<class-string, array<string, string>> */
/** @return iterable<string, array<string, string>> */
private function guessHandledMessages(ClassReflection $reflectionClass): iterable
{
if ($reflectionClass->implementsInterface(MessageSubscriberInterface::class)) {
foreach ($reflectionClass->getName()::getHandledMessages() as $index => $value) {
if (self::containOptions($index, $value)) {
yield $index => $value;
} else {
yield $value => ['method' => self::DEFAULT_HANDLER_METHOD];
$className = $reflectionClass->getName();

foreach ($className::getHandledMessages() as $index => $value) {
try {
if (self::containOptions($index, $value)) {
yield $index => $value;
} else {
yield $value => ['method' => self::DEFAULT_HANDLER_METHOD];
}
} catch (ShouldNotHappenException $e) {
continue;
}
}

return;
}

try {
$methodReflection = $reflectionClass->getNativeMethod(self::DEFAULT_HANDLER_METHOD);
} catch (MissingMethodFromReflectionException $e) {
if (!$reflectionClass->hasNativeMethod(self::DEFAULT_HANDLER_METHOD)) {
return;
}

$methodReflection = $reflectionClass->getNativeMethod(self::DEFAULT_HANDLER_METHOD);

$variants = $methodReflection->getVariants();
if (count($variants) !== 1) {
return;
Expand All @@ -111,7 +127,6 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
return;
}

/** @var class-string[] $classNames */
$classNames = $parameters[0]->getType()->getObjectClassNames();

if (count($classNames) !== 1) {
Expand All @@ -124,10 +139,10 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
/**
* @param mixed $index
* @param mixed $value
* @phpstan-assert-if-true class-string $index
* @phpstan-assert-if-true array<string, mixed> $value
* @phpstan-assert-if-false int $index
* @phpstan-assert-if-false class-string $value
* @phpstan-assert-if-true =class-string $index
* @phpstan-assert-if-true =array<string, mixed> $value
* @phpstan-assert-if-false =int $index
* @phpstan-assert-if-false =class-string $value
*/
private static function containOptions($index, $value): bool
{
Expand Down
16 changes: 8 additions & 8 deletions src/Type/Symfony/MessengerHandleTraitReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Symfony\MessageMapFactory;
use PHPStan\Type\ExpressionTypeResolverExtension;
use PHPStan\Type\Type;
use ReflectionException;
use function count;
use function is_null;

Expand All @@ -35,7 +34,6 @@ public function getType(Expr $expr, Scope $scope): ?Type
{
if ($this->isSupported($expr, $scope)) {
$arg = $expr->getArgs()[0]->value;
/** @var class-string[] $argClassNames */
$argClassNames = $scope->getType($arg)->getObjectClassNames();

if (count($argClassNames) === 1) {
Expand All @@ -61,7 +59,7 @@ private function getMessageMap(): MessageMap
}

/**
* @phpstan-assert-if-true MethodCall $expr
* @phpstan-assert-if-true =MethodCall $expr
*/
private function isSupported(Expr $expr, Scope $scope): bool
{
Expand All @@ -73,14 +71,16 @@ private function isSupported(Expr $expr, Scope $scope): bool
return false;
}

try {
$methodReflection = $scope->getClassReflection()->getNativeReflection()->getMethod(self::TRAIT_METHOD_NAME);
$declaringClassReflection = $methodReflection->getBetterReflection()->getDeclaringClass();
$reflectionClass = $scope->getClassReflection()->getNativeReflection();

return $declaringClassReflection->getName() === self::TRAIT_NAME;
} catch (ReflectionException $e) {
if (!$reflectionClass->hasMethod(self::TRAIT_METHOD_NAME)) {
return false;
}

$methodReflection = $reflectionClass->getMethod(self::TRAIT_METHOD_NAME);
$declaringClassReflection = $methodReflection->getBetterReflection()->getDeclaringClass();

return $declaringClassReflection->getName() === self::TRAIT_NAME;
}

}

0 comments on commit 89c7534

Please sign in to comment.