• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nepada / phpstan-message-bus / 13093006272

01 Feb 2025 10:24PM UTC coverage: 84.848%. Remained the same
13093006272

push

github

web-flow
Bump the phpstan group with 3 updates (#92)

56 of 66 relevant lines covered (84.85%)

1.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/src/MessageBus/CommandBusDynamicMethodThrowTypeExtension.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\PHPStan\MessageBus;
5

6
use Nepada\MessageBus\Commands\Command;
7
use Nepada\MessageBus\Commands\CommandBus;
8
use PhpParser\Node\Expr\MethodCall;
9
use PHPStan\Analyser\Scope;
10
use PHPStan\Reflection\MethodReflection;
11
use PHPStan\Reflection\ReflectionProvider;
12
use PHPStan\Type\DynamicMethodThrowTypeExtension;
13
use PHPStan\Type\Type;
14
use PHPStan\Type\TypeCombinator;
15

16
class CommandBusDynamicMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension
17
{
18

19
    private CommandTypeExtractor $commandTypeExtractor;
20

21
    private CommandHandlerResolver $commandHandlerResolver;
22

23
    private ReflectionProvider $reflectionProvider;
24

25
    public function __construct(CommandTypeExtractor $commandTypeExtractor, CommandHandlerResolver $commandHandlerResolver, ReflectionProvider $reflectionProvider)
26
    {
27
        $this->commandTypeExtractor = $commandTypeExtractor;
1✔
28
        $this->commandHandlerResolver = $commandHandlerResolver;
1✔
29
        $this->reflectionProvider = $reflectionProvider;
1✔
30
    }
31

32
    public function isMethodSupported(MethodReflection $methodReflection): bool
33
    {
34
        if (! $methodReflection->getDeclaringClass()->is(CommandBus::class)) {
2✔
35
            return false;
×
36
        }
37

38
        return $methodReflection->getName() === 'handle';
2✔
39
    }
40

41
    public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
42
    {
43
        $throwTypes = [];
2✔
44

45
        $commandBusThrowType = $methodReflection->getThrowType();
2✔
46
        if ($commandBusThrowType !== null && ! $commandBusThrowType->isVoid()->yes()) {
2✔
47
            $throwTypes[] = $commandBusThrowType;
2✔
48
        }
49

50
        $commandTypes = $this->commandTypeExtractor->extractCommandType($methodCall, $scope);
2✔
51
        foreach ($commandTypes as $commandType) {
2✔
52
            if ($commandType === Command::class) {
2✔
53
                continue;
×
54
            }
55
            $throwTypes = [
2✔
56
                ...$throwTypes,
2✔
57
                ...$this->getHandlerThrowTypes($commandType, $scope),
2✔
58
            ];
2✔
59
        }
60

61
        if (count($throwTypes) === 0) {
2✔
62
            return null;
×
63
        }
64

65
        return TypeCombinator::union(...$throwTypes);
2✔
66
    }
67

68
    /**
69
     * @return list<Type>
70
     */
71
    private function getHandlerThrowTypes(string $commandType, Scope $scope): array
72
    {
73
        $throwTypes = [];
2✔
74

75
        foreach ($this->commandHandlerResolver->getHandlerClasses($commandType) as $handlerType) {
2✔
76
            if (! $this->reflectionProvider->hasClass($handlerType)) {
2✔
77
                continue;
×
78
            }
79
            $handlerReflection = $this->reflectionProvider->getClass($handlerType);
2✔
80
            if (! $handlerReflection->hasMethod('__invoke')) {
2✔
81
                continue;
×
82
            }
83
            $handlerThrowType = $handlerReflection->getMethod('__invoke', $scope)->getThrowType();
2✔
84
            if ($handlerThrowType === null || $handlerThrowType->isVoid()->yes()) {
2✔
85
                continue;
×
86
            }
87
            $throwTypes[] = $handlerThrowType;
2✔
88
        }
89

90
        return $throwTypes;
2✔
91
    }
92

93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc