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 $_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 | 8 | 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 | 2 | public function __get($property) |
|
74 | { |
||
75 | 2 | View Code Duplication | if (array_key_exists($property, $this->_attributes)) { |
76 | 1 | return $this->_attributes[$property]; |
|
77 | } else { |
||
78 | 1 | throw new PropertyNotFoundException('The '.get_class($this)." instance has no {$property} property."); |
|
79 | } |
||
80 | } |
||
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 | 4 | public function __set($property, $value) |
|
89 | { |
||
90 | 4 | if (! is_scalar($value)) { |
|
91 | 1 | throw new AssignmentException("Only scalar values can be assigned to the {$property} property."); |
|
92 | } |
||
93 | |||
94 | 3 | View Code Duplication | if (array_key_exists($property, $this->_attributes)) { |
95 | 2 | $this->_attributes[$property] = $value; |
|
96 | 2 | } else { |
|
97 | 1 | throw new PropertyNotFoundException('The '.get_class($this)." instance has no {$property} property."); |
|
98 | } |
||
99 | 2 | } |
|
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 | 3 | public static function destroy($number) |
|
109 | { |
||
110 | 3 | if (! is_int($number)) { |
|
111 | 1 | throw new InvalidArgumentException("The parameter {$number} is not an integer. An integer is required instead."); |
|
112 | } |
||
113 | |||
114 | 2 | if ($number <= 0) { |
|
115 | 1 | throw new InvalidArgumentException("The parameter {$number} is not a positive integer. A positive integer is required instead."); |
|
116 | } |
||
117 | |||
118 | 1 | return self::$_connection->deleteRecord(self::$_table, $number - 1); |
|
119 | } |
||
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 | 3 | public static function find($number) |
|
129 | { |
||
130 | 3 | if (! is_int($number)) { |
|
131 | 1 | throw new InvalidArgumentException("The parameter {$number} is not an integer. An integer is required instead."); |
|
132 | } |
||
133 | |||
134 | 2 | if ($number <= 0) { |
|
135 | 1 | throw new InvalidArgumentException("The parameter {$number} is not a positive integer. A positive integer is required instead."); |
|
136 | } |
||
137 | |||
138 | 1 | $result = new self(); |
|
139 | |||
140 | 1 | $record = self::$_connection->findRecord(self::$_table, $number - 1); |
|
141 | |||
142 | 1 | $result->setProperties($record[0]); |
|
143 | |||
144 | 1 | return $result; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Returns all instances of the model in the database. |
||
149 | * |
||
150 | * @return array All instances of the model in the database. |
||
151 | */ |
||
152 | 1 | public static function getAll() |
|
153 | { |
||
154 | 1 | $records = self::$_connection->getAllRecords(self::$_table); |
|
155 | |||
156 | 1 | $count = count($records); |
|
157 | 1 | $result = []; |
|
158 | |||
159 | 1 | for ($i = 0; $i < $count; $i++) { |
|
160 | 1 | array_push($result, new self($records[$i])); |
|
161 | 1 | } |
|
162 | |||
163 | 1 | return $result; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Checks the attributes of the model to ensure they are not all null. |
||
168 | * |
||
169 | * @return bool true if at least one of the models's attributes is not null else false. |
||
170 | */ |
||
171 | 3 | private function hasAttributes() |
|
172 | { |
||
173 | 3 | $hasAttributes = false; |
|
174 | |||
175 | 3 | foreach ($this->_attributes as $key => $value) { |
|
176 | 3 | if (! is_null($value)) { |
|
177 | 2 | $hasAttributes = true; |
|
178 | 2 | } |
|
179 | 3 | } |
|
180 | |||
181 | 3 | return $hasAttributes; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Saves or updates an instance of the model in the database. |
||
186 | * |
||
187 | * @return bool Returns true if the operation was successfully else returns false. |
||
188 | */ |
||
189 | 3 | public function save() |
|
205 | |||
206 | /** |
||
207 | * Sets the model's connection. |
||
208 | * |
||
209 | * @param Opeyemiabiodun\PotatoORM\Connections\Connection $connection An instance of Opeyemiabiodun\PotatoORM\Connections\Connection. |
||
210 | */ |
||
211 | 8 | protected function setConnection(Connection $connection) |
|
215 | |||
216 | /** |
||
217 | * Sets the model's properties. |
||
218 | * |
||
219 | * @param string $array An array containing the model's poperties. |
||
220 | */ |
||
221 | 2 | public function setProperties($array) |
|
229 | |||
230 | /** |
||
231 | * Sets the model's table. |
||
232 | * |
||
233 | * @param string $table An existing table in the database. |
||
234 | */ |
||
235 | 8 | protected function setTable($table) |
|
251 | } |
||
252 |
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: