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 | public function resetInstance() |
||
| 39 | |||
| 40 | protected function loadDependencies() |
||
| 49 | |||
| 50 | protected function setUp() |
||
| 54 | |||
| 55 | //-------------------------------------------------------------------- |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Takes care of any required cleanup after the test, like |
||
| 59 | * removing any rows inserted via $this->hasInDatabase() |
||
| 60 | */ |
||
| 61 | protected function tearDown() |
||
| 71 | |||
| 72 | //-------------------------------------------------------------------- |
||
| 73 | // Database Test Helpers |
||
| 74 | //-------------------------------------------------------------------- |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Asserts that records that match the conditions in $where do |
||
| 78 | * not exist in the database. |
||
| 79 | * |
||
| 80 | * @param string $table |
||
| 81 | * @param array $where |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function dontSeeInDatabase($table, array $where) |
|
| 93 | |||
| 94 | //-------------------------------------------------------------------- |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Asserts that records that match the conditions in $where DO |
||
| 98 | * exist in the database. |
||
| 99 | * |
||
| 100 | * @param string $table |
||
| 101 | * @param array $where |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function seeInDatabase($table, array $where) |
|
| 113 | |||
| 114 | //-------------------------------------------------------------------- |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Fetches a single column from a database row with criteria |
||
| 118 | * matching $where. |
||
| 119 | * |
||
| 120 | * @param string $table |
||
| 121 | * @param string $column |
||
| 122 | * @param array $where |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function grabFromDatabase($table, $column, array $where) |
||
| 135 | |||
| 136 | //-------------------------------------------------------------------- |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Inserts a row into to the database. This row will be removed |
||
| 140 | * after the test has run. |
||
| 141 | * |
||
| 142 | * @param string $table |
||
| 143 | * @param array $data |
||
| 144 | * |
||
| 145 | */ |
||
| 146 | public function hasInDatabase($table, array $data) |
||
| 154 | |||
| 155 | //-------------------------------------------------------------------- |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Asserts that the number of rows in the database that match $where |
||
| 159 | * is equal to $expected. |
||
| 160 | * |
||
| 161 | * @param int $expected |
||
| 162 | * @param string $table |
||
| 163 | * @param array $where |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function seeNumRecords($expected, $table, array $where = []) |
||
| 175 | |||
| 176 | //-------------------------------------------------------------------- |
||
| 177 | |||
| 178 | } |
||
| 179 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.