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 |
||
| 24 | 1 | class Role extends Nette\Object implements IRole |
|
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $id; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var IRole|NULL |
||
| 33 | */ |
||
| 34 | protected $parent; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \SplObjectStorage |
||
| 38 | */ |
||
| 39 | protected $children; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string|NULL |
||
| 43 | */ |
||
| 44 | protected $name; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|NULL |
||
| 48 | */ |
||
| 49 | protected $comment; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \SplObjectStorage |
||
| 53 | */ |
||
| 54 | protected $permissions; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $id |
||
| 58 | * @param string|NULL $name |
||
| 59 | * @param string|NULL $comment |
||
| 60 | */ |
||
| 61 | public function __construct(string $id, string $name = NULL, string $comment = NULL) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function setParent(IRole $parent = NULL) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function getParent() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | public function setChildren(array $roles) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | public function addChild(IRole $role) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function getChildren() : array |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function getRoleId() : string |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function setName(string $name) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function getName() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function setComment(string $comment) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function getComment() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function setPermissions(array $permissions) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function addPermission(IPermission $permission) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function getPermissions() : array |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function hasPermission(IPermission $permission) : bool |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function removePermission(IPermission $permission) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | public function clearPermissions() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function isLocked() : bool |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function isAnonymous() : bool |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function isAuthenticated() : bool |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function isAdministrator() : bool |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Convert role object to string |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function __toString() |
||
| 291 | } |
||
| 292 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: