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 |
||
17 | class Customer extends HttpApi |
||
18 | { |
||
19 | /** |
||
20 | * @throws \FAPI\Fortnox\Exception\DomainException |
||
21 | * |
||
22 | * @return CustomerCollection|ResponseInterface |
||
23 | */ |
||
24 | public function all(array $params = []) |
||
25 | { |
||
26 | $response = $this->httpGet('/3/customers', $params); |
||
27 | |||
28 | if (!$this->hydrator) { |
||
29 | return $response; |
||
30 | } |
||
31 | |||
32 | // Use any valid status code here |
||
33 | if (200 !== $response->getStatusCode()) { |
||
34 | $this->handleErrors($response); |
||
35 | } |
||
36 | |||
37 | return $this->hydrator->hydrate($response, CustomerCollection::class); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @throws \FAPI\Fortnox\Exception\DomainException |
||
42 | * |
||
43 | * @return Model|ResponseInterface |
||
44 | */ |
||
45 | View Code Duplication | public function get(string $customer) |
|
|
|||
46 | { |
||
47 | $response = $this->httpGet('/3/customers/'.$customer); |
||
48 | |||
49 | if (!$this->hydrator) { |
||
50 | return $response; |
||
51 | } |
||
52 | |||
53 | // Use any valid status code here |
||
54 | if (200 !== $response->getStatusCode()) { |
||
55 | $this->handleErrors($response); |
||
56 | } |
||
57 | |||
58 | return $this->hydrator->hydrate($response, Model::class); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @throws \FAPI\Fortnox\Exception\DomainException |
||
63 | * |
||
64 | * @return Model|ResponseInterface |
||
65 | */ |
||
66 | public function create(array $data) |
||
81 | |||
82 | /** |
||
83 | * @throws \FAPI\Fortnox\Exception\DomainException |
||
84 | * |
||
85 | * @return Model|ResponseInterface |
||
86 | */ |
||
87 | public function update(string $customer, array $data) |
||
88 | { |
||
89 | $response = $this->httpPut('/3/customers/'.$customer, ['Customer' => $data]); |
||
90 | |||
91 | if (!$this->hydrator) { |
||
92 | return $response; |
||
93 | } |
||
94 | |||
95 | // Use any valid status code here |
||
96 | if (200 !== $response->getStatusCode()) { |
||
97 | $this->handleErrors($response); |
||
98 | } |
||
99 | |||
100 | return $this->hydrator->hydrate($response, Model::class); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @throws \FAPI\Fortnox\Exception\DomainException |
||
105 | * |
||
106 | * @return ApiResponse|ResponseInterface |
||
107 | */ |
||
108 | public function delete(string $customer) |
||
123 | } |
||
124 |
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.