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 |
||
19 | abstract class DriverTestAbstract extends TestAbstract{ |
||
20 | |||
21 | /** |
||
22 | * @var \chillerlan\Database\Drivers\DBDriverInterface |
||
23 | */ |
||
24 | protected $DBDriver; |
||
25 | |||
26 | |||
27 | protected $driver; |
||
28 | |||
29 | protected $SQL_RAW_TRUNCATE = 'DELETE FROM test'; |
||
30 | protected $SQL_RAW_SELECT_ALL = 'SELECT * FROM test'; |
||
31 | protected $SQL_RAW_DROP = 'DROP TABLE IF EXISTS test'; |
||
32 | protected $SQL_RAW_CREATE = 'CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL, hash VARCHAR(32) NOT NULL)'; |
||
33 | protected $SQL_RAW_INSERT = 'INSERT INTO test (id, hash) VALUES (%1$d, \'%2$s\')'; |
||
34 | protected $SQL_RAW_ERROR = '-'; |
||
35 | |||
36 | protected $SQL_PREPARED_INSERT = 'INSERT INTO test (id, hash) VALUES (?, ?)'; |
||
37 | |||
38 | protected $SQL_INDEX_COL = 'id'; |
||
39 | protected $SQL_DATA_COL = 'hash'; |
||
40 | |||
41 | protected function setUp(){ |
||
46 | |||
47 | protected function tearDown(){ |
||
50 | |||
51 | protected function resultTest(){ |
||
72 | |||
73 | public function testInstance(){ |
||
83 | |||
84 | public function testCreateTable(){ |
||
88 | |||
89 | public function testTruncate(){ |
||
93 | |||
94 | /** |
||
95 | * @expectedException \chillerlan\Database\DBException |
||
96 | * @expectedExceptionMessage sql error: |
||
97 | */ |
||
98 | public function testRawSQLError(){ |
||
101 | |||
102 | public function testRaw(){ |
||
111 | |||
112 | /** |
||
113 | * @expectedException \chillerlan\Database\DBException |
||
114 | * @expectedExceptionMessage sql error: |
||
115 | */ |
||
116 | public function testPreparedSQLError(){ |
||
119 | |||
120 | View Code Duplication | public function testPrepared(){ |
|
128 | |||
129 | /** |
||
130 | * @expectedException \chillerlan\Database\DBException |
||
131 | * @expectedExceptionMessage invalid data |
||
132 | */ |
||
133 | public function testMultiInvalidData(){ |
||
136 | |||
137 | /** |
||
138 | * @expectedException \chillerlan\Database\DBException |
||
139 | * @expectedExceptionMessage sql error: |
||
140 | */ |
||
141 | public function testMultiSQLError(){ |
||
144 | |||
145 | View Code Duplication | public function testMulti(){ |
|
153 | |||
154 | /** |
||
155 | * @expectedException \chillerlan\Database\DBException |
||
156 | * @expectedExceptionMessage invalid data |
||
157 | */ |
||
158 | public function testMultiCallbackInvalidData(){ |
||
161 | |||
162 | /** |
||
163 | * @expectedException \chillerlan\Database\DBException |
||
164 | * @expectedExceptionMessage invalid callback |
||
165 | */ |
||
166 | public function testMultiCallbackInvalidCallback(){ |
||
169 | |||
170 | /** |
||
171 | * @expectedException \chillerlan\Database\DBException |
||
172 | * @expectedExceptionMessage sql error: |
||
173 | */ |
||
174 | public function testMultiCallbackSQLError(){ |
||
177 | |||
178 | View Code Duplication | public function testMultiCallback(){ |
|
186 | } |
||
187 |
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.