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 |
||
26 | class Domain extends BaseObject |
||
27 | { |
||
28 | const CACHE_FORWARDERS = 'forwarders'; |
||
29 | const CACHE_MAILBOXES = 'mailboxes'; |
||
30 | const CACHE_SUBDOMAINS = 'subdomains'; |
||
31 | |||
32 | const CATCHALL_BLACKHOLE = ':blackhole:'; |
||
33 | const CATCHALL_FAIL = ':fail:'; |
||
34 | |||
35 | /** @var string */ |
||
36 | private $domainName; |
||
37 | |||
38 | /** @var User */ |
||
39 | private $owner; |
||
40 | |||
41 | /** @var string[] */ |
||
42 | private $aliases; |
||
43 | |||
44 | /** @var string[] */ |
||
45 | private $pointers; |
||
46 | |||
47 | /** @var float */ |
||
48 | private $bandwidthUsed; |
||
49 | |||
50 | /** @var float|null */ |
||
51 | private $bandwidthLimit; |
||
52 | |||
53 | /** @var float */ |
||
54 | private $diskUsage; |
||
55 | |||
56 | /** |
||
57 | * Construct the object. |
||
58 | * |
||
59 | * @param string $name The domain name |
||
60 | * @param UserContext $context The owning user context |
||
61 | * @param string|array $config The basic config string as returned by CMD_API_ADDITIONAL_DOMAINS |
||
62 | */ |
||
63 | public function __construct($name, UserContext $context, $config) |
||
68 | |||
69 | /** |
||
70 | * Creates a new domain under the specified user. |
||
71 | * |
||
72 | * @param User $user Owner of the domain |
||
73 | * @param string $domainName Domain name to create |
||
74 | * @param float|null $bandwidthLimit Bandwidth limit in MB, or NULL to share with account |
||
75 | * @param float|null $diskLimit Disk limit in MB, or NULL to share with account |
||
76 | * @param bool|null $ssl Whether SSL is to be enabled, or NULL to fallback to account default |
||
77 | * @param bool|null $php Whether PHP is to be enabled, or NULL to fallback to account default |
||
78 | * @param bool|null $cgi Whether CGI is to be enabled, or NULL to fallback to account default |
||
79 | * @return Domain The newly created domain |
||
80 | */ |
||
81 | public static function create(User $user, $domainName, $bandwidthLimit = null, $diskLimit = null, $ssl = null, $php = null, $cgi = null) |
||
96 | |||
97 | /** |
||
98 | * Creates a new email forwarder. |
||
99 | * |
||
100 | * @param string $prefix Part of the email address before the @ |
||
101 | * @param string|string[] $recipients One or more recipients |
||
102 | * @return Forwarder The newly created forwarder |
||
103 | */ |
||
104 | public function createForwarder($prefix, $recipients) |
||
108 | |||
109 | /** |
||
110 | * Creates a new mailbox. |
||
111 | * |
||
112 | * @param string $prefix Prefix for the account |
||
113 | * @param string $password Password for the account |
||
114 | * @param int|null $quota Quota in megabytes, or zero/null for unlimited |
||
115 | * @param int|null $sendLimit Send limit, or 0 for unlimited, or null for system default |
||
116 | * @return Mailbox The newly created mailbox |
||
117 | */ |
||
118 | public function createMailbox($prefix, $password, $quota = null, $sendLimit = null) |
||
122 | |||
123 | /** |
||
124 | * Creates a pointer or alias. |
||
125 | * |
||
126 | * @param string $domain |
||
127 | * @param bool $alias |
||
128 | */ |
||
129 | public function createPointer($domain, $alias = false) |
||
146 | |||
147 | /** |
||
148 | * Creates a new subdomain. |
||
149 | * |
||
150 | * @param string $prefix Prefix to add before the domain name |
||
151 | * @return Subdomain The newly created subdomain |
||
152 | */ |
||
153 | public function createSubdomain($prefix) |
||
157 | |||
158 | /** |
||
159 | * Deletes this domain from the user. |
||
160 | */ |
||
161 | public function delete() |
||
170 | |||
171 | /** |
||
172 | * @return string[] List of aliases for this domain |
||
173 | */ |
||
174 | public function getAliases() |
||
178 | |||
179 | /** |
||
180 | * @return float Bandwidth used in megabytes |
||
181 | */ |
||
182 | public function getBandwidthUsed() |
||
186 | |||
187 | /** |
||
188 | * @return float|null Bandwidth quotum in megabytes, or NULL for unlimited |
||
189 | */ |
||
190 | public function getBandwidthLimit() |
||
194 | |||
195 | /** |
||
196 | * @return string|null Currently configured catch-all configuration |
||
197 | */ |
||
198 | public function getCatchall() |
||
203 | |||
204 | /** |
||
205 | * @return float Disk usage in megabytes |
||
206 | */ |
||
207 | public function getDiskUsage() |
||
211 | |||
212 | /** |
||
213 | * @return string The real domain name |
||
214 | */ |
||
215 | public function getDomainName() |
||
219 | |||
220 | /** |
||
221 | * Returns unified sorted list of main domain name, aliases and pointers. |
||
222 | * |
||
223 | * @return string[] |
||
224 | */ |
||
225 | public function getDomainNames() |
||
233 | |||
234 | /** |
||
235 | * @return Forwarder[] Associative array of forwarders |
||
236 | */ |
||
237 | View Code Duplication | public function getForwarders() |
|
246 | |||
247 | /** |
||
248 | * @return Mailbox[] Associative array of mailboxes |
||
249 | */ |
||
250 | View Code Duplication | public function getMailboxes() |
|
260 | |||
261 | /** |
||
262 | * @return User |
||
263 | */ |
||
264 | public function getOwner() |
||
268 | |||
269 | /** |
||
270 | * @return string[] List of domain pointers for this domain |
||
271 | */ |
||
272 | public function getPointers() |
||
276 | |||
277 | /** |
||
278 | * @return Subdomain[] Associative array of subdomains |
||
279 | */ |
||
280 | public function getSubdomains() |
||
288 | |||
289 | /** |
||
290 | * Invokes a POST command on a domain object. |
||
291 | * |
||
292 | * @param string $command Command to invoke |
||
293 | * @param string $action Action to execute |
||
294 | * @param array $parameters Additional options for the command |
||
295 | * @param bool $clearCache Whether to clear the domain cache on success |
||
296 | * @return array Response from the API |
||
297 | */ |
||
298 | public function invokePost($command, $action, $parameters = [], $clearCache = true) |
||
309 | |||
310 | /** |
||
311 | * @param string $newValue New address for the catch-all, or one of the CATCHALL_ constants |
||
312 | */ |
||
313 | public function setCatchall($newValue) |
||
319 | |||
320 | /** |
||
321 | * Allows Domain object to be passed as a string with its domain name. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public function __toString() |
||
329 | |||
330 | /** |
||
331 | * Sets configuration options from raw DirectAdmin data. |
||
332 | * |
||
333 | * @param UserContext $context Owning user context |
||
334 | * @param array $config An array of settings |
||
335 | */ |
||
336 | private function setConfig(UserContext $context, array $config) |
||
356 | } |
||
357 |
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.