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 | class Clairvoyant extends AbstractConfigurableElement implements WriterInterface, \PHPUnit_Framework_TestListener |
||
21 | { |
||
22 | const TYPE_TEST_RESULT = 'test-result'; |
||
23 | const TYPE_TEST_STATUS = 'test-status'; |
||
24 | const TYPE_TEST_CHECKPOINT = 'test-checkpoint'; |
||
25 | |||
26 | const TEST_RESULT_PASSED = 'passed'; |
||
27 | const TEST_RESULT_ERROR = 'error'; |
||
28 | const TEST_RESULT_FAILED = 'failed'; |
||
29 | const TEST_RESULT_SKIPPED = 'skipped'; |
||
30 | const TEST_RESULT_RISKY = 'risky'; |
||
31 | const TEST_RESULT_INCOMPLETE = 'incomplete'; |
||
32 | |||
33 | const TEST_STATUS_STARTED = 'started'; |
||
34 | const TEST_STATUS_COMPLETED = 'completed'; |
||
35 | |||
36 | /** |
||
37 | * This provides the Clairvoyant-based project ID. It must be retrieved from the MagiumLib.com website. If |
||
38 | * Clairvoyant is enabled and this project ID is missing an exception will be thrown. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | |||
43 | public $projectId; |
||
44 | public $enabled = null; |
||
45 | |||
46 | protected $testName; |
||
47 | protected $logger; |
||
48 | protected $testTitle; |
||
49 | protected $testDescription; |
||
50 | protected $capability; |
||
51 | protected $sessionId; |
||
52 | /** |
||
53 | * @var Request |
||
54 | */ |
||
55 | protected $request; |
||
56 | protected $testId; |
||
57 | protected static $testRunId; |
||
58 | protected $events = []; |
||
59 | protected $apiConfiguration; |
||
60 | protected $testResult; |
||
61 | protected $characteristics = []; |
||
62 | |||
63 | public function __construct( |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function getEnabled() |
||
82 | |||
83 | /** |
||
84 | * @param bool $enabled |
||
85 | */ |
||
86 | public function setEnabled($enabled) |
||
90 | |||
91 | /** |
||
92 | * @return mixed |
||
93 | */ |
||
94 | public function getProjectId() |
||
98 | |||
99 | /** |
||
100 | * @param mixed $projectId |
||
101 | */ |
||
102 | public function setProjectId($projectId) |
||
106 | |||
107 | |||
108 | |||
109 | public function setApiRequest(Request $request) |
||
113 | |||
114 | /** |
||
115 | * Provide a test-run-wide ID that can be used to tie individual test runs together |
||
116 | * |
||
117 | * @param string $id |
||
118 | */ |
||
119 | |||
120 | public static function setTestRunId($id = null) |
||
130 | |||
131 | public function reset() |
||
144 | |||
145 | public function markKeyCheckpoint($checkpoint) |
||
155 | |||
156 | public function setSessionId($sessionId) |
||
160 | |||
161 | /** |
||
162 | * @param mixed $capability |
||
163 | */ |
||
164 | public function setCapability($capability) |
||
168 | |||
169 | /** |
||
170 | * @param mixed $testDescription |
||
171 | */ |
||
172 | public function setTestDescription($testDescription) |
||
176 | |||
177 | /** |
||
178 | * @param mixed $testTitle |
||
179 | */ |
||
180 | public function setTestTitle($testTitle) |
||
184 | |||
185 | public function addFilter($filter) |
||
189 | |||
190 | public function setFormatter($formatter) |
||
194 | |||
195 | public function write(array $event) |
||
207 | |||
208 | public function shutdown() |
||
212 | |||
213 | View Code Duplication | public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) |
|
224 | |||
225 | View Code Duplication | public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) |
|
236 | |||
237 | View Code Duplication | public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
|
248 | |||
249 | View Code Duplication | public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
|
260 | |||
261 | View Code Duplication | public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
|
272 | |||
273 | public function startTestSuite(PHPUnit_Framework_TestSuite $suite) |
||
277 | |||
278 | public function endTestSuite(PHPUnit_Framework_TestSuite $suite) |
||
282 | |||
283 | public function startTest(PHPUnit_Framework_Test $test) |
||
299 | |||
300 | View Code Duplication | public function endTest(PHPUnit_Framework_Test $test, $time) |
|
311 | |||
312 | public function send() |
||
350 | |||
351 | } |
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.