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 |
||
12 | abstract class Provider |
||
13 | { |
||
14 | /** |
||
15 | * List of methods that require logged status. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $loginRequiredFor = []; |
||
20 | |||
21 | /** |
||
22 | * Instance of the API RequestInterface. |
||
23 | * |
||
24 | * @var RequestInterface |
||
25 | */ |
||
26 | protected $request; |
||
27 | |||
28 | /** |
||
29 | * Instance of the API ResponseInterface. |
||
30 | * |
||
31 | * @var ResponseInterface |
||
32 | */ |
||
33 | protected $response; |
||
34 | |||
35 | /** |
||
36 | * @param RequestInterface $request |
||
37 | * @param ResponseInterface $response |
||
38 | */ |
||
39 | public function __construct(RequestInterface $request, ResponseInterface $response) |
||
44 | |||
45 | /** |
||
46 | * Executes a POST request to Pinterest API. |
||
47 | * |
||
48 | * @param array $requestOptions |
||
49 | * @param string $resourceUrl |
||
50 | * @param bool $returnData |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | protected function execPostRequest($requestOptions, $resourceUrl, $returnData = false) |
||
65 | |||
66 | /** |
||
67 | * Executes a GET request to Pinterest API. |
||
68 | * |
||
69 | * @param array $requestOptions |
||
70 | * @param string $resourceUrl |
||
71 | * @return array|bool |
||
72 | */ |
||
73 | View Code Duplication | protected function execGetRequest(array $requestOptions, $resourceUrl) |
|
80 | |||
81 | /** |
||
82 | * Executes a GET request to Pinterest API with pagination. |
||
83 | * |
||
84 | * @param array $requestOptions |
||
85 | * @param string $resourceUrl |
||
86 | * @param array $bookmarks |
||
87 | * @return array|bool |
||
88 | */ |
||
89 | View Code Duplication | protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = []) |
|
96 | |||
97 | /** |
||
98 | * Executes pagination GET request. |
||
99 | * |
||
100 | * @param array $data |
||
101 | * @param string $url |
||
102 | * @param array $bookmarks |
||
103 | * @return array|bool |
||
104 | */ |
||
105 | public function getPaginatedData(array $data, $url, $bookmarks = []) |
||
109 | |||
110 | /** |
||
111 | * @param string $method |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function checkMethodRequiresLogin($method) |
||
119 | |||
120 | /** |
||
121 | * @return RequestInterface |
||
122 | */ |
||
123 | public function getRequest() |
||
127 | |||
128 | /** |
||
129 | * @return ResponseInterface |
||
130 | */ |
||
131 | public function getResponse() |
||
135 | } |
||
136 |
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.