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 |
||
| 22 | trait Object |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @return string |
||
| 27 | */ |
||
| 28 | public abstract static function recordClass(): string; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | public static function recordClassInstance(): string |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param array $config |
||
| 40 | * @return \yii\db\ActiveQuery |
||
| 41 | */ |
||
| 42 | public function getRecordQuery($config = []): ActiveQuery |
||
| 62 | |||
| 63 | /******************************************* |
||
| 64 | * CREATE |
||
| 65 | *******************************************/ |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param array $attributes |
||
| 69 | * @param string $toScenario |
||
| 70 | * @return Record |
||
| 71 | */ |
||
| 72 | public function createRecord(array $attributes = [], string $toScenario = null) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param $condition |
||
| 97 | * @param string $toScenario |
||
| 98 | * @return Record|null |
||
| 99 | */ |
||
| 100 | public function findRecordByCondition($condition, string $toScenario = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param $criteria |
||
| 116 | * @param string $toScenario |
||
| 117 | * @return Record |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function findRecordByCriteria($criteria, string $toScenario = null) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param $condition |
||
| 140 | * @param string $toScenario |
||
| 141 | * @return Record |
||
| 142 | * @throws RecordNotFoundException |
||
| 143 | */ |
||
| 144 | public function getRecordByCondition($condition, string $toScenario = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $criteria |
||
| 159 | * @param string $toScenario |
||
| 160 | * @return Record |
||
| 161 | * @throws RecordNotFoundException |
||
| 162 | */ |
||
| 163 | public function getRecordByCriteria($criteria, string $toScenario = null) |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $toScenario |
||
| 179 | * @return Record[] |
||
| 180 | */ |
||
| 181 | public function findAllRecords(string $toScenario = null) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param array $condition |
||
| 188 | * @param string $toScenario |
||
| 189 | * @return Record[] |
||
| 190 | */ |
||
| 191 | public function findAllRecordsByCondition($condition = [], string $toScenario = null) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param array $criteria |
||
| 203 | * @param string $toScenario |
||
| 204 | * @return Record[] |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function findAllRecordsByCriteria($criteria = [], string $toScenario = null) |
|
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @deprecated |
||
| 234 | * @param array $condition |
||
| 235 | * @param string $toScenario |
||
| 236 | * @return Record[] |
||
| 237 | * @throws RecordNotFoundException |
||
| 238 | */ |
||
| 239 | public function getAllRecords($condition = [], string $toScenario = null) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param array $condition |
||
| 253 | * @param string $toScenario |
||
| 254 | * @return Record[] |
||
| 255 | * @throws RecordNotFoundException |
||
| 256 | */ |
||
| 257 | public function getAllRecordsByCondition($condition = [], string $toScenario = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $criteria |
||
| 272 | * @param string $toScenario |
||
| 273 | * @return Record[] |
||
| 274 | * @throws RecordNotFoundException |
||
| 275 | */ |
||
| 276 | public function getAllRecordsByCriteria($criteria = [], string $toScenario = null) |
||
| 288 | |||
| 289 | /******************************************* |
||
| 290 | * EXCEPTIONS |
||
| 291 | *******************************************/ |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @throws RecordNotFoundException |
||
| 295 | */ |
||
| 296 | protected function notFoundRecordException() |
||
| 306 | |||
| 307 | } |
||
| 308 |
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.