Passed
Push — stable ( c09016...4fe1c4 )
by Nuno
01:53
created

State   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 200
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 10.64%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 1
dl 32
loc 200
ccs 5
cts 47
cp 0.1064
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A from() 0 4 1
A add() 0 6 1
A getTestCaseTitle() 16 16 5
A getTestCaseTitleColor() 16 16 5
A testCaseTestsCount() 0 4 1
A testSuiteTestsCount() 0 4 1
A testCaseHasChanged() 0 4 1
A moveTo() 0 6 1
A eachTestCaseTests() 0 6 2
A countTestsInTestSuiteBy() 0 6 1
A existsInTestCase() 0 10 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
/**
4
 * This file is part of Collision.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace NunoMaduro\Collision\Adapters\Phpunit;
13
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * @internal
18
 */
19
final class State
20
{
21
    /**
22
     * The complete test suite number of tests.
23
     *
24
     * @var int|null
25
     */
26
    public $suiteTotalTests;
27
28
    /**
29
     * The complete test suite tests.
30
     *
31
     * @var array<int, TestResult>
32
     */
33
    public $suiteTests = [];
34
35
    /**
36
     * The current test case class.
37
     *
38
     * @var string
39
     */
40
    public $testCaseClass;
41
42
    /**
43
     * The current test case tests.
44
     *
45
     * @var array<int, TestResult>
46
     */
47
    public $testCaseTests = [];
48
49
    /**
50
     * The state constructor.
51
     *
52
     * @param  string  $testCaseClass
53
     */
54 3
    private function __construct(string $testCaseClass)
55
    {
56 3
        $this->testCaseClass = $testCaseClass;
57 3
    }
58
59
    /**
60
     * Creates a new State starting from the given test case.
61
     *
62
     * @param  TestCase  $test
63
     *
64
     * @return self
65
     */
66 3
    public static function from(TestCase $test): self
67
    {
68 3
        return new self(get_class($test));
69
    }
70
71
    /**
72
     * Adds the given test to the State.
73
     *
74
     * @param  TestResult  $test
75
     *
76
     * @return void
77
     */
78
    public function add(TestResult $test): void
79
    {
80
        $this->testCaseTests[] = $test;
81
82
        $this->suiteTests[] = $test;
83
    }
84
85
    /**
86
     * Gets the test case title.
87
     *
88
     * @return string
89
     */
90 View Code Duplication
    public function getTestCaseTitle(): string
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...
91
    {
92
        foreach ($this->testCaseTests as $test) {
93
            if ($test->type === TestResult::FAIL) {
94
                return 'FAIL';
95
            }
96
        }
97
98
        foreach ($this->testCaseTests as $test) {
99
            if ($test->type !== TestResult::PASS) {
100
                return 'WARN';
101
            }
102
        }
103
104
        return 'PASS';
105
    }
106
107
    /**
108
     * Gets the test case title color.
109
     *
110
     * @return string
111
     */
112 View Code Duplication
    public function getTestCaseTitleColor(): string
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...
113
    {
114
        foreach ($this->testCaseTests as $test) {
115
            if ($test->type === TestResult::FAIL) {
116
                return 'red';
117
            }
118
        }
119
120
        foreach ($this->testCaseTests as $test) {
121
            if ($test->type !== TestResult::PASS) {
122
                return 'yellow';
123
            }
124
        }
125
126
        return 'green';
127
    }
128
129
    /**
130
     * Returns the number of tests on the current test case.
131
     *
132
     * @return int
133
     */
134
    public function testCaseTestsCount(): int
135
    {
136
        return count($this->testCaseTests);
137
    }
138
139
    /**
140
     * Returns the number of tests on the complete test suite.
141
     *
142
     * @return int
143
     */
144
    public function testSuiteTestsCount(): int
145
    {
146
        return count($this->suiteTests);
147
    }
148
149
    /**
150
     * Checks if the given test case is different from the current one.
151
     *
152
     * @param  TestCase  $testCase
153
     *
154
     * @return bool
155
     */
156
    public function testCaseHasChanged(TestCase $testCase): bool
157
    {
158
        return get_class($testCase) !== $this->testCaseClass;
159
    }
160
161
    /**
162
     * Moves the a new test case.
163
     *
164
     * @param  TestCase  $testCase
165
     *
166
     * @return void
167
     */
168
    public function moveTo(TestCase $testCase): void
169
    {
170
        $this->testCaseClass = get_class($testCase);
171
172
        $this->testCaseTests = [];
173
    }
174
175
    /**
176
     * Foreach test in the test case.
177
     *
178
     * @param  callable  $callback
179
     *
180
     * @return void
181
     */
182
    public function eachTestCaseTests(callable $callback): void
183
    {
184
        foreach ($this->testCaseTests as $test) {
185
            $callback($test);
186
        }
187
    }
188
189
    /**
190
     * @param  string  $type
191
     *
192
     * @return int
193
     */
194
    public function countTestsInTestSuiteBy(string $type): int
195
    {
196
        return count(array_filter($this->suiteTests, function (TestResult $testResult) use ($type) {
197
            return $testResult->type === $type;
198
        }));
199
    }
200
201
    /**
202
     * Checks if the given test already contains a result.
203
     *
204
     * @param  TestCase  $test
205
     *
206
     * @return bool
207
     */
208
    public function existsInTestCase(TestCase $test): bool
209
    {
210
        foreach ($this->testCaseTests as $testResult) {
211
            if (TestResult::makeDescription($test) === $testResult->description) {
212
                return true;
213
            }
214
        }
215
216
        return false;
217
    }
218
}
219