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 |
||
11 | class Decorator implements Cache |
||
12 | { |
||
13 | use EncryptionStatsTrait; |
||
14 | |||
15 | /** @var Cache */ |
||
16 | private $decorated; |
||
17 | /** @var Iron\Iron */ |
||
18 | private $iron; |
||
19 | /** @var PasswordInterface */ |
||
20 | private $password; |
||
21 | |||
22 | 129 | public function __construct( |
|
31 | |||
32 | 78 | public function fetch($id) |
|
43 | |||
44 | 33 | public function contains($id) |
|
45 | { |
||
46 | try { |
||
47 | 33 | Token::fromSealed( |
|
48 | 33 | $this->password, |
|
49 | 33 | $this->decorated->fetch($id) |
|
50 | 33 | ); |
|
51 | 15 | return true; |
|
52 | 18 | } catch (Exception $e) { |
|
53 | 18 | return false; |
|
54 | } |
||
55 | } |
||
56 | |||
57 | 75 | View Code Duplication | public function save($id, $data, $ttl = 0) |
68 | |||
69 | 3 | public function delete($id) |
|
73 | |||
74 | 45 | public function getStats() |
|
78 | } |
||
79 |