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 |
||
12 | class MasterListener implements \PHPUnit_Framework_TestListener |
||
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) |
||
32 | |||
33 | public function clear() |
||
39 | |||
40 | public function addListener(\PHPUnit_Framework_TestListener $listener) |
||
52 | |||
53 | public function play($method, $args, \PHPUnit_Framework_TestListener $instance = null) |
||
63 | |||
64 | public function bindToResult(\PHPUnit_Framework_TestResult $result) |
||
71 | |||
72 | public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) |
||
80 | |||
81 | View Code Duplication | public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) |
|
89 | |||
90 | public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
||
98 | |||
99 | public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
||
107 | |||
108 | public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
||
116 | |||
117 | View Code Duplication | public function startTestSuite(PHPUnit_Framework_TestSuite $suite) |
|
125 | |||
126 | View Code Duplication | public function endTestSuite(PHPUnit_Framework_TestSuite $suite) |
|
134 | |||
135 | View Code Duplication | public function startTest(PHPUnit_Framework_Test $test) |
|
143 | |||
144 | View Code Duplication | public function endTest(PHPUnit_Framework_Test $test, $time) |
|
152 | |||
153 | } |
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.