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 | trait Model |
||
13 | { |
||
14 | /** |
||
15 | * The model's attributes. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected static $_attributes = []; |
||
20 | |||
21 | /** |
||
22 | * The model's database connection. |
||
23 | * |
||
24 | * @var Opeyemiabiodun\PotatoORM\Connections\Connection |
||
25 | */ |
||
26 | protected static $_connection; |
||
27 | |||
28 | /** |
||
29 | * The primary key of the model's database table. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected static $_primaryKey; |
||
34 | |||
35 | /** |
||
36 | * The model's database table. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected static $_table; |
||
41 | |||
42 | /** |
||
43 | * The model's constructor method. |
||
44 | * |
||
45 | * @param Connection|null $connection An Opeyemiabiodun\PotatoORM\Connections\Connection instance or null |
||
46 | * @param string $table The name of the model's table in the database |
||
47 | */ |
||
48 | public function __construct($array = [], Connection $connection = null, $table = null) |
||
65 | |||
66 | /** |
||
67 | * The getter method for the model's properties. |
||
68 | * |
||
69 | * @param string $property The particular property |
||
70 | * |
||
71 | * @return int|float|string|bool The value of the property |
||
72 | */ |
||
73 | public function __get($property) |
||
81 | |||
82 | /** |
||
83 | * The setter method for the model's properties. |
||
84 | * |
||
85 | * @param string $property The particular property |
||
86 | * @param int|float|string|bool $value The value of the property |
||
87 | */ |
||
88 | public function __set($property, $value) |
||
100 | |||
101 | /** |
||
102 | * Deletes a specified instance of the model in the database. |
||
103 | * |
||
104 | * @param int $number Specifies which model instance to delete; the 1st, 2nd, 3rd, ..... |
||
105 | * |
||
106 | * @return bool Returns boolean true if the instance was successfully deleted or else it returns false. |
||
107 | */ |
||
108 | View Code Duplication | public static function destroy($number) |
|
120 | |||
121 | /** |
||
122 | * Finds a specified instance of the model in the database. |
||
123 | * |
||
124 | * @param int $number Specifies which model instance to find; the 1st, 2nd, 3rd, ..... |
||
125 | * |
||
126 | * @return array Returns the particular instance of the model. |
||
127 | */ |
||
128 | View Code Duplication | public static function find($number) |
|
142 | |||
143 | /** |
||
144 | * Returns all instances of the model in the database. |
||
145 | * |
||
146 | * @return array All instances of the model in the database. |
||
147 | */ |
||
148 | public static function getAll() |
||
152 | |||
153 | /** |
||
154 | * Checks the attributes of the model to ensure they are not all null. |
||
155 | * |
||
156 | * @return bool true if at least one of the models's attributes is not null else false. |
||
157 | */ |
||
158 | private function hasAttributes() |
||
170 | |||
171 | /** |
||
172 | * Saves or updates an instance of the model in the database. |
||
173 | * |
||
174 | * @return bool Returns true if the operation was successfully else returns false. |
||
175 | */ |
||
176 | public function save() |
||
192 | |||
193 | /** |
||
194 | * Sets the model's connection. |
||
195 | * |
||
196 | * @param Connection $connection An instance of Opeyemiabiodun\PotatoORM\Connections\Connection. |
||
197 | */ |
||
198 | protected function setConnection(Connection $connection) |
||
202 | |||
203 | protected function setProperties($array) |
||
213 | |||
214 | /** |
||
215 | * Sets the model's table. |
||
216 | * |
||
217 | * @param string $table An existing table in the database. |
||
218 | */ |
||
219 | protected function setTable($table) |
||
235 | } |
||
236 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.