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 |
||
22 | class CIPHPUnitTestDbTestCase extends CIPHPUnitTestCase |
||
23 | { |
||
24 | protected $db; |
||
25 | |||
26 | /** |
||
27 | * Stores information needed to remove any |
||
28 | * rows inserted via $this->hasInDatabase(); |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $insertCache = []; |
||
33 | |||
34 | protected function loadDependencies() |
||
43 | |||
44 | protected function setUp() |
||
48 | |||
49 | //-------------------------------------------------------------------- |
||
50 | |||
51 | /** |
||
52 | * Takes care of any required cleanup after the test, like |
||
53 | * removing any rows inserted via $this->hasInDatabase() |
||
54 | */ |
||
55 | protected function tearDown() |
||
65 | |||
66 | /** |
||
67 | * Workaround for the following error |
||
68 | * |
||
69 | * Error: Call to a member function quote() on boolean |
||
70 | * vendor/codeigniter/framework/system/database/drivers/pdo/pdo_driver.php:234 |
||
71 | * |
||
72 | * I don't know why, but when I call $this->seeInDatabase() after $this->request(), |
||
73 | * I got it |
||
74 | */ |
||
75 | private function checkDbConnId() |
||
88 | |||
89 | //-------------------------------------------------------------------- |
||
90 | // Database Test Helpers |
||
91 | //-------------------------------------------------------------------- |
||
92 | |||
93 | /** |
||
94 | * Asserts that records that match the conditions in $where do |
||
95 | * not exist in the database. |
||
96 | * |
||
97 | * @param string $table |
||
98 | * @param array $where |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | View Code Duplication | public function dontSeeInDatabase($table, array $where) |
|
112 | |||
113 | //-------------------------------------------------------------------- |
||
114 | |||
115 | /** |
||
116 | * Asserts that records that match the conditions in $where DO |
||
117 | * exist in the database. |
||
118 | * |
||
119 | * @param string $table |
||
120 | * @param array $where |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | View Code Duplication | public function seeInDatabase($table, array $where) |
|
134 | |||
135 | //-------------------------------------------------------------------- |
||
136 | |||
137 | /** |
||
138 | * Fetches a single column from a database row with criteria |
||
139 | * matching $where. |
||
140 | * |
||
141 | * @param string $table |
||
142 | * @param string $column |
||
143 | * @param array $where |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function grabFromDatabase($table, $column, array $where) |
||
158 | |||
159 | //-------------------------------------------------------------------- |
||
160 | |||
161 | /** |
||
162 | * Inserts a row into to the database. This row will be removed |
||
163 | * after the test has run. |
||
164 | * |
||
165 | * @param string $table |
||
166 | * @param array $data |
||
167 | * |
||
168 | */ |
||
169 | public function hasInDatabase($table, array $data) |
||
179 | |||
180 | //-------------------------------------------------------------------- |
||
181 | |||
182 | /** |
||
183 | * Asserts that the number of rows in the database that match $where |
||
184 | * is equal to $expected. |
||
185 | * |
||
186 | * @param int $expected |
||
187 | * @param string $table |
||
188 | * @param array $where |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | View Code Duplication | public function seeNumRecords($expected, $table, array $where = []) |
|
202 | |||
203 | //-------------------------------------------------------------------- |
||
204 | |||
205 | } |
||
206 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.