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 |
||
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) |
|
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 |
|
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 |
||
84 | |||
85 | /** |
||
86 | * Gets the test case title. |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | View Code Duplication | public function getTestCaseTitle(): string |
|
104 | |||
105 | /** |
||
106 | * Gets the test case title color. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | View Code Duplication | public function getTestCaseTitleColor(): string |
|
124 | |||
125 | /** |
||
126 | * Returns the number of tests on the current test case. |
||
127 | * |
||
128 | * @return int |
||
129 | */ |
||
130 | public function testCaseTestsCount(): int |
||
134 | |||
135 | /** |
||
136 | * Returns the number of tests on the complete test suite. |
||
137 | * |
||
138 | * @return int |
||
139 | */ |
||
140 | public function testSuiteTestsCount(): int |
||
144 | |||
145 | /** |
||
146 | * Checks if the given test case is different from the current one. |
||
147 | * |
||
148 | * @param TestCase $testCase |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function testCaseHasChanged(TestCase $testCase): bool |
||
156 | |||
157 | /** |
||
158 | * Moves the a new test case. |
||
159 | * |
||
160 | * @param TestCase $testCase |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public function moveTo(TestCase $testCase): void |
||
170 | |||
171 | /** |
||
172 | * Foreach test in the test case. |
||
173 | * |
||
174 | * @param callable $callback |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | public function eachTestCaseTests(callable $callback): void |
||
184 | |||
185 | /** |
||
186 | * @param string $type |
||
187 | * |
||
188 | * @return int |
||
189 | */ |
||
190 | public function countTestsInTestSuiteBy(string $type): int |
||
196 | |||
197 | /** |
||
198 | * Checks if the given test already contains a result. |
||
199 | * |
||
200 | * @param TestCase $test |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function existsInTestCase(TestCase $test): bool |
||
214 | } |
||
215 |
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.