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 |
||
25 | class Request implements RequestInterface |
||
26 | { |
||
27 | protected $url; |
||
28 | protected $post = []; |
||
29 | protected $startTime; |
||
30 | protected $endTime; |
||
31 | protected $result; |
||
32 | protected $listeners = []; |
||
33 | protected $timeout; |
||
34 | protected $curlHandle; |
||
35 | protected $headers = []; |
||
36 | protected $options = []; |
||
37 | protected $success; |
||
38 | protected $response; |
||
39 | |||
40 | private function camelize($str) |
||
44 | |||
45 | View Code Duplication | public function __set($name, $value) |
|
54 | |||
55 | View Code Duplication | public function __get($name) { |
|
63 | |||
64 | /** |
||
65 | * Request constructor. |
||
66 | * @param string $url optional url |
||
67 | */ |
||
68 | public function __construct($url = null) |
||
76 | |||
77 | public function setUrl($url) |
||
83 | |||
84 | public function getUrl() |
||
88 | |||
89 | public function setPostData(array $postValues) |
||
97 | |||
98 | public function getPostData() |
||
102 | |||
103 | public function getTime() |
||
107 | |||
108 | public function startTimer() |
||
112 | |||
113 | public function stopTimer() |
||
117 | |||
118 | public function getResult() |
||
122 | |||
123 | public function callBack($result) |
||
139 | |||
140 | public function addListener(callable $function) |
||
146 | |||
147 | protected function notify() |
||
154 | |||
155 | public function setTimeout($timeout) |
||
162 | |||
163 | public function getHandle() |
||
172 | |||
173 | function setHeaders(array $headers) |
||
178 | |||
179 | function getHeaders() |
||
183 | |||
184 | function setOptions(array $options) |
||
188 | |||
189 | function getOptions() |
||
193 | |||
194 | function getResponse() |
||
198 | } |
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.