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 |
||
7 | class Feeds |
||
8 | { |
||
9 | /** |
||
10 | * The Http Client. |
||
11 | * |
||
12 | * @var \Mozammil\Putio\Http\Client |
||
13 | */ |
||
14 | protected $client; |
||
15 | |||
16 | public function __construct(Client $client) |
||
20 | |||
21 | /** |
||
22 | * Creates an RSS feed with the given parameters. |
||
23 | * |
||
24 | * @param string $title Title of the RSS feed as will appear on the site |
||
25 | * @param string $rss_source_url The URL of the RSS feed to be watched |
||
26 | * @param int $parent_dir_id The file ID of the folder to place the RSS feed files in |
||
27 | * @param bool $delete_old_files Should old files in the folder be deleted when space is low |
||
28 | * @param bool $dont_process_whole_feed Should the current items in the feed, at creation time, be ignored |
||
29 | * @param array $keywords Only items with titles that contain any of these words will be transferred |
||
30 | * @param array $unwanted_keywords No items with titles that contain any of these words will be transferred |
||
31 | * @param bool $paused Should the RSS feed be created in the paused state |
||
32 | * |
||
33 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
34 | * |
||
35 | * @see https://api.put.io/v2/docs/feeds.html#post--rss-create |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | View Code Duplication | public function create( |
|
62 | |||
63 | /** |
||
64 | * Lists RSS feeds. |
||
65 | * |
||
66 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
67 | * |
||
68 | * @see https://api.put.io/v2/docs/feeds.html#get--rss-list |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function list() |
||
76 | |||
77 | /** |
||
78 | * Gives detailed information about the given feed id. |
||
79 | * |
||
80 | * @param int $id |
||
81 | * |
||
82 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
83 | * |
||
84 | * @see https://api.put.io/v2/docs/feeds.html#get--rss-list |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function get(int $id) |
||
92 | |||
93 | /** |
||
94 | * Updates an RSS feed with the given parameters. |
||
95 | * |
||
96 | * @param int $id Id of the RSS feed to be updated |
||
97 | * @param string $title Title of the RSS feed as will appear on the site |
||
98 | * @param string $rss_source_url The URL of the RSS feed to be watched |
||
99 | * @param int $parent_dir_id The file ID of the folder to place the RSS feed files in |
||
100 | * @param bool $delete_old_files Should old files in the folder be deleted when space is low |
||
101 | * @param bool $dont_process_whole_feed Should the current items in the feed, at creation time, be ignored |
||
102 | * @param array $keywords Only items with titles that contain any of these words will be transferred |
||
103 | * @param array $unwanted_keywords No items with titles that contain any of these words will be transferred |
||
104 | * @param bool $paused Should the RSS feed be created in the paused state |
||
105 | * |
||
106 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
107 | * |
||
108 | * @see https://api.put.io/v2/docs/feeds.html#update |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | View Code Duplication | public function update( |
|
136 | |||
137 | /** |
||
138 | * Pauses the RSS feed, so that it is not polled for new items anymore. |
||
139 | * |
||
140 | * @param int $id |
||
141 | * |
||
142 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
143 | * |
||
144 | * @see https://api.put.io/v2/docs/feeds.html#post--rss--feed_id--pause |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function pause(int $id) |
||
152 | |||
153 | /** |
||
154 | * Resumes the RSS feed, so that it starts being polled for new items again. |
||
155 | * |
||
156 | * @param int $id |
||
157 | * |
||
158 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
159 | * |
||
160 | * @see https://api.put.io/v2/docs/feeds.html#post--rss--feed_id--resume |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function resume(int $id) |
||
168 | |||
169 | /** |
||
170 | * Deletes given RSS feed. |
||
171 | * |
||
172 | * @param int $id |
||
173 | * |
||
174 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
175 | * |
||
176 | * @see https://api.put.io/v2/docs/feeds.html#post--rss--feed_id--delete |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function delete(int $id) |
||
184 | } |
||
185 |
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.