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 | trait UserTrait |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var int|null |
||
| 24 | */ |
||
| 25 | private $_userId; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var UserElement|null |
||
| 29 | */ |
||
| 30 | private $_user; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Set associated userId |
||
| 34 | * |
||
| 35 | * @param $id |
||
| 36 | * @return $this |
||
| 37 | */ |
||
| 38 | public function setUserId(int $id) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get associated userId |
||
| 58 | * |
||
| 59 | * @return int|null |
||
| 60 | */ |
||
| 61 | public function getUserId() |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Associate a user |
||
| 69 | * |
||
| 70 | * @param $user |
||
| 71 | * @return $this |
||
| 72 | */ |
||
| 73 | public function setUser($user) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return UserElement|null |
||
| 101 | */ |
||
| 102 | public function getUser() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $user |
||
| 142 | * @return UserElement|null |
||
| 143 | */ |
||
| 144 | private function findUserElement($user) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | View Code Duplication | protected function userRules(): array |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | protected function userFields(): array |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | protected function userAttributes(): array |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | protected function userAttributeLabels(): array |
||
| 231 | |||
| 232 | } |
||
| 233 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..