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 |
||
| 9 | trait Model |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The model's attributes. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $_attributes = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The model's database connection. |
||
| 20 | * |
||
| 21 | * @var Opeyemiabiodun\PotatoORM\Connections\Connection |
||
| 22 | */ |
||
| 23 | protected $_connection; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The primary key of the model's database table. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $_primaryKey; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The model's database table. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $_table; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The model's constructor method. |
||
| 41 | * |
||
| 42 | * @param Connection|null $connection An Opeyemiabiodun\PotatoORM\Connections\Connection instance or null |
||
| 43 | * @param string $table The name of the model's table in the database |
||
| 44 | */ |
||
| 45 | public function __construct(Connection $connection = null, $table = null) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The getter method for the model's properties. |
||
| 62 | * |
||
| 63 | * @param string $property The particular property |
||
| 64 | * |
||
| 65 | * @return int|float|string|bool The value of the property |
||
| 66 | */ |
||
| 67 | public function __get($property) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The setter method for the model's properties. |
||
| 78 | * |
||
| 79 | * @param string $property The particular property |
||
| 80 | * @param int|float|string|bool $value The value of the property |
||
| 81 | */ |
||
| 82 | public function __set($property, $value) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Deletes a specified instance of the model in the database. |
||
| 97 | * |
||
| 98 | * @param int $number Specifies which model instance to delete; the 1st, 2nd, 3rd, ..... |
||
| 99 | * |
||
| 100 | * @return bool Returns boolean true if the instance was successfully deleted or else it returns false. |
||
| 101 | */ |
||
| 102 | View Code Duplication | public static function destroy($number) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Finds a specified instance of the model in the database. |
||
| 113 | * |
||
| 114 | * @param int $number Specifies which model instance to find; the 1st, 2nd, 3rd, ..... |
||
| 115 | * |
||
| 116 | * @return array Returns the particular instance of the model. |
||
| 117 | */ |
||
| 118 | View Code Duplication | public static function find($number) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Returns all instances of the model in the database. |
||
| 129 | * |
||
| 130 | * @return array All instances of the model in the database. |
||
| 131 | */ |
||
| 132 | public static function getAll() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Checks the attributes of the model to ensure they are not all null. |
||
| 139 | * |
||
| 140 | * @return bool true if at least one of the models's attributes is not null else false. |
||
| 141 | */ |
||
| 142 | private function hasAttributes() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Saves or updates an instance of the model in the database. |
||
| 157 | * |
||
| 158 | * @return bool Returns true if the operation was successfully else returns false. |
||
| 159 | */ |
||
| 160 | public function save() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets the model's connection. |
||
| 183 | * |
||
| 184 | * @param Connection $connection An instance of Opeyemiabiodun\PotatoORM\Connections\Connection. |
||
| 185 | */ |
||
| 186 | protected static function setConnection(Connection $connection) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Sets the model's table. |
||
| 193 | * |
||
| 194 | * @param string $table An existing table in the database. |
||
| 195 | */ |
||
| 196 | protected static function setTable($table) |
||
| 211 | } |
||
| 212 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.