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 |
||
10 | final class TailoredAudience extends Resource |
||
11 | { |
||
12 | const RESOURCE_COLLECTION = 'accounts/{account_id}/tailored_audiences'; |
||
13 | const RESOURCE = 'accounts/{account_id}/tailored_audiences/{id}'; |
||
14 | |||
15 | const LIST_TYPE_EMAIL = 'EMAIL'; |
||
16 | const LIST_TYPE_DEVICE_ID = 'DEVICE_ID'; |
||
17 | const LIST_TYPE_TWITTER_ID = 'TWITTER_ID'; |
||
18 | const LIST_TYPE_HANDLE = 'HANDLE'; |
||
19 | const LIST_TYPE_PHONE_NUMBER = 'PHONE_NUMBER'; |
||
20 | |||
21 | /** Writable */ |
||
22 | protected $list_type; |
||
23 | protected $name; |
||
24 | |||
25 | protected $properties = [ |
||
26 | 'name', |
||
27 | 'list_type', |
||
28 | ]; |
||
29 | |||
30 | /** Read Only */ |
||
31 | protected $deleted; |
||
32 | protected $targetable; |
||
33 | protected $audience_size; |
||
34 | protected $id; |
||
35 | protected $updated_at; |
||
36 | protected $created_at; |
||
37 | protected $audience_type; |
||
38 | protected $reasons_not_targetable; |
||
39 | protected $targetable_types; |
||
40 | protected $partner_source; |
||
41 | protected $metadata; |
||
42 | |||
43 | /** |
||
44 | * Uploads and creates a new tailored audience |
||
45 | * |
||
46 | * @param $filePath |
||
47 | * @param $name |
||
48 | * @param $listType |
||
49 | * @return Resource |
||
50 | * @throws TwitterAds\Errors\ServerError |
||
51 | */ |
||
52 | public function create($filePath, $name, $listType) |
||
63 | |||
64 | /** |
||
65 | * Create a simple tailored audience object |
||
66 | * |
||
67 | * @param $name |
||
68 | * @param $listType |
||
69 | * @return $this |
||
70 | */ |
||
71 | public function createAudience($name, $listType) |
||
79 | |||
80 | /** |
||
81 | * Returns the TailoredAudienceChange with the status |
||
82 | * @return null |
||
83 | */ |
||
84 | public function status() |
||
89 | |||
90 | /** |
||
91 | * @return boolean |
||
92 | */ |
||
93 | public function isDeleted() |
||
97 | |||
98 | /** |
||
99 | * @return boolean |
||
100 | */ |
||
101 | public function isTargetable() |
||
105 | |||
106 | /** |
||
107 | * @return integer |
||
108 | */ |
||
109 | public function getAudienceSize() |
||
113 | |||
114 | /** |
||
115 | * @return mixed |
||
116 | */ |
||
117 | public function getId() |
||
121 | |||
122 | /** |
||
123 | * @return \DateTimeImmutable |
||
124 | */ |
||
125 | public function getCreatedAt() |
||
129 | |||
130 | /** |
||
131 | * @return \DateTimeImmutable |
||
132 | */ |
||
133 | public function getUpdatedAt() |
||
137 | |||
138 | /** |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function getListType() |
||
145 | |||
146 | /** |
||
147 | * @param string $type |
||
148 | */ |
||
149 | public function setListType($type) |
||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getAudienceType() |
||
161 | |||
162 | /** |
||
163 | * @return array |
||
164 | */ |
||
165 | public function getReasonsNotTargetable() |
||
169 | |||
170 | /** |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getTargetableTypes() |
||
177 | |||
178 | /** |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getName() |
||
185 | |||
186 | /** |
||
187 | * @param string $name |
||
188 | */ |
||
189 | public function setName($name) |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getPartnerSource() |
||
201 | |||
202 | /** |
||
203 | * @return array |
||
204 | */ |
||
205 | public static function getTypes() |
||
215 | |||
216 | /** |
||
217 | * Asserts that the given type is valid |
||
218 | * |
||
219 | * @param string $type |
||
220 | * @throws InvalidType - if type is invalid or null |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | View Code Duplication | private function assureValidType($type) |
|
236 | |||
237 | /** |
||
238 | * @return array |
||
239 | */ |
||
240 | public function getProperties() |
||
244 | |||
245 | /** |
||
246 | * @return mixed |
||
247 | */ |
||
248 | public function getMetadata() |
||
252 | } |
||
253 |
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.