Completed
Push — master ( 6dae49...efbc70 )
by Kevin
06:34 queued 03:08
created

MasterListener   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 142
Duplicated Lines 28.17 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 22
c 6
b 3
f 1
lcom 1
cbo 1
dl 40
loc 142
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getListener() 0 8 3
A clear() 0 6 1
A addListener() 0 12 4
A play() 0 10 3
A bindToResult() 0 7 2
A addError() 0 8 1
A addFailure() 8 8 1
A addIncompleteTest() 0 8 1
A addRiskyTest() 0 8 1
A addSkippedTest() 0 8 1
A startTestSuite() 8 8 1
A endTestSuite() 8 8 1
A startTest() 8 8 1
A endTest() 8 8 1

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 Magium\Util\Phpunit;
4
5
use Exception;
6
use Magium\Util\Log\Clairvoyant;
7
use Magium\Util\TestCase\RegistrationCallbackInterface;
8
use PHPUnit_Framework_AssertionFailedError;
9
use PHPUnit_Framework_Test;
10
use PHPUnit_Framework_TestSuite;
11
12
class MasterListener implements \PHPUnit_Framework_TestListener
13
{
14
15
    protected $replay = [];
16
    protected $listeners = [];
17
    protected $result;
18
19
    /**
20
     * @param $class
21
     * @return \PHPUnit_Framework_TestListener
22
     */
23
24
    public function getListener($class)
25
    {
26
        foreach ($this->listeners as $listener) {
27
            if (get_class($listener) == $class) {
28
                return $listener;
29
            }
30
        }
31
    }
32
33
    public function clear()
34
    {
35
        $this->replay = [];
36
        $this->listeners = [];
37
        $this->result = null;
38
    }
39
40
    public function addListener(\PHPUnit_Framework_TestListener $listener)
41
    {
42
        foreach ($this->listeners as $existingListener) {
43
            if (get_class($listener) == get_class($existingListener)) {
44
                return;
45
            }
46
        }
47
        $this->listeners[] = $listener;
48
        foreach ($this->replay as $replay) {
49
            $this->play($replay['method'], $replay['args'], $listener);
50
        }
51
    }
52
53
    public function play($method, $args, \PHPUnit_Framework_TestListener $instance = null)
54
    {
55
        if ($instance instanceof \PHPUnit_Framework_TestListener) {
56
            call_user_func_array([$instance, $method], $args);
57
        } else {
58
            foreach ($this->listeners as $listener) {
59
                call_user_func_array([$listener, $method], $args);
60
            }
61
        }
62
    }
63
64
    public function bindToResult(\PHPUnit_Framework_TestResult $result)
65
    {
66
        if ($this->result !== $result) {
67
            $this->result = $result;
68
            $result->addListener($this);
69
        }
70
    }
71
72
    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
73
    {
74
        $this->replay[] = [
75
            'method' => __FUNCTION__,
76
            'args'  => func_get_args()
77
        ];
78
        $this->play(__FUNCTION__, func_get_args());
79
    }
80
81 View Code Duplication
    public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
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...
82
    {
83
        $this->replay[] = [
84
            'method' => __FUNCTION__,
85
            'args'  => func_get_args()
86
        ];
87
        $this->play(__FUNCTION__, func_get_args());
88
    }
89
90
    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
91
    {
92
        $this->replay[] = [
93
            'method' => __FUNCTION__,
94
            'args'  => func_get_args()
95
        ];
96
        $this->play(__FUNCTION__, func_get_args());
97
    }
98
99
    public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
100
    {
101
        $this->replay[] = [
102
            'method' => __FUNCTION__,
103
            'args'  => func_get_args()
104
        ];
105
        $this->play(__FUNCTION__, func_get_args());
106
    }
107
108
    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
109
    {
110
        $this->replay[] = [
111
            'method' => __FUNCTION__,
112
            'args'  => func_get_args()
113
        ];
114
        $this->play(__FUNCTION__, func_get_args());
115
    }
116
117 View Code Duplication
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
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...
118
    {
119
        $this->replay[] = [
120
            'method' => __FUNCTION__,
121
            'args'  => func_get_args()
122
        ];
123
        $this->play(__FUNCTION__, func_get_args());
124
    }
125
126 View Code Duplication
    public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
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...
127
    {
128
        $this->replay[] = [
129
            'method' => __FUNCTION__,
130
            'args'  => func_get_args()
131
        ];
132
        $this->play(__FUNCTION__, func_get_args());
133
    }
134
135 View Code Duplication
    public function startTest(PHPUnit_Framework_Test $test)
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...
136
    {
137
        $this->replay[] = [
138
            'method' => __FUNCTION__,
139
            'args'  => func_get_args()
140
        ];
141
        $this->play(__FUNCTION__, func_get_args());
142
    }
143
144 View Code Duplication
    public function endTest(PHPUnit_Framework_Test $test, $time)
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...
145
    {
146
        $this->replay[] = [
147
            'method' => __FUNCTION__,
148
            'args'  => func_get_args()
149
        ];
150
        $this->play(__FUNCTION__, func_get_args());
151
    }
152
153
}