Listener::init()   C
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 14
nc 15
nop 0
1
<?php
2
3
namespace yiicod\listener;
4
5
use Yii;
6
use yii\base\BootstrapInterface;
7
use yii\base\Component;
8
use yii\base\Event;
9
use yii\helpers\ArrayHelper;
10
11
/**
12
 * Class Listener
13
 *
14
 * @package yiicod\listener\components
15
 */
16
class Listener extends Component implements BootstrapInterface
17
{
18
    public $eventAliases = [
19
        '@app/config/listeners.php',
20
    ];
21
22
    /**
23
     * @var array
24
     */
25
    private static $_events;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
27
    public function init()
28
    {
29
        $events = [];
30
        static::$_events = [];
0 ignored issues
show
Bug introduced by
Since $_events is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $_events to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
31
        foreach ($this->eventAliases as $alias) {
32
            if (file_exists(Yii::getAlias($alias))) {
33
                $events = ArrayHelper::merge($events, include(Yii::getAlias($alias)));
34
            }
35
        }
36
        ksort($events);
37
        foreach ($events as $priority => $keys) {
38
            foreach ($keys as $event => $listeners) {
39
                foreach ($listeners as $handler) {
40
                    list($class, $name) = explode('@', $event);
41
                    if (false === self::hasHandler($class, $name, $handler)) {
42
                        static::$_events[$class][$name][] = $handler;
0 ignored issues
show
Bug introduced by
Since $_events is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $_events to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
43
                        Event::on($class, $name, $handler);
44
                    }
45
                }
46
            }
47
        }
48
    }
49
50
    /**
51
     * Register events
52
     *
53
     * @param \yii\base\Application $app
54
     */
55
    public function bootstrap($app)
56
    {
57
        // Called in the init method
58
    }
59
60
    /**
61
     * Check event list if handler exists for class.
62
     *
63
     * @param $class
64
     * @param $name
65
     * @param $handler
66
     *
67
     * @return bool
68
     */
69
    public static function hasHandler(string $class, string $name, array $handler): bool
70
    {
71
        $result = array_filter((static::$_events[$class][$name] ?? []), function ($item) use ($handler) {
0 ignored issues
show
Bug introduced by
Since $_events is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $_events to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
72
            $item = array_map(function ($value) {
73
                return trim($value, '\\');
74
            }, $item);
75
            $handler = array_map(function ($value) {
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $handler, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
76
                return trim($value, '\\');
77
            }, $handler);
78
79
            return $item === $handler;
80
        });
81
82
        return count($result) > 0;
83
    }
84
}
85