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

ICanBoogie / Event / 11627051317

01 Nov 2024 09:10AM UTC coverage: 95.279% (-0.4%) from 95.708%
11627051317

push

github

olvlvl
Support PHP 8.4

222 of 233 relevant lines covered (95.28%)

5.58 hits per line

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

94.59
/lib/Event/ConfigBuilder.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\Event;
13

14
use ICanBoogie\Event;
15
use LogicException;
16
use olvlvl\ComposerAttributeCollector\Attributes;
17
use ReflectionMethod;
18
use ReflectionNamedType;
19
use ReflectionType;
20

21
use function class_exists;
22
use function ICanBoogie\Service\ref;
23

24
final class ConfigBuilder
25
{
26
    /**
27
     * @var array<string, callable[]>
28
     */
29
    private array $listeners = [];
30

31
    public function build(): Config
32
    {
33
        return new Config($this->listeners);
3✔
34
    }
35

36
    /**
37
     * @param class-string<Event> $event_class
38
     * @param callable $listener
39
     *
40
     * @return $this
41
     */
42
    public function attach(string $event_class, callable $listener): self
43
    {
44
        $this->listeners[$event_class][] = $listener;
3✔
45

46
        return $this;
3✔
47
    }
48

49
    /**
50
     * @param class-string $sender_class
51
     * @param class-string<Event> $event_class
52
     * @param callable $listener
53
     *
54
     * @return $this
55
     */
56
    public function attach_to(string $sender_class, string $event_class, callable $listener): self
57
    {
58
        $this->listeners[$event_class::for($sender_class)][] = $listener;
3✔
59

60
        return $this;
3✔
61
    }
62

63
    public function use_attributes(): self
64
    {
65
        $targets = Attributes::findTargetMethods(Listen::class);
1✔
66

67
        foreach ($targets as $target) {
1✔
68
            $method = new ReflectionMethod($target->class, $target->name);
1✔
69

70
            if ($method->isStatic()) {
1✔
71
                /** @var array{ class-string, non-empty-string } $listener */
72
                $listener = [ $target->class, $target->name ];
1✔
73
            } else {
74
                if ($target->name !== '__invoke') {
1✔
75
                    throw new LogicException("Only invokable classes can be attached");
×
76
                }
77

78
                /* @var callable $listener **/
79
                $listener = ref($target->attribute->ref ?? $target->class);
1✔
80
            }
81

82
            $parameters = $method->getParameters();
1✔
83
            $event_class = self::ensure_extends_event($parameters[0]->getType());
1✔
84

85
            /** @var class-string<Event> $event_class */
86

87
            match (count($parameters)) {
1✔
88
                1 => $this->attach($event_class, $listener), // @phpstan-ignore-line
1✔
89
                2 => $this->attach_to(
1✔
90
                    self::ensure_class($parameters[1]->getType()),
1✔
91
                    $event_class,
1✔
92
                    $listener // @phpstan-ignore-line
1✔
93
                ),
1✔
94
                default => throw new LogicException("Too many parameters for $target->class::$target->name")
×
95
            };
1✔
96
        }
97

98
        return $this;
1✔
99
    }
100

101
    /**
102
     * @return class-string<Event>
103
     */
104
    private static function ensure_extends_event(?ReflectionType $type): string
105
    {
106
        $type instanceof ReflectionNamedType
1✔
107
            or throw new LogicException("Expected named type, got: $type");
1✔
108

109
        $type = $type->getName();
1✔
110

111
        is_a($type, Event::class, true)
1✔
112
            or throw new LogicException("$type does not extend " . Event::class);
1✔
113

114
        return $type;
1✔
115
    }
116

117
    /**
118
     * @return class-string
119
     */
120
    private static function ensure_class(?ReflectionType $type): string
121
    {
122
        $type instanceof ReflectionNamedType
1✔
123
            or throw new LogicException("Expected named type, got: $type");
1✔
124

125
        $type = $type->getName();
1✔
126

127
        class_exists($type, true)
1✔
128
            or throw new LogicException("$type is not a loadable class");
1✔
129

130
        return $type;
1✔
131
    }
132
}
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