Issues (59)

src/Actions/Observer.php (2 issues)

1
<?php
2
/**
3
 * Action observer
4
 * User: moyo
5
 * Date: 25/12/2017
6
 * Time: 5:56 PM
7
 */
8
9
namespace Carno\Database\SQL\Actions;
10
11
use Carno\Database\SQL\Builder;
12
use Closure;
13
14
trait Observer
15
{
16
    /**
17
     * @var Closure[]
18
     */
19
    private $actWatchers = [];
20
21
    /**
22
     * @var Closure[]
23
     */
24
    private $rsWatchers = [];
25
26
    /**
27
     * @param int $action
28
     * @param Closure ...$programs
29
     */
30
    protected function actWatching(int $action, Closure ...$programs) : void
31
    {
32
        $this->actWatchers[$action] = array_merge($this->actWatchers[$action] ?? [], $programs);
0 ignored issues
show
It seems like $this->actWatchers[$action] ?? array() can also be of type Closure; however, parameter $array1 of array_merge() does only seem to accept array, 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

32
        $this->actWatchers[$action] = array_merge(/** @scrutinizer ignore-type */ $this->actWatchers[$action] ?? [], $programs);
Loading history...
33
    }
34
35
    /**
36
     * @param int $action
37
     * @param Builder $instance
38
     */
39
    protected function actTrigger(int $action, Builder $instance) : void
40
    {
41
        foreach ($this->actWatchers[$action] ?? [] as $watcher) {
42
            $watcher($instance);
43
        }
44
    }
45
46
    /**
47
     * @param Closure ...$programs
48
     */
49
    protected function rsWatching(Closure ...$programs) : void
50
    {
51
        $this->rsWatchers = array_merge($this->rsWatchers, $programs);
52
    }
53
54
    /**
55
     * @param array $rows
56
     * @return array
57
     */
58
    protected function rsTrigger(array $rows)
59
    {
60
        foreach ($this->rsWatchers as $watcher) {
61
            $rows = yield $watcher($rows);
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $watcher($rows) returns the type Generator which is incompatible with the documented return type array.
Loading history...
62
        }
63
64
        return $rows;
65
    }
66
}
67