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 |
||
10 | View Code Duplication | class AcceptAnswerCommand |
|
|
|||
11 | { |
||
12 | const URL = '/answers/%s/accept'; |
||
13 | |||
14 | /** |
||
15 | * The API URL. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | private $url; |
||
20 | |||
21 | /** |
||
22 | * Array that contains StackExchange API params. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | private $params; |
||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | * |
||
31 | * @param array|string $id The answer id. Also, it can be multiple ids separated by semicolon. |
||
32 | * @param array $params Array that contains StackExchange API params |
||
33 | */ |
||
34 | public function __construct($id, array $params = ['site' => 'stackoverflow']) |
||
47 | |||
48 | /** |
||
49 | * The API URL. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public function url() |
||
57 | |||
58 | /** |
||
59 | * Gets the params. |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public function params() |
||
67 | } |
||
68 |
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.