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 |
||
23 | class UidSignatureValidator implements ResponseValidatorInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $secret; |
||
29 | |||
30 | /** |
||
31 | * @var Signature |
||
32 | */ |
||
33 | private $signature; |
||
34 | |||
35 | /** |
||
36 | * @param Signature $signature |
||
37 | * @param string $secret |
||
38 | */ |
||
39 | 13 | public function __construct(Signature $signature, $secret) |
|
44 | |||
45 | /** |
||
46 | * Can validate. |
||
47 | * |
||
48 | * @param ResponseInterface $response |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | 8 | View Code Duplication | public function canValidate(ResponseInterface $response) |
60 | |||
61 | /** |
||
62 | * Throws exceptions if any errors are found. |
||
63 | * |
||
64 | * @param ResponseInterface $response |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | 3 | View Code Duplication | public function validate(ResponseInterface $response) |
78 | |||
79 | /** |
||
80 | * @param ResponseInterface $response |
||
81 | * |
||
82 | * @throws InvalidTimestampException |
||
83 | * @throws InvalidUidSignatureException |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | 1 | View Code Duplication | public function assert(ResponseInterface $response) |
88 | { |
||
89 | 1 | $data = $response->getData(); |
|
90 | |||
91 | 1 | $this->assertUid( |
|
92 | 1 | $data->get('UID'), |
|
93 | 1 | $data->get('signatureTimestamp'), |
|
94 | 1 | $data->get('UIDSignature'), |
|
95 | 1 | $response |
|
96 | ); |
||
97 | 1 | } |
|
98 | |||
99 | /** |
||
100 | * Validate the provided Uid signature is valid. |
||
101 | * |
||
102 | * @param string $uid |
||
103 | * @param int $timestamp Unix Timestamp |
||
104 | * @param string $signature |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | 3 | public function validateUid($uid, $timestamp, $signature) |
|
113 | |||
114 | /** |
||
115 | * @param string $uid |
||
116 | * @param int $timestamp Unix Timestamp |
||
117 | * @param string $signature |
||
118 | * @param ResponseInterface $response |
||
119 | * |
||
120 | * @throws InvalidTimestampException |
||
121 | * @throws InvalidUidSignatureException |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | 1 | private function assertUid($uid, $timestamp, $signature, ResponseInterface $response) |
|
137 | } |
||
138 |
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.