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 |
||
| 25 | class ObjectRecordAccessor extends Behavior |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | public $record; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | public $instance = Record::class; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @inheritdoc |
||
| 40 | * |
||
| 41 | * @throws InvalidConfigException if the behavior was not configured properly |
||
| 42 | */ |
||
| 43 | public function init() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | protected function getRecordClass() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array $config |
||
| 64 | * @return \yii\db\ActiveQuery |
||
| 65 | */ |
||
| 66 | public function getRecordQuery($config = []): ActiveQuery |
||
| 86 | |||
| 87 | /******************************************* |
||
| 88 | * CREATE |
||
| 89 | *******************************************/ |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param array $attributes |
||
| 93 | * @param string $toScenario |
||
| 94 | * @return Record |
||
| 95 | */ |
||
| 96 | public function createRecord(array $attributes = [], string $toScenario = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $condition |
||
| 121 | * @param string $toScenario |
||
| 122 | * @return Record|null |
||
| 123 | */ |
||
| 124 | public function findRecordByCondition($condition, string $toScenario = null) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $criteria |
||
| 140 | * @param string $toScenario |
||
| 141 | * @return Record |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function findRecordByCriteria($criteria, string $toScenario = null) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param $condition |
||
| 164 | * @param string $toScenario |
||
| 165 | * @return Record |
||
| 166 | * @throws RecordNotFoundException |
||
| 167 | */ |
||
| 168 | public function getRecordByCondition($condition, string $toScenario = null) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $criteria |
||
| 183 | * @param string $toScenario |
||
| 184 | * @return Record |
||
| 185 | * @throws RecordNotFoundException |
||
| 186 | */ |
||
| 187 | public function getRecordByCriteria($criteria, string $toScenario = null) |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $toScenario |
||
| 203 | * @return Record[] |
||
| 204 | */ |
||
| 205 | public function findAllRecords(string $toScenario = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $condition |
||
| 212 | * @param string $toScenario |
||
| 213 | * @return Record[] |
||
| 214 | */ |
||
| 215 | public function findAllRecordsByCondition($condition = [], string $toScenario = null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $criteria |
||
| 227 | * @param string $toScenario |
||
| 228 | * @return Record[] |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function findAllRecordsByCriteria($criteria = [], string $toScenario = null) |
|
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * @deprecated |
||
| 258 | * @param array $condition |
||
| 259 | * @param string $toScenario |
||
| 260 | * @return Record[] |
||
| 261 | * @throws RecordNotFoundException |
||
| 262 | */ |
||
| 263 | public function getAllRecords($condition = [], string $toScenario = null) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param array $condition |
||
| 277 | * @param string $toScenario |
||
| 278 | * @return Record[] |
||
| 279 | * @throws RecordNotFoundException |
||
| 280 | */ |
||
| 281 | public function getAllRecordsByCondition($condition = [], string $toScenario = null) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param array $criteria |
||
| 296 | * @param string $toScenario |
||
| 297 | * @return Record[] |
||
| 298 | * @throws RecordNotFoundException |
||
| 299 | */ |
||
| 300 | public function getAllRecordsByCriteria($criteria = [], string $toScenario = null) |
||
| 312 | |||
| 313 | /******************************************* |
||
| 314 | * EXCEPTIONS |
||
| 315 | *******************************************/ |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @throws RecordNotFoundException |
||
| 319 | */ |
||
| 320 | protected function notFoundRecordException() |
||
| 330 | |||
| 331 | } |
||
| 332 |
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.