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 |
||
| 12 | class ActiveRecordModel |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var DatabaseQueryBuilder $db the object for persistent |
||
| 16 | * storage. |
||
| 17 | */ |
||
| 18 | protected $db = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string $tableName name of the database table. |
||
| 22 | */ |
||
| 23 | protected $tableName = null; |
||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Set the database object to use for accessing storage. |
||
| 29 | * |
||
| 30 | * @param DatabaseQueryBuilder $db as database access object. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function setDb(DatabaseQueryBuilder $db) |
||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Check if database is injected or throw an exception. |
||
| 43 | * |
||
| 44 | * @throws ActiveRecordException when database is not set. |
||
| 45 | * |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | protected function checkDb() |
||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Get essential object properties. |
||
| 59 | * |
||
| 60 | * @return array with object properties. |
||
| 61 | */ |
||
| 62 | private function getProperties() |
||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Find and return first object found by search criteria and use |
||
| 75 | * its data to populate this instance. |
||
| 76 | * |
||
| 77 | * @param string $column to use in where statement. |
||
| 78 | * @param mixed $value to use in where statement. |
||
| 79 | * |
||
| 80 | * @return this |
||
| 81 | */ |
||
| 82 | public function find($column, $value) |
||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Find and return all. |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function findAll() |
||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Save current object/row, insert if id is missing and do an |
||
| 114 | * update if the id exists. |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | public function save() |
||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * Create new row. |
||
| 131 | * |
||
| 132 | * @return void |
||
| 133 | */ |
||
| 134 | View Code Duplication | private function create() |
|
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Update row. |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | View Code Duplication | private function update() |
|
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Delete row. |
||
| 175 | * |
||
| 176 | * @param integer $id to delete or use $this->id as default. |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function delete($id = null) |
||
| 192 | } |
||
| 193 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: