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 |
||
22 | class HttpListener |
||
23 | { |
||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $options; |
||
28 | |||
29 | /** |
||
30 | * @var resource |
||
31 | */ |
||
32 | protected $curlHandle; |
||
33 | |||
34 | /** |
||
35 | * The constructor class. |
||
36 | * |
||
37 | * @param array $options Options for curl (default empty). |
||
38 | */ |
||
39 | public function __construct(array $options = []) |
||
43 | |||
44 | /** |
||
45 | * Sends a GET request to the API. |
||
46 | * |
||
47 | * @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
||
48 | */ |
||
49 | View Code Duplication | public function onGetRequest(GetResponseEvent $getResponseEvent) |
|
56 | |||
57 | /** |
||
58 | * Sends a POST request to the API. |
||
59 | * |
||
60 | * @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
||
61 | */ |
||
62 | View Code Duplication | public function onPostRequest(GetResponseEvent $getResponseEvent) |
|
69 | |||
70 | /** |
||
71 | * Sends a PUT request to the API. |
||
72 | * |
||
73 | * @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
||
74 | */ |
||
75 | View Code Duplication | public function onPutRequest(GetResponseEvent $getResponseEvent) |
|
82 | |||
83 | /** |
||
84 | * Sends a DELETE request to the API. |
||
85 | * |
||
86 | * @param GetResponseEvent $getResponseEvent The event for sharing rest response and request. |
||
87 | */ |
||
88 | View Code Duplication | public function onDeleteRequest(GetResponseEvent $getResponseEvent) |
|
95 | |||
96 | /** |
||
97 | * Executes a curl request to the given url. |
||
98 | * |
||
99 | * @param GetResponseEvent $event The event for sharing rest response and request. |
||
100 | */ |
||
101 | protected function execute(GetResponseEvent $event) |
||
128 | } |
||
129 |
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.