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 |
||
| 19 | class ObjectRecordAccessor extends Behavior |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $record; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $instance = Record::class; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @inheritdoc |
||
| 34 | * |
||
| 35 | * @throws InvalidConfigException if the behavior was not configured properly |
||
| 36 | */ |
||
| 37 | public function init() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | protected function getRecordClass() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param array $config |
||
| 58 | * @return \yii\db\ActiveQuery |
||
| 59 | */ |
||
| 60 | public function getRecordQuery($config = []): ActiveQuery |
||
| 80 | |||
| 81 | /******************************************* |
||
| 82 | * CREATE |
||
| 83 | *******************************************/ |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param array $attributes |
||
| 87 | * @param string $toScenario |
||
| 88 | * @return Record |
||
| 89 | */ |
||
| 90 | public function createRecord(array $attributes = [], string $toScenario = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $condition |
||
| 115 | * @param string $toScenario |
||
| 116 | * @return Record|null |
||
| 117 | */ |
||
| 118 | public function findRecordByCondition($condition, string $toScenario = null) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $criteria |
||
| 134 | * @param string $toScenario |
||
| 135 | * @return Record |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function findRecordByCriteria($criteria, string $toScenario = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param $condition |
||
| 158 | * @param string $toScenario |
||
| 159 | * @return Record |
||
| 160 | * @throws RecordNotFoundException |
||
| 161 | */ |
||
| 162 | public function getRecordByCondition($condition, string $toScenario = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param $criteria |
||
| 177 | * @param string $toScenario |
||
| 178 | * @return Record |
||
| 179 | * @throws RecordNotFoundException |
||
| 180 | */ |
||
| 181 | public function getRecordByCriteria($criteria, string $toScenario = null) |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $toScenario |
||
| 197 | * @return Record[] |
||
| 198 | */ |
||
| 199 | public function findAllRecords(string $toScenario = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param array $condition |
||
| 206 | * @param string $toScenario |
||
| 207 | * @return Record[] |
||
| 208 | */ |
||
| 209 | public function findAllRecordsByCondition($condition = [], string $toScenario = null) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param array $criteria |
||
| 221 | * @param string $toScenario |
||
| 222 | * @return Record[] |
||
| 223 | */ |
||
| 224 | View Code Duplication | public function findAllRecordsByCriteria($criteria = [], string $toScenario = null) |
|
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * @deprecated |
||
| 252 | * @param array $condition |
||
| 253 | * @param string $toScenario |
||
| 254 | * @return Record[] |
||
| 255 | * @throws RecordNotFoundException |
||
| 256 | */ |
||
| 257 | public function getAllRecords($condition = [], string $toScenario = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $condition |
||
| 271 | * @param string $toScenario |
||
| 272 | * @return Record[] |
||
| 273 | * @throws RecordNotFoundException |
||
| 274 | */ |
||
| 275 | public function getAllRecordsByCondition($condition = [], string $toScenario = null) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param array $criteria |
||
| 290 | * @param string $toScenario |
||
| 291 | * @return Record[] |
||
| 292 | * @throws RecordNotFoundException |
||
| 293 | */ |
||
| 294 | public function getAllRecordsByCriteria($criteria = [], string $toScenario = null) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param int $id |
||
| 309 | * @param string $toScenario |
||
| 310 | * @return Record|null |
||
| 311 | */ |
||
| 312 | public function findRecordById(int $id, string $toScenario = null) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param int $id |
||
| 324 | * @param string $toScenario |
||
| 325 | * @return Record |
||
| 326 | */ |
||
| 327 | public function getRecordById(int $id, string $toScenario = null): Record |
||
| 336 | |||
| 337 | /******************************************* |
||
| 338 | * EXCEPTIONS |
||
| 339 | *******************************************/ |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @throws RecordNotFoundException |
||
| 343 | */ |
||
| 344 | protected function notFoundRecordException() |
||
| 354 | |||
| 355 | } |
||
| 356 |
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.