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 | abstract class DriverTestAbstract extends TestAbstract{ |
||
22 | |||
23 | /** |
||
24 | * @var \chillerlan\Database\Drivers\DBDriverInterface |
||
25 | */ |
||
26 | protected $DBDriver; |
||
27 | |||
28 | /** |
||
29 | * @var \Psr\SimpleCache\CacheInterface |
||
30 | */ |
||
31 | protected $Cache; |
||
32 | |||
33 | protected $driver; |
||
34 | |||
35 | protected $SQL_RAW_TRUNCATE = 'DELETE FROM test'; |
||
36 | protected $SQL_RAW_SELECT_ALL = 'SELECT * FROM test'; |
||
37 | protected $SQL_RAW_DROP = 'DROP TABLE IF EXISTS test'; |
||
38 | protected $SQL_RAW_CREATE = 'CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL, hash VARCHAR(32) NOT NULL)'; |
||
39 | protected $SQL_RAW_INSERT = 'INSERT INTO test (id, hash) VALUES (%1$d, \'%2$s\')'; |
||
40 | protected $SQL_RAW_ERROR = '-'; |
||
41 | |||
42 | protected $SQL_PREPARED_INSERT = 'INSERT INTO test (id, hash) VALUES (?, ?)'; |
||
43 | |||
44 | protected $SQL_INDEX_COL = 'id'; |
||
45 | protected $SQL_DATA_COL = 'hash'; |
||
46 | |||
47 | protected $SQL_ESCAPED; |
||
48 | |||
49 | protected function setUp(){ |
||
55 | |||
56 | protected function tearDown(){ |
||
59 | |||
60 | public function testInstance(){ |
||
73 | |||
74 | /** |
||
75 | * @expectedException \chillerlan\Database\DBException |
||
76 | * @expectedExceptionMessage db error: |
||
77 | */ |
||
78 | public function testConnectDBconnectError(){ |
||
83 | |||
84 | // coverage |
||
85 | public function testEscape(){ |
||
88 | |||
89 | public function testCreateTable(){ |
||
93 | |||
94 | public function testTruncate(){ |
||
98 | |||
99 | /** |
||
100 | * @expectedException \chillerlan\Database\DBException |
||
101 | * @expectedExceptionMessage sql error: |
||
102 | */ |
||
103 | public function testRawSQLError(){ |
||
106 | |||
107 | protected function resultTest(){ |
||
132 | |||
133 | public function testRaw(){ |
||
142 | |||
143 | /** |
||
144 | * @expectedException \chillerlan\Database\DBException |
||
145 | * @expectedExceptionMessage sql error: |
||
146 | */ |
||
147 | public function testPreparedSQLError(){ |
||
150 | |||
151 | View Code Duplication | public function testPrepared(){ |
|
159 | |||
160 | /** |
||
161 | * @expectedException \chillerlan\Database\DBException |
||
162 | * @expectedExceptionMessage invalid data |
||
163 | */ |
||
164 | public function testMultiInvalidData(){ |
||
167 | |||
168 | /** |
||
169 | * @expectedException \chillerlan\Database\DBException |
||
170 | * @expectedExceptionMessage sql error: |
||
171 | */ |
||
172 | public function testMultiSQLError(){ |
||
175 | |||
176 | View Code Duplication | public function testMulti(){ |
|
190 | |||
191 | /** |
||
192 | * @expectedException \chillerlan\Database\DBException |
||
193 | * @expectedExceptionMessage invalid data |
||
194 | */ |
||
195 | public function testMultiCallbackInvalidData(){ |
||
200 | |||
201 | /** |
||
202 | * @expectedException \chillerlan\Database\DBException |
||
203 | * @expectedExceptionMessage invalid callback |
||
204 | */ |
||
205 | public function testMultiCallbackInvalidCallback(){ |
||
208 | |||
209 | /** |
||
210 | * @expectedException \chillerlan\Database\DBException |
||
211 | * @expectedExceptionMessage sql error: |
||
212 | */ |
||
213 | public function testMultiCallbackSQLError(){ |
||
219 | |||
220 | View Code Duplication | public function testMultiCallback(){ |
|
229 | |||
230 | public function testCached(){ |
||
266 | |||
267 | } |
||
268 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.