Bootstrap   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 173
Duplicated Lines 12.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 22
loc 173
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCacheKey() 0 4 1
A setCacheKey() 0 5 1
B configure() 0 23 4
A configureServices() 0 11 2
A attachCoreListeners() 0 5 1
A detachCoreListeners() 0 5 1
A attachListeners() 0 5 1
A attachListener() 0 5 2
A detachListener() 0 5 2
A eachCoreListener() 11 11 3
A eachListener() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoAppLib\Service;
12
13
use WebinoAppLib\Application;
14
use WebinoAppLib\Event\AppEvent;
15
use WebinoAppLib\Feature\Config;
16
use WebinoAppLib\Log;
17
use WebinoConfigLib\Feature\ConfigCacheEnabled;
18
use Zend\ServiceManager\Config as ServicesConfig;
19
20
/**
21
 * Application bootstrap
22
 */
23
class Bootstrap
24
{
25
    /**
26
     * Application configuration cache key
27
     */
28
    const CACHE_KEY = 'config';
29
30
    /**
31
     * @var Application
32
     */
33
    private $app;
34
35
    /**
36
     * @var string
37
     */
38
    private $cacheKey;
39
40
    /**
41
     * @param object|Application $app
42
     */
43
    public function __construct(Application $app)
44
    {
45
        $this->app = $app;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    private function getCacheKey()
52
    {
53
        return $this::CACHE_KEY . '_' . $this->cacheKey;
54
    }
55
56
    /**
57
     * @param string $key
58
     * @return $this
59
     */
60
    public function setCacheKey($key)
61
    {
62
        $this->cacheKey .= md5($key);
63
        return $this;
64
    }
65
66
    /**
67
     * Configures the application
68
     *
69
     * Returns cached early if any.
70
     *
71
     * @triggers configure
72
     * @return $this
73
     */
74
    public function configure()
75
    {
76
        $config   = $this->app->getConfig();
77
        $useCache = $this->app->getConfig(ConfigCacheEnabled::KEY);
78
79
        if ($useCache) {
80
            $cached = $this->app->getCache($this->getCacheKey());
81
            if ($cached) {
82
                $this->app->setConfig($cached);
83
                $this->configureServices();
84
                $this->app->log(Log\LoadCachedAppConfig::class);
85
                return $this;
86
            }
87
        }
88
89
        $this->app->emit(AppEvent::CONFIGURE, [], [$this->app, 'mergeConfig']);
90
        $this->configureServices();
91
        $this->app->log(Log\ConfigureApp::class);
92
93
        $useCache and $this->app->setCache($this->getCacheKey(), $config);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
94
95
        return $this;
96
    }
97
98
    /**
99
     * Configure application services
100
     *
101
     * @return $this
102
     */
103
    protected function configureServices()
104
    {
105
        $services = $this->app->getConfig(Config::SERVICES);
106
        if (null === $services) {
107
            return $this;
108
        }
109
110
        $config = $services->toArray();
111
        (new ServicesConfig($config))->configureServiceManager($this->app->getServices());
112
        return $this;
113
    }
114
115
    /**
116
     * Attach core listeners
117
     *
118
     * @return $this
119
     */
120
    public function attachCoreListeners()
121
    {
122
        $this->eachCoreListener([$this, 'attachListener']);
123
        return $this;
124
    }
125
126
    /**
127
     * Detach core listeners
128
     *
129
     * @return $this
130
     */
131
    public function detachCoreListeners()
132
    {
133
        $this->eachCoreListener([$this, 'detachListener']);
134
        return $this;
135
    }
136
137
    /**
138
     * Attach application listeners
139
     * 
140
     * @return $this
141
     */
142
    public function attachListeners()
143
    {
144
        $this->eachListener([$this, 'attachListener']);
145
        return $this;
146
    }
147
148
    /**
149
     * @param $listener
150
     */
151
    protected function attachListener($listener)
152
    {
153
        $service = $this->app->get($listener);
154
        $service and $this->app->bind($service);
0 ignored issues
show
Documentation introduced by
$service is of type object|array, but the function expects a string|object<Zend\Event...enerAggregateInterface>.

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...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
155
    }
156
157
    /**
158
     * @param $listener
159
     */
160
    protected function detachListener($listener)
161
    {
162
        $service = $this->app->get($listener);
163
        $service and $this->app->unbind($service);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
164
    }
165
166
    /**
167
     * @param callable $callback
168
     */
169 View Code Duplication
    protected function eachCoreListener(callable $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $listeners = $this->app->getCoreConfig(Config::LISTENERS);
172
        if (empty($listeners)) {
173
            return;
174
        }
175
176
        foreach ($listeners as $listener) {
177
            call_user_func($callback, $listener);
178
        }
179
    }
180
181
    /**
182
     * @param callable $callback
183
     */
184 View Code Duplication
    protected function eachListener(callable $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186
        $listeners = $this->app->getConfig(Config::LISTENERS);
187
        if (empty($listeners)) {
188
            return;
189
        }
190
191
        foreach ($listeners as $listener) {
192
            call_user_func($callback, $listener);
193
        }
194
    }
195
}
196