StopExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 17.78 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 8
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 3
A isStopped() 0 11 3
A getStrategy() 8 8 2

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
namespace whm\Smoke\Extensions\SmokeStop;
4
5
use phmLabs\Components\Annovent\Dispatcher;
6
use phmLabs\Components\Annovent\Event\Event;
7
use PhmLabs\Components\Init\Init;
8
use whm\Smoke\Config\Configuration;
9
10
class StopExtension
11
{
12
    private $stopStrategies = array();
13
14
    /**
15
     * @param Configuration $_configuration
16
     * @param Dispatcher $_eventDispatcher
17
     * @throws \phmLabs\Components\Annovent\Exception
18
     */
19
    public function init(Configuration $_configuration, Dispatcher $_eventDispatcher)
20
    {
21
        if ($_configuration->hasSection('stop')) {
22
            $strategies = $_configuration->getSection('stop');
23
24
            foreach ($strategies as $name => $strategy) {
25
                $this->stopStrategies[$name] = Init::initialize($strategy);
26
                $_eventDispatcher->connectListener($this->stopStrategies[$name]);
27
            }
28
        }
29
    }
30
31
    /**
32
     * @Event("Scanner.Scan.isStopped")
33
     */
34
    public function isStopped(Event $event)
35
    {
36
        foreach ($this->stopStrategies as $strategy) {
37
            if ($strategy->isStopped()) {
38
                $event->setProcessed();
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
45
46 View Code Duplication
    public function getStrategy($name)
47
    {
48
        if (array_key_exists($name, $this->stopStrategies)) {
49
            return $this->stopStrategies[$name];
50
        } else {
51
            throw new \RuntimeException("Strategy ('" . $name . "') not found. Available strategies are " . implode(', ', array_keys($this->stopStrategies)));
52
        }
53
    }
54
}
55