Completed
Push — master ( f172d4...76e139 )
by devosc
02:14
created

src/Event/Generator.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Event;
7
8
use Iterator;
9
use Mvc5\Signal as _Signal;
10
11
trait Generator
12
{
13
    /**
14
     *
15
     */
16
    use _Signal;
17
18
    /**
19
     * @param array|callable|object|string $config
20
     * @return callable|null
21
     */
22
    protected abstract function callable($config) : callable;
23
24
    /**
25
     * @param callable|Event|string $event
26
     * @param callable $listener
27
     * @param array $args
28
     * @param callable $callback
29
     * @return mixed
30
     */
31 40
    protected function emit($event, callable $listener, array $args = [], callable $callback = null)
32
    {
33 40
        return $event instanceof Event ? $event($listener, $args, $callback) : $this->signal($listener, $args, $callback);
34
    }
35
36
    /**
37
     * @param array|string|Iterator $event
38
     * @param array $args
39
     * @param callable $callback
40
     * @return mixed|null
41
     */
42 40
    protected function generate($event, array $args = [], callable $callback = null)
43
    {
44 40
        return $this->traverse($event, $this->queue($event, $args), $args, $callback);
45
    }
46
47
    /**
48
     * @param $listener
49
     * @param $event
50
     * @param $queue
51
     * @param $args
52
     * @param $callback
53
     * @param null $result
54
     * @return null
55
     */
56 40
    protected function iterate($listener, $event, &$queue, $args, $callback, $result = null)
57
    {
58 40
        return !$listener || ($event instanceof Event && $event->stopped()) ? $result : $this->iterate(
59 40
            $this->step($queue), $event, $queue, $args, $callback, $this->result($event, $listener, $args, $callback)
60
        );
61
    }
62
63
    /**
64
     * @param Event|object|string $event
65
     * @param array $args
66
     * @return array|Iterator|null
67
     */
68
    protected abstract function iterator($event, array $args = []);
69
70
    /**
71
     * @param array|Event|object|string|Iterator $event
72
     * @param array $args
73
     * @return array|Iterator
74
     */
75 40
    protected function queue($event, array $args = [])
76
    {
77 40
        return is_array($event) || $event instanceof Iterator ? $event : $this->iterator($event, $args);
78
    }
79
80
    /**
81
     * @param array|Event|object|Iterator $event
82
     * @param $listener
83
     * @param array $args
84
     * @param callable $callback
85
     * @return mixed
86
     */
87 40
    protected function result($event, $listener, array $args = [], callable $callback = null)
88
    {
89 40
        return $this->emit($event, $this->callable($listener), $args, $callback);
0 ignored issues
show
It seems like $this->callable($listener) targeting Mvc5\Event\Generator::callable() can also be of type null; however, Mvc5\Event\Generator::emit() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
90
    }
91
92
    /**
93
     * @param array|Iterator $queue
94
     * @return mixed|null
95
     */
96 40
    protected function start(&$queue)
97
    {
98 40
        if (is_array($queue)) {
99 37
            return reset($queue);
100
        }
101
102 3
        $queue->rewind();
103
104 3
        return $queue->current();
105
    }
106
107
    /**
108
     * @param array|Iterator $queue
109
     * @return mixed|null
110
     */
111 40
    protected function step(&$queue)
112
    {
113 40
        if (is_array($queue)) {
114 37
            return next($queue);
115
        }
116
117 3
        $queue->next();
118
119 3
        return $queue->current();
120
    }
121
122
    /**
123
     * @param $event
124
     * @param $queue
125
     * @param $args
126
     * @param $callback
127
     * @return null
128
     */
129 40
    protected function traverse($event, $queue, $args, $callback)
130
    {
131 40
        return $this->iterate($this->start($queue), $event, $queue, $args, $callback);
132
    }
133
}
134