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 |
||
| 41 | class Project extends Model implements AccessControl |
||
| 42 | { |
||
| 43 | use CountAttributeTrait, |
||
| 44 | ProjectRelations, |
||
| 45 | ProjectScopes; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Project private & user role can see their own issues only. |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | const INTERNAL_YES = 2; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Project not public to view and create issue. |
||
| 56 | * |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | const PRIVATE_YES = 1; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Project public to view and create issue. |
||
| 63 | * |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | const PRIVATE_NO = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * All projects. |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | const PRIVATE_ALL = -1; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Project status Open. |
||
| 77 | * |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | const STATUS_OPEN = 1; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Project status Archived. |
||
| 84 | * |
||
| 85 | * @var int |
||
| 86 | */ |
||
| 87 | const STATUS_ARCHIVED = 0; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Timestamp enabled. |
||
| 91 | * |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | public $timestamps = true; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Name of database table. |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $table = 'projects'; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * List of allowed columns to be used in $this->fill(). |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | protected $fillable = ['name', 'default_assignee', 'status', 'private']; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * List of HTML classes for each status. |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | protected $attrClassNames = [ |
||
| 116 | self::PRIVATE_NO => 'note', |
||
| 117 | self::PRIVATE_YES => 'info', |
||
| 118 | self::INTERNAL_YES => 'primary', |
||
| 119 | ]; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * List of statuses names. |
||
| 123 | * |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected $statusesNames = [ |
||
| 127 | self::PRIVATE_NO => 'public', |
||
| 128 | self::PRIVATE_YES => 'private', |
||
| 129 | self::INTERNAL_YES => 'internal', |
||
| 130 | ]; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Generate a URL for the active project. |
||
| 134 | * |
||
| 135 | * @param string $url |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function to($url = '') |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns the aggregate value of number of open issues in the project. |
||
| 146 | * |
||
| 147 | * @return int |
||
| 148 | */ |
||
| 149 | public function getCountOpenIssuesAttribute() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the aggregate value of number of closed issues in the project. |
||
| 156 | * |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | public function getCountClosedIssuesAttribute() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Set default assignee attribute. |
||
| 166 | * |
||
| 167 | * @param int $value |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function setDefaultAssigneeAttribute($value) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns the aggregate value of number of issues in the project. |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public function getIssuesCountAttribute() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get total issues total quote time. |
||
| 192 | * |
||
| 193 | * @return int |
||
| 194 | */ |
||
| 195 | public function getTotalQuote() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Calculate the progress (open & closed issues). |
||
| 207 | * |
||
| 208 | * @return float|int |
||
| 209 | */ |
||
| 210 | public function getProgress() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Whether or not a user is member of the project. |
||
| 231 | * |
||
| 232 | * @param int $userId |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function isMember($userId) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Whether or not the project is private. |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function isPrivate() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Whether or not the project is public. |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function isPublic() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Whether or not the project is private internal. |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function isPrivateInternal() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns project status as string name. |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getStatusAsName() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the class name to be used for project status. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function getStatusClass() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Whether or not a user can access the project. |
||
| 301 | * |
||
| 302 | * @param UserInterface $user |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function canView(UserInterface $user) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Whether a user can edit the project. |
||
| 321 | * |
||
| 322 | * @param UserInterface $user |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function canEdit(UserInterface $user) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $permission |
||
| 333 | * @param UserInterface $user |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function can($permission, UserInterface $user) |
|
| 350 | |||
| 351 | public function getPreferredMessageIdForUser($userId) |
||
| 358 | } |
||
| 359 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: