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 | View Code Duplication | class CreateSnapshot extends Action |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * Action parameter |
||
15 | * |
||
16 | * @const ACTION |
||
17 | */ |
||
18 | const ACTION = 'snapshot'; |
||
19 | |||
20 | /** |
||
21 | * Action HTTP Method |
||
22 | * |
||
23 | * @const method |
||
24 | */ |
||
25 | const METHOD = 'POST'; |
||
26 | |||
27 | /** |
||
28 | * Name of the snapshot to be created |
||
29 | * |
||
30 | * @var string name |
||
31 | */ |
||
32 | private $name; |
||
33 | |||
34 | /** |
||
35 | * Constructor for the create snapshot action |
||
36 | * |
||
37 | * @param Array values |
||
38 | * @throws \InvalidArgumentException |
||
39 | */ |
||
40 | public function __construct(Array $values) |
||
52 | |||
53 | /** |
||
54 | * Return the body of the request |
||
55 | * |
||
56 | * @return string json representation of the body |
||
57 | */ |
||
58 | public function getBody() |
||
65 | } |
||
66 | |||
67 |
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.