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 CodeigniterModel extends \CI_Model |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Defines an inverse one-to-one or many relationship. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $_belongs_to = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Columns that will be displayed. |
||
| 22 | * If not set, it will get the columns from the database table. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $_columns = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Columns that will be hidden in the display. |
||
| 30 | * If not set, it will hide a "password" column if it exists. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $_hidden = [ 'password' ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The table associated with the model. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $_table = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \Rougin\Wildfire\Wildfire |
||
| 45 | */ |
||
| 46 | private $_wildfire; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $_with = []; |
||
| 52 | |||
| 53 | public function __construct() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns all of the models from the database. |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function all() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Deletes the specified ID of the model from the database. |
||
| 74 | * |
||
| 75 | * @param integer $id |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function delete($id) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Finds the specified model from the database. |
||
| 85 | * |
||
| 86 | * @param integer $id |
||
| 87 | * @return mixed |
||
| 88 | */ |
||
| 89 | public function find($id) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Finds the specified model from the database by the given delimiters. |
||
| 96 | * |
||
| 97 | * @param array $delimiters |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | public function findBy(array $delimiters) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns all rows from the specified table. |
||
| 109 | * |
||
| 110 | * @return self |
||
| 111 | */ |
||
| 112 | public function get() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns "belongs to" relationships. |
||
| 119 | * |
||
| 120 | * @return |
||
| 121 | */ |
||
| 122 | public function getBelongsToRelationships() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns the specified columns of the model. |
||
| 129 | * |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function getColumns() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the specified hidden columns of the model. |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function getHiddenColumns() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Gets the defined relationships. |
||
| 149 | * |
||
| 150 | * @return |
||
| 151 | */ |
||
| 152 | public function getRelationships() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Gets the specified table name of the model. |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function getTableName() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Adds a relationship/s to the model. |
||
| 173 | * |
||
| 174 | * @param string|array $relationships |
||
| 175 | * @return self |
||
| 176 | */ |
||
| 177 | public function with($relationships) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Calls methods from this class in underscore case. |
||
| 192 | * |
||
| 193 | * @param string $method |
||
| 194 | * @param mixed $parameters |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | View Code Duplication | public function __call($method, $parameters) |
|
| 210 | } |
||
| 211 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.