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 |
||
8 | class RequestFactory |
||
9 | { |
||
10 | /** |
||
11 | * @var ClientInterface |
||
12 | */ |
||
13 | private $client; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $apiKey; |
||
19 | |||
20 | /** |
||
21 | * @param ClientInterface $client |
||
22 | * @param string $apiKey |
||
23 | */ |
||
24 | 4 | public function __construct(ClientInterface $client, $apiKey) |
|
29 | |||
30 | /** |
||
31 | * @param DataSetInterface $dataSet |
||
32 | * |
||
33 | * @return RequestInterface |
||
34 | */ |
||
35 | 1 | public function getCreateRequest(DataSetInterface $dataSet) |
|
49 | |||
50 | /** |
||
51 | * @param string $datasetName |
||
52 | * @param DataSetRowInterface[] $rows |
||
53 | * |
||
54 | * @return RequestInterface |
||
55 | */ |
||
56 | 1 | View Code Duplication | public function getAppendRequest($datasetName, array $rows) |
75 | |||
76 | /** |
||
77 | * @param string $datasetName |
||
78 | * @param DataSetRowInterface[] $rows |
||
79 | * |
||
80 | * @return RequestInterface |
||
81 | */ |
||
82 | 1 | View Code Duplication | public function getReplaceRequest($datasetName, array $rows) |
101 | |||
102 | /** |
||
103 | * @param string $datasetName |
||
104 | * |
||
105 | * @return RequestInterface |
||
106 | */ |
||
107 | 1 | public function getDeleteRequest($datasetName) |
|
117 | } |
||
118 |
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.