Completed
Push — master ( bdfd2e...af0531 )
by Cheren
25:10 queued 18:50
created

CoreEventHandler::_onSetupAdmin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
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 Core\Plugin;
19
use JBZoo\Utils\FS;
20
use Core\Controller\AppController;
21
use Cake\Event\EventListenerInterface;
22
23
/**
24
 * Class CoreEventHandler
25
 *
26
 * @package Core\Event
27
 */
28
class CoreEventHandler implements EventListenerInterface
29
{
30
31
    /**
32
     * Returns a list of events this object is implementing.
33
     *
34
     * @return  array
35
     * @return  void
36
     */
37
    public function implementedEvents()
38
    {
39
        return [
40
            'Controller.setup' => 'onControllerSetup'
41
        ];
42
    }
43
44
    /**
45
     * On controller setup.
46
     *
47
     * @param   Event $event
48
     * @return  void
49
     */
50
    public function onControllerSetup(Event $event)
51
    {
52
        /** @var AppController $controller */
53
        $controller = $event->getSubject();
54
        $isAdmin    = ($controller->request->getParam('prefix') === 'admin');
55
56
        $plugins = Plugin::loaded();
57
        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...
58
            $path     = Plugin::path($plugin);
59
            $menuFile = ($isAdmin) ? 'admin_menu' : 'menu';
60
            $navConf  = $path . 'config/' . $menuFile . '.php';
61
62
            if (FS::isFile($navConf)) {
63
                /** @noinspection PhpIncludeInspection */
64
                require_once $navConf;
65
            }
66
        }
67
    }
68
69
}
70