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:
Complex classes like LocalhostMultiTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LocalhostMultiTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class LocalhostMultiTest extends LocalhostTest |
||
15 | { |
||
16 | /** |
||
17 | * Returns all test methods from the base class, except the ones which failed already |
||
18 | * |
||
19 | * @todo reintroduce skipping of tests which failed when executed individually if test runs happen as separate processes |
||
20 | * @todo reintroduce skipping of tests within the loop |
||
21 | */ |
||
22 | public function getSingleHttpTestMethods() |
||
42 | |||
43 | /** |
||
44 | * @dataProvider getSingleHttpTestMethods |
||
45 | * @param string $method |
||
46 | */ |
||
47 | View Code Duplication | public function testDeflate($method) |
|
60 | |||
61 | /** |
||
62 | * @dataProvider getSingleHttpTestMethods |
||
63 | * @param string $method |
||
64 | */ |
||
65 | View Code Duplication | public function testGzip($method) |
|
78 | |||
79 | public function testKeepAlives() |
||
97 | |||
98 | /** |
||
99 | * @dataProvider getSingleHttpTestMethods |
||
100 | * @param string $method |
||
101 | */ |
||
102 | public function testProxy($method) |
||
114 | |||
115 | /** |
||
116 | * @dataProvider getSingleHttpTestMethods |
||
117 | * @param string $method |
||
118 | */ |
||
119 | View Code Duplication | public function testHttp11($method) |
|
133 | |||
134 | /** |
||
135 | * @dataProvider getSingleHttpTestMethods |
||
136 | * @param string $method |
||
137 | */ |
||
138 | View Code Duplication | public function testHttp10Curl($method) |
|
153 | |||
154 | /** |
||
155 | * @dataProvider getSingleHttpTestMethods |
||
156 | * @param string $method |
||
157 | */ |
||
158 | View Code Duplication | public function testHttp11Gzip($method) |
|
173 | |||
174 | /** |
||
175 | * @dataProvider getSingleHttpTestMethods |
||
176 | * @param string $method |
||
177 | */ |
||
178 | View Code Duplication | public function testHttp11Deflate($method) |
|
193 | |||
194 | /** |
||
195 | * @dataProvider getSingleHttpTestMethods |
||
196 | * @param string $method |
||
197 | */ |
||
198 | public function testHttp11Proxy($method) |
||
218 | |||
219 | /** |
||
220 | * @dataProvider getSingleHttpTestMethods |
||
221 | * @param string $method |
||
222 | */ |
||
223 | public function testHttps($method) |
||
246 | |||
247 | /** |
||
248 | * @dataProvider getSingleHttpTestMethods |
||
249 | * @param string $method |
||
250 | */ |
||
251 | public function testHttpsSocket($method) |
||
270 | |||
271 | /** |
||
272 | * @dataProvider getSingleHttpTestMethods |
||
273 | * @param string $method |
||
274 | */ |
||
275 | public function testHttpsProxy($method) |
||
304 | |||
305 | /** |
||
306 | * @dataProvider getSingleHttpTestMethods |
||
307 | * @param string $method |
||
308 | */ |
||
309 | public function testUTF8Responses($method) |
||
315 | |||
316 | /** |
||
317 | * @dataProvider getSingleHttpTestMethods |
||
318 | * @param string $method |
||
319 | */ |
||
320 | public function testUTF8Requests($method) |
||
326 | |||
327 | /** |
||
328 | * @dataProvider getSingleHttpTestMethods |
||
329 | * @param string $method |
||
330 | */ |
||
331 | public function testISOResponses($method) |
||
337 | |||
338 | /** |
||
339 | * @dataProvider getSingleHttpTestMethods |
||
340 | * @param string $method |
||
341 | */ |
||
342 | public function testISORequests($method) |
||
348 | |||
349 | /** |
||
350 | * @dataProvider getSingleHttpTestMethods |
||
351 | * @param string $method |
||
352 | */ |
||
353 | public function testBasicAuth($method) |
||
360 | |||
361 | /** |
||
362 | * @dataProvider getSingleHttpTestMethods |
||
363 | * @param string $method |
||
364 | */ |
||
365 | public function testDigestAuth($method) |
||
380 | } |
||
381 |
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.