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 |
||
| 30 | View Code Duplication | class WebServerListener implements TestListener |
|
| 31 | { |
||
| 32 | /** @var WebServerListenerTrait */ |
||
| 33 | private $trait; |
||
| 34 | |||
| 35 | public function __construct() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Make sure the PHP built-in web server is running for tests with group |
||
| 42 | * 'webserver'. |
||
| 43 | */ |
||
| 44 | public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * We don't need these. |
||
| 51 | */ |
||
| 52 | public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) |
||
| 55 | |||
| 56 | public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
||
| 59 | |||
| 60 | public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) |
||
| 63 | |||
| 64 | public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
||
| 67 | |||
| 68 | public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
||
| 71 | |||
| 72 | public function startTest(\PHPUnit_Framework_Test $test) |
||
| 75 | |||
| 76 | public function endTest(\PHPUnit_Framework_Test $test, $time) |
||
| 79 | |||
| 80 | public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get web server hostname. |
||
| 86 | * |
||
| 87 | * @throws \Exception |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | protected function getHostName() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get web server port. |
||
| 98 | * |
||
| 99 | * @throws \Exception |
||
| 100 | * |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | protected function getPort() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get web server port. |
||
| 110 | * |
||
| 111 | * @throws \Exception |
||
| 112 | * |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | protected function getDocRoot() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Start PHP built-in web server. |
||
| 122 | * |
||
| 123 | * @return int PID |
||
| 124 | */ |
||
| 125 | protected function startPhpWebServer() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Wait for caching proxy to be started up and reachable. |
||
| 132 | * |
||
| 133 | * @param string $ip |
||
| 134 | * @param int $port |
||
| 135 | * @param int $timeout Timeout in milliseconds |
||
| 136 | * |
||
| 137 | * @throws \RuntimeException If proxy is not reachable within timeout |
||
| 138 | */ |
||
| 139 | protected function waitFor($ip, $port, $timeout) |
||
| 143 | } |
||
| 144 |