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 | class UserModel extends ConstructorModel |
||
| 20 | { |
||
| 21 | public function save(array $properties, $key = null, array $options = []) |
||
| 22 | { |
||
| 23 | if (!$key) |
||
| 24 | { |
||
| 25 | View Code Duplication | if (empty($properties[User::PASSWORD_HASH])) |
|
| 26 | { |
||
| 27 | $properties[User::PASSWORD_HASH] = User::hash_password(uniqid(true)); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (empty($properties[User::CREATED_AT]) || DateTime::from($properties[User::CREATED_AT])->is_empty) |
||
| 31 | { |
||
| 32 | $properties[User::CREATED_AT] = DateTime::now(); |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | # |
||
| 37 | # If defined, the password is encrypted before we pass it to our super class. |
||
| 38 | # |
||
| 39 | |||
| 40 | View Code Duplication | if (!empty($properties[User::PASSWORD])) |
|
| 41 | { |
||
| 42 | $properties[User::PASSWORD_HASH] = User::hash_password($properties[User::PASSWORD]); |
||
| 43 | } |
||
| 44 | |||
| 45 | $rc = parent::save($properties, $key, $options); |
||
| 46 | |||
| 47 | if (array_key_exists(User::ROLES, $properties)) |
||
| 48 | { |
||
| 49 | $this->save_roles($key, $rc, $properties[User::ROLES]); |
||
| 50 | } |
||
| 51 | |||
| 52 | if (array_key_exists(User::RESTRICTED_SITES, $properties)) |
||
| 53 | { |
||
| 54 | $this->save_restricted_sites($key, $rc, $properties[User::RESTRICTED_SITES]); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $rc; |
||
| 58 | } |
||
| 59 | |||
| 60 | View Code Duplication | protected function save_roles($key, $rc, $roles) |
|
| 79 | |||
| 80 | View Code Duplication | protected function save_restricted_sites($key, $rc, $restricted_sites) |
|
| 94 | } |
||
| 95 |