Completed
Push — master ( f66d75...ad4e62 )
by devosc
02:38
created

Generator::events()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Resolver;
7
8
use Mvc5\Arg;
9
use Mvc5\Event\Event;
10
use Mvc5\Event\Generator as Base;
11
12
trait Generator
13
{
14
    /**
15
     *
16
     */
17
    use Base;
18
19
    /**
20
     * @var array|\Traversable
21
     */
22
    protected $events = [];
23
24
    /**
25
     * @param array|object|string|\Traversable $event
26
     * @param array $args
27
     * @param callable $callback
28
     * @return mixed|null
29
     */
30
    protected function event($event, array $args = [], callable $callback = null)
31
    {
32
        return $this->generate($event, $args, $callback ?? $this);
0 ignored issues
show
Bug introduced by
It seems like $callback ?? $this can also be of type this<Mvc5\Resolver\Generator>; however, Mvc5\Event\Generator::generate() does only seem to accept null|callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33
    }
34
35
    /**
36
     * @param array|\ArrayAccess|null|\Traversable $config
37
     * @return array|\ArrayAccess|null|\Traversable
38
     */
39
    public function events($config = null)
40
    {
41
        return null !== $config ? $this->events = $config : $this->events;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config can also be of type object<ArrayAccess>. However, the property $events is declared as type array|object<Traversable>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
    }
43
44
    /**
45
     * @param array|callable|object|string $plugin
46
     * @param callable $callback
47
     * @return callable|null
48
     */
49
    protected function listener($plugin, callable $callback = null)
50
    {
51
        return !$plugin instanceof Event ? $plugin : function(array $args = []) use ($plugin, $callback) {
52
            return $this->event($plugin, $args, $callback);
53
        };
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return array|\Traversable|null
59
     */
60
    protected function listeners($name)
61
    {
62
        return $this->events[$name] ?? $this->signal(new Exception, [Arg::PLUGIN => $name]);
63
    }
64
}
65