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 |
||
| 20 | trait ModelByString |
||
| 21 | { |
||
| 22 | |||
| 23 | use Model; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var BaseModel[] |
||
| 27 | */ |
||
| 28 | protected $_cacheByString = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | abstract protected function stringProperty(): string; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Record $record |
||
| 37 | * @param string|null $toScenario |
||
| 38 | * @return BaseModel |
||
| 39 | */ |
||
| 40 | abstract protected function findByRecord(Record $record, string $toScenario = null): BaseModel; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param array $config |
||
| 44 | * @param string|null $toScenario |
||
| 45 | * @return BaseModel |
||
| 46 | */ |
||
| 47 | abstract public function create($config = [], string $toScenario = null): BaseModel; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | protected function recordStringProperty(): string |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param BaseModel $model |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | protected function stringValue(BaseModel $model) |
||
| 69 | |||
| 70 | /******************************************* |
||
| 71 | * FIND/GET BY STRING |
||
| 72 | *******************************************/ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $string |
||
| 76 | * @param string|null $toScenario |
||
| 77 | * @return BaseModel|null |
||
| 78 | */ |
||
| 79 | public function findByString(string $string, string $toScenario = null) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $string |
||
| 106 | * @param string|null $toScenario |
||
| 107 | * @return BaseModel|null |
||
| 108 | * @throws ModelNotFoundException |
||
| 109 | */ |
||
| 110 | public function getByString(string $string, string $toScenario = null): BaseModel |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $string |
||
| 125 | * @param string|null $toScenario |
||
| 126 | * @return BaseModel|null |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function freshFindByString(string $string, string $toScenario = null) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $string |
||
| 144 | * @param string|null $toScenario |
||
| 145 | * @return BaseModel |
||
| 146 | * @throws ModelNotFoundException |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function freshGetByString(string $string, string $toScenario = null): BaseModel |
|
| 160 | |||
| 161 | /******************************************* |
||
| 162 | * CACHE |
||
| 163 | *******************************************/ |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Find an existing cache by string |
||
| 167 | * |
||
| 168 | * @param string $string |
||
| 169 | * @return null |
||
| 170 | */ |
||
| 171 | public function findCacheByString(string $string) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Identify whether in cache by string |
||
| 185 | * |
||
| 186 | * @param string $string |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | private function isCachedByString(string $string): bool |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @param BaseModel $model |
||
| 197 | * @return static |
||
| 198 | */ |
||
| 199 | View Code Duplication | protected function cacheByString(BaseModel $model) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param Record $record |
||
| 222 | * @return BaseModel|null |
||
| 223 | */ |
||
| 224 | View Code Duplication | protected function findCacheByRecordByString(Record $record) |
|
| 238 | |||
| 239 | /******************************************* |
||
| 240 | * RECORD BY STRING |
||
| 241 | *******************************************/ |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $string |
||
| 245 | * @param string|null $toScenario |
||
| 246 | * @return Record|null |
||
| 247 | */ |
||
| 248 | public function findRecordByString(string $string, string $toScenario = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $string |
||
| 262 | * @param string|null $toScenario |
||
| 263 | * @throws RecordNotFoundException |
||
| 264 | * @return Record|null |
||
| 265 | */ |
||
| 266 | public function getRecordByString(string $string, string $toScenario = null) |
||
| 278 | |||
| 279 | /******************************************* |
||
| 280 | * EXCEPTIONS |
||
| 281 | *******************************************/ |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string|null $string |
||
| 285 | * @throws ModelNotFoundException |
||
| 286 | */ |
||
| 287 | protected function notFoundByStringException(string $string = null) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string|null $string |
||
| 301 | * @throws RecordNotFoundException |
||
| 302 | */ |
||
| 303 | protected function notFoundRecordByStringException(string $string = null) |
||
| 314 | |||
| 315 | } |
||
| 316 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.