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 |
||
| 11 | class ActiveRecordModel |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string TABLE_NAME name of the database table. |
||
| 15 | */ |
||
| 16 | protected $tableName = null; |
||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor. |
||
| 22 | * |
||
| 23 | * @param DatabaseQueryBuilder $db as database access object. |
||
| 24 | */ |
||
| 25 | public function __construct(DatabaseQueryBuilder $db) |
||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Get essential object properties. |
||
| 34 | * |
||
| 35 | * @return array with object properties. |
||
| 36 | */ |
||
| 37 | private function getProperties() |
||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Set object properties. |
||
| 50 | * |
||
| 51 | * @param array $properties with properties to set. |
||
| 52 | * |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | // private function setProperties($properties) |
||
| 56 | // { |
||
| 57 | // if (!empty($properties)) { |
||
| 58 | // foreach ($properties as $key => $val) { |
||
| 59 | // $this->$key = $val; |
||
| 60 | // } |
||
| 61 | // } |
||
| 62 | // } |
||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Find and return first object found by search criteria and use |
||
| 68 | * its data to populate this instance. |
||
| 69 | * |
||
| 70 | * @param string $column to use in where statement. |
||
| 71 | * @param mixed $value to use in where statement. |
||
| 72 | * |
||
| 73 | * @return this |
||
| 74 | */ |
||
| 75 | public function find($column, $value) |
||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Find and return all. |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function findAll() |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Save current object/row, insert if id is missing and do an |
||
| 106 | * update if the id exists. |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public function save() |
||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Create new row. |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | View Code Duplication | private function create() |
|
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Update row. |
||
| 145 | * |
||
| 146 | * @param array $values key/values to save. |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | View Code Duplication | private function update($values) |
|
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Delete row. |
||
| 169 | * |
||
| 170 | * @param integer $id to delete or use $this->id as default. |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function delete($id = null) |
||
| 186 | } |
||
| 187 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: