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 |
||
21 | class DbTest4DoQuery extends \PHPUnit_Extensions_Database_TestCase |
||
22 | { |
||
23 | //------------------------------------------------------// |
||
24 | // テストクラス変数定義 |
||
25 | //------------------------------------------------------// |
||
26 | /** |
||
27 | * $instance |
||
28 | * @access protected |
||
29 | * @var object テスト対象インスタンスを保持 |
||
30 | */ |
||
31 | protected $instance; |
||
32 | |||
33 | //------------------------------------------------------// |
||
34 | // テストメソッド定義 |
||
35 | //------------------------------------------------------// |
||
36 | /** |
||
37 | * setUp() |
||
38 | * |
||
39 | * テストに必要な準備を実施 |
||
40 | */ |
||
41 | View Code Duplication | protected function setUp() |
|
|
|||
42 | { |
||
43 | // 拡張モジュールがロードされているかをチェック |
||
44 | if (!extension_loaded( 'mysqli' )) { |
||
45 | $this->markTestSkipped( 'Cannot use mysqli expansion module.' ); |
||
46 | } |
||
47 | |||
48 | if (!isset( $GLOBALS[ 'DB_DRIVER' ] )) { |
||
49 | $this->markTestSkipped( 'DB_DRIVER was not defined. Check phpunit.xml' ); |
||
50 | } elseif (!isset( $GLOBALS[ 'DB_USER' ] )) { |
||
51 | $this->markTestSkipped( 'DB_USER was not defined. Check phpunit.xml' ); |
||
52 | } elseif (!isset( $GLOBALS[ 'DB_PASSWORD' ] )) { |
||
53 | $this->markTestSkipped( 'DB_PASSWORD was not defined. Check phpunit.xml' ); |
||
54 | } elseif (!isset( $GLOBALS[ 'DB_DBNAME' ] )) { |
||
55 | $this->markTestSkipped( 'DB_DBNAME was not defined. Check phpunit.xml' ); |
||
56 | } elseif (!isset( $GLOBALS[ 'DB_HOST' ] )) { |
||
57 | $this->markTestSkipped( 'DB_HOST was not defined. Check phpunit.xml' ); |
||
58 | } |
||
59 | |||
60 | // DB周りの初期化を行う為に元々のsetUp()をコールする |
||
61 | parent::setUp(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * getConnection() |
||
66 | * |
||
67 | * DBテストに必要な接続を実施 |
||
68 | */ |
||
69 | View Code Duplication | public function getConnection() |
|
70 | { |
||
71 | $dsn = $GLOBALS[ 'DB_DRIVER' ] . ':dbname=' . $GLOBALS[ 'DB_DBNAME' ] . ';host=' . $GLOBALS[ 'DB_HOST' ]; |
||
72 | $pdo = new \PDO( $dsn, $GLOBALS[ 'DB_USER' ], $GLOBALS[ 'DB_PASSWORD' ] ); |
||
73 | |||
74 | return $this->createDefaultDBConnection( $pdo, $GLOBALS[ 'DB_DBNAME' ] ); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * getDataSet() |
||
79 | * |
||
80 | * DBテストに必要なデータセットを実施 |
||
81 | */ |
||
82 | public function getDataSet() |
||
86 | |||
87 | /** |
||
88 | * testPreCondition() |
||
89 | * |
||
90 | * テスト開始前に前提条件をチェックする |
||
91 | */ |
||
92 | public function testPreCondition() |
||
96 | |||
97 | /** |
||
98 | * test_DoQuery_NoArgs() |
||
99 | * |
||
100 | * doQuery()のテスト(引数なし) |
||
101 | */ |
||
102 | View Code Duplication | public function test_DoQuery_NoArgs() |
|
121 | |||
122 | /** |
||
123 | * test_DoQuery_WithSql() |
||
124 | * |
||
125 | * doQuery()のテスト(SQLを指定) |
||
126 | */ |
||
127 | public function test_DoQuery_WithSql() |
||
154 | |||
155 | |||
156 | /** |
||
157 | * test_DoQuery_WithSqlAndParam() |
||
158 | * |
||
159 | * doQuery()のテスト(SQLを指定) |
||
160 | */ |
||
161 | public function test_DoQuery_WithSqlAndParam() |
||
208 | } |
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.