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 | // Database Test Helpers |
||
| 68 | //-------------------------------------------------------------------- |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Asserts that records that match the conditions in $where do |
||
| 72 | * not exist in the database. |
||
| 73 | * |
||
| 74 | * @param string $table |
||
| 75 | * @param array $where |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function dontSeeInDatabase($table, array $where) |
|
| 87 | |||
| 88 | //-------------------------------------------------------------------- |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Asserts that records that match the conditions in $where DO |
||
| 92 | * exist in the database. |
||
| 93 | * |
||
| 94 | * @param string $table |
||
| 95 | * @param array $where |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function seeInDatabase($table, array $where) |
|
| 107 | |||
| 108 | //-------------------------------------------------------------------- |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Fetches a single column from a database row with criteria |
||
| 112 | * matching $where. |
||
| 113 | * |
||
| 114 | * @param string $table |
||
| 115 | * @param string $column |
||
| 116 | * @param array $where |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function grabFromDatabase($table, $column, array $where) |
||
| 129 | |||
| 130 | //-------------------------------------------------------------------- |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Inserts a row into to the database. This row will be removed |
||
| 134 | * after the test has run. |
||
| 135 | * |
||
| 136 | * @param string $table |
||
| 137 | * @param array $data |
||
| 138 | * |
||
| 139 | */ |
||
| 140 | public function hasInDatabase($table, array $data) |
||
| 148 | |||
| 149 | //-------------------------------------------------------------------- |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Asserts that the number of rows in the database that match $where |
||
| 153 | * is equal to $expected. |
||
| 154 | * |
||
| 155 | * @param int $expected |
||
| 156 | * @param string $table |
||
| 157 | * @param array $where |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function seeNumRecords($expected, $table, array $where = []) |
||
| 169 | |||
| 170 | //-------------------------------------------------------------------- |
||
| 171 | |||
| 172 | } |
||
| 173 |
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.