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 |
||
38 | View Code Duplication | class WebServerListener implements TestListener |
|
39 | { |
||
40 | /** @var WebServerListenerTrait */ |
||
41 | private $trait; |
||
42 | |||
43 | public function __construct() |
||
47 | |||
48 | /** |
||
49 | * Make sure the PHP built-in web server is running for tests with group |
||
50 | * 'webserver'. |
||
51 | */ |
||
52 | public function startTestSuite(TestSuite $suite) |
||
53 | { |
||
54 | $this->trait->startTestSuite($suite); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * We don't need these. |
||
59 | */ |
||
60 | public function endTestSuite(TestSuite $suite) |
||
61 | { |
||
62 | } |
||
63 | |||
64 | public function addError(Test $test, \Exception $e, $time) |
||
65 | { |
||
66 | } |
||
67 | |||
68 | public function addFailure(Test $test, AssertionFailedError $e, $time) |
||
69 | { |
||
70 | } |
||
71 | |||
72 | public function addIncompleteTest(Test $test, \Exception $e, $time) |
||
73 | { |
||
74 | } |
||
75 | |||
76 | public function addSkippedTest(Test $test, \Exception $e, $time) |
||
77 | { |
||
78 | } |
||
79 | |||
80 | public function startTest(Test $test) |
||
81 | { |
||
82 | } |
||
83 | |||
84 | public function endTest(Test $test, $time) |
||
85 | { |
||
86 | } |
||
87 | |||
88 | public function addRiskyTest(Test $test, \Exception $e, $time) |
||
89 | { |
||
90 | } |
||
91 | |||
92 | public function addWarning(Test $test, Warning $e, $time) |
||
95 | |||
96 | /** |
||
97 | * Get web server hostname. |
||
98 | * |
||
99 | * @throws \Exception |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | protected function getHostName() |
||
104 | { |
||
107 | |||
108 | /** |
||
109 | * Get web server port. |
||
110 | * |
||
111 | * @throws \Exception |
||
112 | * |
||
113 | * @return int |
||
114 | */ |
||
115 | protected function getPort() |
||
119 | |||
120 | /** |
||
121 | * Get web server port. |
||
122 | * |
||
123 | * @throws \Exception |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | protected function getDocRoot() |
||
131 | |||
132 | /** |
||
133 | * Start PHP built-in web server. |
||
134 | * |
||
135 | * @return int PID |
||
136 | */ |
||
137 | protected function startPhpWebServer() |
||
141 | |||
142 | /** |
||
143 | * Wait for caching proxy to be started up and reachable. |
||
144 | * |
||
145 | * @param string $ip |
||
146 | * @param int $port |
||
147 | * @param int $timeout Timeout in milliseconds |
||
148 | * |
||
149 | * @throws \RuntimeException If proxy is not reachable within timeout |
||
150 | */ |
||
151 | protected function waitFor($ip, $port, $timeout) |
||
155 | } |
||
156 | } |
||
157 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.