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 |
||
24 | trait ObjectTrait |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * @return string |
||
29 | */ |
||
30 | public abstract static function recordClass(): string; |
||
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | public static function recordClassInstance(): string |
||
39 | |||
40 | /** |
||
41 | * @param array $config |
||
42 | * @return \yii\db\ActiveQuery |
||
43 | */ |
||
44 | public function getRecordQuery($config = []): ActiveQuery |
||
64 | |||
65 | /******************************************* |
||
66 | * CREATE |
||
67 | *******************************************/ |
||
68 | |||
69 | /** |
||
70 | * @param array $attributes |
||
71 | * @param string $toScenario |
||
72 | * @return Record |
||
73 | */ |
||
74 | public function createRecord(array $attributes = [], string $toScenario = null) |
||
96 | |||
97 | /** |
||
98 | * @param $condition |
||
99 | * @param string $toScenario |
||
100 | * @return Record|null |
||
101 | */ |
||
102 | public function findRecordByCondition($condition, string $toScenario = null) |
||
115 | |||
116 | /** |
||
117 | * @param $criteria |
||
118 | * @param string $toScenario |
||
119 | * @return Record |
||
120 | */ |
||
121 | View Code Duplication | public function findRecordByCriteria($criteria, string $toScenario = null) |
|
139 | |||
140 | /** |
||
141 | * @param $condition |
||
142 | * @param string $toScenario |
||
143 | * @return Record |
||
144 | * @throws RecordNotFoundException |
||
145 | */ |
||
146 | public function getRecordByCondition($condition, string $toScenario = null) |
||
158 | |||
159 | /** |
||
160 | * @param $criteria |
||
161 | * @param string $toScenario |
||
162 | * @return Record |
||
163 | * @throws RecordNotFoundException |
||
164 | */ |
||
165 | public function getRecordByCriteria($criteria, string $toScenario = null) |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @param string $toScenario |
||
181 | * @return Record[] |
||
182 | */ |
||
183 | public function findAllRecords(string $toScenario = null) |
||
187 | |||
188 | /** |
||
189 | * @param array $condition |
||
190 | * @param string $toScenario |
||
191 | * @return Record[] |
||
192 | */ |
||
193 | public function findAllRecordsByCondition($condition = [], string $toScenario = null) |
||
202 | |||
203 | /** |
||
204 | * @param array $criteria |
||
205 | * @param string $toScenario |
||
206 | * @return Record[] |
||
207 | */ |
||
208 | View Code Duplication | public function findAllRecordsByCriteria($criteria = [], string $toScenario = null) |
|
232 | |||
233 | |||
234 | /** |
||
235 | * @deprecated |
||
236 | * @param array $condition |
||
237 | * @param string $toScenario |
||
238 | * @return Record[] |
||
239 | * @throws RecordNotFoundException |
||
240 | */ |
||
241 | public function getAllRecords($condition = [], string $toScenario = null) |
||
252 | |||
253 | /** |
||
254 | * @param array $condition |
||
255 | * @param string $toScenario |
||
256 | * @return Record[] |
||
257 | * @throws RecordNotFoundException |
||
258 | */ |
||
259 | public function getAllRecordsByCondition($condition = [], string $toScenario = null) |
||
271 | |||
272 | /** |
||
273 | * @param array $criteria |
||
274 | * @param string $toScenario |
||
275 | * @return Record[] |
||
276 | * @throws RecordNotFoundException |
||
277 | */ |
||
278 | public function getAllRecordsByCriteria($criteria = [], string $toScenario = null) |
||
290 | |||
291 | /******************************************* |
||
292 | * EXCEPTIONS |
||
293 | *******************************************/ |
||
294 | |||
295 | /** |
||
296 | * @throws RecordNotFoundException |
||
297 | */ |
||
298 | protected function notFoundRecordException() |
||
308 | |||
309 | } |
||
310 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.