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 |
||
| 12 | class NonceIdentity extends DataMapper |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param Entity\NonceIdentity $entity |
||
| 17 | */ |
||
| 18 | View Code Duplication | public function fetch(Entity\NonceIdentity $entity) |
|
|
|
|||
| 19 | { |
||
| 20 | $sql = "SELECT identity_id AS id, |
||
| 21 | account_id AS accountId, |
||
| 22 | hash AS hash, |
||
| 23 | status AS status, |
||
| 24 | expires_on AS expiresOn |
||
| 25 | FROM {$this->table} |
||
| 26 | WHERE type = :type |
||
| 27 | AND status = :status |
||
| 28 | AND identifier = :identifier"; |
||
| 29 | |||
| 30 | $statement = $this->connection->prepare($sql); |
||
| 31 | |||
| 32 | $statement->bindValue(':identifier', $entity->getIdentifier()); |
||
| 33 | $statement->bindValue(':status', Entity\Identity::STATUS_ACTIVE); |
||
| 34 | $statement->bindValue(':type', $entity->getType()); |
||
| 35 | |||
| 36 | $statement->execute(); |
||
| 37 | |||
| 38 | $data = $statement->fetch(); |
||
| 39 | |||
| 40 | if ($data) { |
||
| 41 | $this->applyValues($entity, $data); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * @param Entity\NonceIdentity $entity |
||
| 48 | */ |
||
| 49 | 2 | public function store(Entity\NonceIdentity $entity) |
|
| 58 | |||
| 59 | |||
| 60 | 1 | private function createIdentity(Entity\NonceIdentity $entity) |
|
| 79 | |||
| 80 | |||
| 81 | 1 | View Code Duplication | private function updateIdentity(Entity\NonceIdentity $entity) |
| 96 | |||
| 97 | } |
||
| 98 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.