Completed
Push — master ( c25c8e...44516d )
by Kevin
06:39
created

MasterListener::bindToResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Magium\Util\Phpunit5;
4
5
use Exception;
6
use Magium\Util\Phpunit\MasterListenerInterface;
7
use PHPUnit\Framework\TestListener;
8
use PHPUnit_Framework_AssertionFailedError;
9
use PHPUnit_Framework_Test;
10
use PHPUnit_Framework_TestSuite;
11
12
class MasterListener implements TestListener, MasterListenerInterface
0 ignored issues
show
Bug introduced by
There is one abstract method addWarning in this class; you could implement it, or declare this class as abstract.
Loading history...
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 View Code Duplication
    public function addListener( $listener)
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...
41
    {
42
        // This pretty piece of code is to maintain compatibility between PHPUnit 5 and 6.
43
        if ($listener instanceof \PHPUnit_Framework_TestListener) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_TestListener does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
44
            foreach ($this->listeners as $existingListener) {
45
                if (get_class($listener) == get_class($existingListener)) {
46
                    return;
47
                }
48
            }
49
            $this->listeners[] = $listener;
50
            foreach ($this->replay as $replay) {
51
                $this->play($replay['method'], $replay['args'], $listener);
52
            }
53
        }
54
    }
55
56 View Code Duplication
    public function play($method, $args, \PHPUnit_Framework_TestListener $instance = null)
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...
57
    {
58
        if ($instance instanceof \PHPUnit_Framework_TestListener) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_TestListener does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
59
            call_user_func_array([$instance, $method], $args);
60
        } else {
61
            foreach ($this->listeners as $listener) {
62
                call_user_func_array([$listener, $method], $args);
63
            }
64
        }
65
    }
66
67
    public function bindToResult(\PHPUnit_Framework_TestResult $result)
68
    {
69
        if ($this->result !== $result) {
70
            $this->result = $result;
71
            $result->addListener($this);
72
        }
73
    }
74
75
    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
76
    {
77
        $this->replay[] = [
78
            'method' => __FUNCTION__,
79
            'args'  => func_get_args()
80
        ];
81
        $this->play(__FUNCTION__, func_get_args());
82
    }
83
84 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...
85
    {
86
        $this->replay[] = [
87
            'method' => __FUNCTION__,
88
            'args'  => func_get_args()
89
        ];
90
        $this->play(__FUNCTION__, func_get_args());
91
    }
92
93
    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
94
    {
95
        $this->replay[] = [
96
            'method' => __FUNCTION__,
97
            'args'  => func_get_args()
98
        ];
99
        $this->play(__FUNCTION__, func_get_args());
100
    }
101
102
    public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
103
    {
104
        $this->replay[] = [
105
            'method' => __FUNCTION__,
106
            'args'  => func_get_args()
107
        ];
108
        $this->play(__FUNCTION__, func_get_args());
109
    }
110
111
    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
112
    {
113
        $this->replay[] = [
114
            'method' => __FUNCTION__,
115
            'args'  => func_get_args()
116
        ];
117
        $this->play(__FUNCTION__, func_get_args());
118
    }
119
120 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...
121
    {
122
        $this->replay[] = [
123
            'method' => __FUNCTION__,
124
            'args'  => func_get_args()
125
        ];
126
        $this->play(__FUNCTION__, func_get_args());
127
    }
128
129 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...
130
    {
131
        $this->replay[] = [
132
            'method' => __FUNCTION__,
133
            'args'  => func_get_args()
134
        ];
135
        $this->play(__FUNCTION__, func_get_args());
136
    }
137
138 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...
139
    {
140
        $this->replay[] = [
141
            'method' => __FUNCTION__,
142
            'args'  => func_get_args()
143
        ];
144
        $this->play(__FUNCTION__, func_get_args());
145
    }
146
147 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...
148
    {
149
        $this->replay[] = [
150
            'method' => __FUNCTION__,
151
            'args'  => func_get_args()
152
        ];
153
        $this->play(__FUNCTION__, func_get_args());
154
    }
155
156
}
157