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 |
||
20 | class ResponseHeader { |
||
21 | /** |
||
22 | * The combined date and time in UTC (ISO8601). |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $request_timestamp; |
||
27 | |||
28 | /** |
||
29 | * Construct and initialize request header. |
||
30 | * |
||
31 | * @param string $timestamp Request timestamp. |
||
32 | */ |
||
33 | 5 | public function __construct( $timestamp ) { |
|
36 | |||
37 | /** |
||
38 | * From JSON. |
||
39 | * |
||
40 | * @link https://github.com/WordPress/wp-notify/blob/develop/includes/JsonUnserializable.php |
||
41 | * @param object $object Object. |
||
42 | * @return self |
||
43 | * @throws \JsonSchema\Exception\ValidationException Throws exception when JSON is not valid. |
||
44 | */ |
||
45 | 3 | View Code Duplication | public static function from_json( $object ) { |
59 | |||
60 | /** |
||
61 | * Get request timestamp. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | 1 | public function get_request_timestamp() { |
|
68 | } |
||
69 |
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.