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 |
||
| 13 | class Identity extends SqlMapper implements CanPersistIdentity |
||
|
|
|||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param Entity\Identity $entity |
||
| 18 | */ |
||
| 19 | public function store(Entity\Identity $entity) |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Entity\Identity $entity |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function fetch(Entity\Identity $entity) |
|
| 39 | { |
||
| 40 | $table = $this->config['accounts']['identities']; |
||
| 41 | |||
| 42 | $sql = "SELECT identity_id AS id, |
||
| 43 | user_id AS userId, |
||
| 44 | status AS status, |
||
| 45 | hash AS hash, |
||
| 46 | UNIX_TIMESTAMP(token_expires_on) AS tokenEndOfLife |
||
| 47 | FROM $table |
||
| 48 | WHERE token = :token |
||
| 49 | AND token_action = :action |
||
| 50 | AND token_expires_on > FROM_UNIXTIME(:expires)"; |
||
| 51 | |||
| 52 | $statement = $this->connection->prepare($sql); |
||
| 53 | |||
| 54 | $statement->bindValue(':token', $entity->getToken()); |
||
| 55 | $statement->bindValue(':action', $entity->getTokenAction()); |
||
| 56 | $statement->bindValue(':expires', $entity->getTokenEndOfLife()); |
||
| 57 | |||
| 58 | $statement->execute(); |
||
| 59 | |||
| 60 | $data = $statement->fetch(); |
||
| 61 | |||
| 62 | if ($data) { |
||
| 63 | $this->applyValues($entity, $data); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @param Entity\Identity $entity |
||
| 70 | */ |
||
| 71 | public function fetch(Entity\Identity $entity) {} |
||
| 72 | } |
||
| 73 |