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 |
||
6 | class ActionScheduler_UnitTestCase extends WP_UnitTestCase { |
||
7 | |||
8 | protected $existing_timezone; |
||
9 | |||
10 | /** |
||
11 | * Counts the number of test cases executed by run(TestResult result). |
||
12 | * |
||
13 | * @return int |
||
14 | */ |
||
15 | public function count() { |
||
16 | return 'UTC' == date_default_timezone_get() ? 2 : 3; |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * We want to run every test multiple times using a different timezone to make sure |
||
21 | * that they are unaffected by changes to PHP's timezone. |
||
22 | */ |
||
23 | public function run( PHPUnit\Framework\TestResult $result = NULL ){ |
||
24 | |||
25 | if ($result === NULL) { |
||
26 | $result = $this->createResult(); |
||
27 | } |
||
28 | |||
29 | if ( 'UTC' != ( $this->existing_timezone = date_default_timezone_get() ) ) { |
||
30 | date_default_timezone_set( 'UTC' ); |
||
31 | $result->run( $this ); |
||
32 | } |
||
33 | |||
34 | date_default_timezone_set( 'Pacific/Fiji' ); // UTC+12 |
||
35 | $result->run( $this ); |
||
36 | |||
37 | date_default_timezone_set( 'Pacific/Tahiti' ); // UTC-10: it's a magical place |
||
38 | $result->run( $this ); |
||
39 | |||
40 | date_default_timezone_set( $this->existing_timezone ); |
||
41 | |||
42 | return $result; |
||
43 | } |
||
44 | } |
||
45 |