Issues (480)

src/Event/Generator.php (1 issue)

Labels
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Event;
7
8
use Iterator;
9
use Mvc5\Signal;
10
use Throwable;
11
12
use function Mvc5\Iterator\{ next, rewind };
13
14
trait Generator
15
{
16
    /**
17
     * @param mixed $listener
18
     * @return callable
19
     */
20
    protected abstract function callable($listener) : callable;
21
22
    /**
23
     * @param array|Event|Iterator|object|string $event
24
     * @param callable $listener
25
     * @param array $args
26
     * @param callable|null $callback
27
     * @return mixed
28 18
     * @throws Throwable
29
     */
30 18
    protected function emit($event, callable $listener, array $args = [], callable $callback = null)
31
    {
32
        return $event instanceof Event ? $event($listener, $args, $callback) : Signal::emit($listener, $args, $callback);
33
    }
34
35
    /**
36
     * @param array|Event|Iterator|object|string $event
37
     * @param array $args
38
     * @param callable|null $callback
39
     * @return mixed
40 20
     * @throws Throwable
41
     */
42 20
    protected function generate($event, array $args = [], callable $callback = null)
43
    {
44
        return $this->iterate(null, $event, rewind($this->iterator($event, $args)), $args, $callback);
0 ignored issues
show
It seems like $event can also be of type array; however, parameter $event of Mvc5\Event\Generator::iterator() does only seem to accept Mvc5\Event\Event|object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        return $this->iterate(null, $event, rewind($this->iterator(/** @scrutinizer ignore-type */ $event, $args)), $args, $callback);
Loading history...
45
    }
46
47
    /**
48
     * @param mixed $result
49
     * @param array|Event|Iterator|object|string $event
50
     * @param Iterator $iterator
51
     * @param array $args
52
     * @param callable|null $callback
53
     * @return mixed
54 19
     * @throws Throwable
55
     */
56 19
    protected function iterate($result, $event, Iterator $iterator, array $args, callable $callback = null)
57 19
    {
58
        return $this->stopped($event, $iterator) ? $result : $this->iterate(
59
            $this->emit($event, $this->callable($iterator->current()), $args, $callback), $event, next($iterator), $args, $callback
60
        );
61
    }
62
63
    /**
64
     * @param Event|object|string $event
65
     * @param array $args
66
     * @return Iterator
67
     * @throws Throwable
68
     */
69 18
    abstract protected function iterator($event, array $args = []) : Iterator;
70
71 18
    /**
72
     * @param Event|mixed $event
73
     * @param Iterator $iterator
74
     * @return bool
75
     */
76
    protected function stopped($event, Iterator $iterator) : bool
77
    {
78
        return ($event instanceof Event && $event->stopped()) || !$iterator->valid();
79
    }
80
}
81