• 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

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

4
namespace Nepada\PHPStan\MessageBus;
5

6
use Nepada\MessageBus\Commands\CommandHandler;
7
use Nepada\MessageBus\StaticAnalysis\HandlerType;
8
use Nepada\MessageBus\StaticAnalysis\MessageTypeExtractor;
9
use Nepada\MessageBus\StaticAnalysis\ReflectionHelper;
10
use Nepada\MessageBus\StaticAnalysis\StaticAnalysisFailedException;
11
use Nette\Loaders\RobotLoader;
12

13
class CommandHandlerResolver
14
{
15

16
    /**
17
     * @var array<string>
18
     */
19
    private array $scanDirectories;
20

21
    /**
22
     * @var array<class-string<CommandHandler>, class-string>|null
23
     */
24
    private ?array $commandTypeByHandlerType = null;
25

26
    private MessageTypeExtractor $messageTypeExtractor;
27

28
    /**
29
     * @param array<string> $scanDirectories
30
     */
31
    public function __construct(array $scanDirectories, MessageTypeExtractor $messageTypeExtractor)
32
    {
33
        $this->scanDirectories = $scanDirectories;
1✔
34
        $this->messageTypeExtractor = $messageTypeExtractor;
1✔
35
    }
36

37
    /**
38
     * @return list<string>
39
     */
40
    public function getHandlerClasses(string $handledCommandType): array
41
    {
42
        $handlers = [];
2✔
43
        foreach ($this->getCommandTypeByHandlerType() as $handlerType => $commandType) {
2✔
44
            if (is_a($commandType, $handledCommandType, true)) {
2✔
45
                $handlers[] = $handlerType;
2✔
46
            }
47
        }
48

49
        return $handlers;
2✔
50
    }
51

52
    /**
53
     * @return array<class-string<CommandHandler>, class-string>
54
     */
55
    private function getCommandTypeByHandlerType(): array
56
    {
57
        if ($this->commandTypeByHandlerType !== null) {
2✔
58
            return $this->commandTypeByHandlerType;
2✔
59
        }
60

61
        $this->commandTypeByHandlerType = [];
1✔
62
        /** @var class-string $class */
63
        foreach ($this->getAllClasses() as $class) {
1✔
64
            if (! is_a($class, CommandHandler::class, true)) {
1✔
65
                continue;
1✔
66
            }
67

68
            try {
69
                $handlerReflection = ReflectionHelper::requireClassReflection($class);
1✔
70
                if ($handlerReflection->isTrait() || $handlerReflection->isInterface() || $handlerReflection->isAbstract()) {
1✔
71
                    continue;
×
72
                }
73

74
                $messageType = $this->messageTypeExtractor->extract(HandlerType::fromString($class));
1✔
75
                $this->commandTypeByHandlerType[$class] = $messageType->toString();
1✔
76

77
            } catch (StaticAnalysisFailedException $exception) {
×
78
                // noop
79
            }
80
        }
81

82
        return $this->commandTypeByHandlerType;
1✔
83
    }
84

85
    /**
86
     * @return list<string>
87
     */
88
    private function getAllClasses(): array
89
    {
90
        $loader = $this->createRobotLoader();
1✔
91

92
        return array_keys($loader->getIndexedClasses());
1✔
93
    }
94

95
    private function createRobotLoader(): RobotLoader
96
    {
97
        $loader = new RobotLoader();
1✔
98
        foreach ($this->scanDirectories as $directory) {
1✔
99
            $loader->addDirectory($directory);
1✔
100
        }
101
        $loader->rebuild();
1✔
102

103
        return $loader;
1✔
104
    }
105

106
}
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

© 2026 Coveralls, Inc