Completed
Push — develop ( 9c40cd...a29501 )
by Peter
01:44
created

Module::init()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 90
rs 5.5115
cc 13
nc 4
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDebug/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDebug;
11
12
use WebinoDebug\Factory\DebuggerFactory;
13
use WebinoDebug\Factory\ModuleOptionsFactory;
14
use WebinoDebug\Options\ModuleOptions;
15
use WebinoDebug\Service\NullDebugger;
16
use Zend\Http\PhpEnvironment\Request;
17
use Zend\ModuleManager\Feature;
18
use Zend\ModuleManager\ModuleEvent;
19
use Zend\ModuleManager\ModuleManagerInterface;
20
use Zend\ServiceManager\ServiceManager;
21
22
/**
23
 * WebinoDebug module
24
 */
25
class Module implements Feature\InitProviderInterface
26
{
27
    /**
28
     * @param ModuleManagerInterface $modules
29
     */
30
    public function init(ModuleManagerInterface $modules)
31
    {
32
        /** @var \Zend\ModuleManager\ModuleManager $modules */
33
        /** @var ServiceManager $services */
34
        $services = $modules->getEvent()->getParam('ServiceManager');
0 ignored issues
show
Bug introduced by
The method getEvent() does not exist on Zend\ModuleManager\ModuleManagerInterface. Did you maybe mean getEventManager()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
35
        $services->setFactory(ModuleOptions::class, ModuleOptionsFactory::class);
36
        $services->setFactory(DebuggerFactory::SERVICE, DebuggerFactory::class);
37
38
        /** @var ModuleOptions $options */
39
        $options = $services->get(ModuleOptions::class);
40
41
        // set error log path
42
        ini_set('error_log', $options->getPhpErrorLog());
43
44
        // return early when disabled
45
        if ($options->isDisabled()) {
46
            return;
47
        }
48
49
        // init debugger
50
        $this->sessionStart();
51
        /** @var Service\Debugger $debugger */
52
        $debugger = $services->get(DebuggerFactory::SERVICE);
53
54
        // add a service initializer
55
        // TODO something better
56
        $modules->getEventManager()->attach(
57
            ModuleEvent::EVENT_LOAD_MODULES_POST,
58
            function () use ($services, $debugger) {
59
60
                $initializer = function ($obj) use ($debugger) {
61
                    if ($obj instanceof Service\DebuggerAwareInterface) {
62
                        /** @var Service\Debugger $debugger */
63
                        $obj->setDebugger($debugger);
64
                    }
65
                };
66
67
                $services->addInitializer($initializer);
68
                foreach ($services->getRegisteredServices()['instances'] as $name) {
69
                    $instance = $services->get($name);
70
                    ($instance instanceof ServiceManager) and $instance->addInitializer($initializer);
71
                }
72
            }
73
        );
74
75
        // return early on null debugger
76
        if ($debugger instanceof NullDebugger) {
77
            return;
78
        }
79
80
        // create bar panels
81
        $showBar   = $options->showBar();
82
        $barPanels = $options->getBarPanels();
83
84
        if ($showBar) {
85
            // set core bar panels
86
            foreach ($barPanels as $id => $barPanel) {
87
                $debugger->setBarPanel(new $barPanel($modules), $id);
88
            }
89
        }
90
91
        // finish debugger init
92
        $modules->getEventManager()->attach(
93
            ModuleEvent::EVENT_LOAD_MODULES_POST,
94
            function () use ($services, $debugger, $options, $showBar, $barPanels) {
95
96
                // update module options
97
                /** @var ModuleOptions $newOptions */
98
                $newOptions = $services->create(ModuleOptions::class);
99
                $options->setFromArray($newOptions->toArray());
100
101
                if ($showBar) {
102
                    // set additional bar panels
103
                    $newBarPanels = array_diff($options->getBarPanels(), $barPanels);
104
                    foreach ($newBarPanels as $id => $barPanel) {
105
                        $debugger->setBarPanel($services->get($barPanel), $id);
0 ignored issues
show
Documentation introduced by
$services->get($barPanel) is of type object|array, but the function expects a null|object<WebinoDebug\Debugger\PanelInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
                    }
107
108
                    // init bar panels
109
                    foreach ($debugger->getBarPanels() as $barPanel) {
110
                        ($barPanel instanceof Debugger\PanelInitInterface) and $barPanel->init($services);
111
                    }
112
                }
113
114
                // debugger templates
115
                $templateMap = $options->getTemplateMap();
116
                empty($templateMap) or $services->get('ViewTemplateMapResolver')->merge($templateMap);
117
            }
118
        );
119
    }
120
121
    /**
122
     * Start session for Ajax debug support
123
     *
124
     * Simplified session start with basic cookie params.
125
     *
126
     * @return void
127
     */
128
    private function sessionStart()
129
    {
130
        if (session_status() !== PHP_SESSION_ACTIVE) {
131
            $request = new Request;
132
            session_set_cookie_params(
133
                0,
134
                $request->getBasePath(),
135
                null,
136
                $request->getUri()->getScheme() === 'https',
137
                true
138
            );
139
            session_start();
140
        }
141
    }
142
}
143