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 |
||
20 | final class State |
||
21 | { |
||
22 | /** |
||
23 | * The complete test suite number of tests. |
||
24 | * |
||
25 | * @var int|null |
||
26 | */ |
||
27 | public $suiteTotalTests; |
||
28 | |||
29 | /** |
||
30 | * The complete test suite tests. |
||
31 | * |
||
32 | * @var array<int, TestResult> |
||
33 | */ |
||
34 | public $suiteTests = []; |
||
35 | |||
36 | /** |
||
37 | * The current test case class. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | public $testCaseName; |
||
42 | |||
43 | /** |
||
44 | * The current test case tests. |
||
45 | * |
||
46 | * @var array<int, TestResult> |
||
47 | */ |
||
48 | public $testCaseTests = []; |
||
49 | |||
50 | /** |
||
51 | * The state constructor. |
||
52 | */ |
||
53 | 3 | private function __construct(string $testCaseName) |
|
57 | |||
58 | /** |
||
59 | * Creates a new State starting from the given test case. |
||
60 | */ |
||
61 | 3 | public static function from(TestCase $test): self |
|
65 | |||
66 | /** |
||
67 | * Adds the given test to the State. |
||
68 | */ |
||
69 | public function add(TestResult $test): void |
||
75 | |||
76 | /** |
||
77 | * Gets the test case title. |
||
78 | */ |
||
79 | View Code Duplication | public function getTestCaseTitle(): string |
|
95 | |||
96 | /** |
||
97 | * Gets the test case title color. |
||
98 | */ |
||
99 | View Code Duplication | public function getTestCaseTitleColor(): string |
|
115 | |||
116 | /** |
||
117 | * Returns the number of tests on the current test case. |
||
118 | */ |
||
119 | public function testCaseTestsCount(): int |
||
123 | |||
124 | /** |
||
125 | * Returns the number of tests on the complete test suite. |
||
126 | */ |
||
127 | public function testSuiteTestsCount(): int |
||
131 | |||
132 | /** |
||
133 | * Checks if the given test case is different from the current one. |
||
134 | */ |
||
135 | public function testCaseHasChanged(TestCase $testCase): bool |
||
139 | |||
140 | /** |
||
141 | * Moves the a new test case. |
||
142 | */ |
||
143 | public function moveTo(TestCase $testCase): void |
||
149 | |||
150 | /** |
||
151 | * Foreach test in the test case. |
||
152 | */ |
||
153 | public function eachTestCaseTests(callable $callback): void |
||
159 | |||
160 | public function countTestsInTestSuiteBy(string $type): int |
||
166 | |||
167 | /** |
||
168 | * Checks if the given test already contains a result. |
||
169 | */ |
||
170 | public function existsInTestCase(TestCase $test): bool |
||
180 | |||
181 | /** |
||
182 | * Returns the printable test case name from the given `TestCase`. |
||
183 | */ |
||
184 | 3 | private static function getPrintableTestCaseName(TestCase $test): string |
|
194 | } |
||
195 |
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.