EventManager::trigger()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Core
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Core".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Event;
17
18
use Cake\Core\App;
19
use Core\Core\Plugin;
20
use Cake\Event\EventManager as CakeEventManager;
21
22
/**
23
 * Class Manager
24
 *
25
 * @package Core\Event
26
 */
27
class EventManager extends CakeEventManager
28
{
29
30
    /**
31
     * Load Event Handlers during bootstrap.
32
     *
33
     * Plugins can add their own custom EventHandler in Config/events.php
34
     * with the following format:
35
     *
36
     * 'events' => [
37
     *      'Core.CoreEventHandler' => [
38
     *          'options' = [
39
     *              'callable' => '',
40
     *              'priority' => '',
41
     *          ]
42
     *      ]
43
     * ]
44
     *
45
     * @return void
46
     */
47
    public static function loadListeners()
48
    {
49
        $manager = self::instance();
50
        $plugins = Plugin::loaded();
51
52
        foreach ($plugins as $plugin) {
0 ignored issues
show
Bug introduced by
The expression $plugins of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
53
            $events = Plugin::getData($plugin, 'events')->getArrayCopy();
54
            foreach ($events as $name => $config) {
55
                if (is_numeric($name)) {
56
                    $name   = $config;
57
                    $config = [];
58
                }
59
60
                $class  = App::className($name, 'Event');
61
                $config = (array) $config;
62
63
                if ($class !== false) {
64
                    $listener = new $class($config);
65
                    $manager->on($listener, $config);
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * Emits an event.
73
     *
74
     * @param string $name
75
     * @param object|null $subject
76
     * @param array|null $data
77
     * @return Event
78
     */
79
    public static function trigger($name, $subject = null, $data = null)
80
    {
81
        $event = new Event($name, $subject, $data);
82
        if (is_object($subject)) {
83
            return $subject->getEventManager()->dispatch($event);
84
        }
85
86
        return EventManager::instance()->dispatch($event);
87
    }
88
}
89