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:
Complex classes like PublicSuffixListManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PublicSuffixListManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class PublicSuffixListManager |
||
25 | { |
||
26 | const ALL_DOMAINS = 'ALL'; |
||
27 | |||
28 | const PDP_PSL_TEXT_FILE = 'public-suffix-list.txt'; |
||
29 | const PDP_PSL_PHP_FILE = 'public-suffix-list.php'; |
||
30 | |||
31 | const ICANN_DOMAINS = 'ICANN'; |
||
32 | const ICANN_PSL_PHP_FILE = 'icann-public-suffix-list.php'; |
||
33 | |||
34 | const PRIVATE_DOMAINS = 'PRIVATE'; |
||
35 | const PRIVATE_PSL_PHP_FILE = 'private-public-suffix-list.php'; |
||
36 | |||
37 | /** |
||
38 | * @var string Public Suffix List URL |
||
39 | */ |
||
40 | protected $publicSuffixListUrl = 'https://publicsuffix.org/list/effective_tld_names.dat'; |
||
41 | |||
42 | /** |
||
43 | * @var string Directory where text and php versions of list will be cached |
||
44 | */ |
||
45 | protected $cacheDir; |
||
46 | |||
47 | /** |
||
48 | * @var PublicSuffixList Public Suffix List |
||
49 | */ |
||
50 | protected static $domainList = [ |
||
51 | self::ALL_DOMAINS => self::PDP_PSL_PHP_FILE, |
||
52 | self::ICANN_DOMAINS => self::ICANN_PSL_PHP_FILE, |
||
53 | self::PRIVATE_DOMAINS => self::PRIVATE_PSL_PHP_FILE, |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * @var \Pdp\HttpAdapter\HttpAdapterInterface Http adapter |
||
58 | */ |
||
59 | protected $httpAdapter; |
||
60 | |||
61 | /** |
||
62 | * Public constructor. |
||
63 | * |
||
64 | * @param string $cacheDir Optional cache directory |
||
65 | */ |
||
66 | 10 | public function __construct($cacheDir = null) |
|
76 | |||
77 | /** |
||
78 | * Downloads Public Suffix List and writes text cache and PHP cache. If these files |
||
79 | * already exist, they will be overwritten. |
||
80 | */ |
||
81 | 1 | public function refreshPublicSuffixList() |
|
97 | |||
98 | /** |
||
99 | * Obtain Public Suffix List from its online source and write to cache dir. |
||
100 | * |
||
101 | * @return int|bool Number of bytes that were written to the file OR false in case of error |
||
102 | */ |
||
103 | 1 | public function fetchListFromSource() |
|
113 | |||
114 | /** |
||
115 | * Parses text representation of list to associative, multidimensional array. |
||
116 | * |
||
117 | * This method is based heavily on the code found in generateEffectiveTLDs.php |
||
118 | * |
||
119 | * @link https://github.com/usrflo/registered-domain-libs/blob/master/generateEffectiveTLDs.php |
||
120 | * A copy of the Apache License, Version 2.0, is provided with this |
||
121 | * distribution |
||
122 | * |
||
123 | * @param string $textFile Public Suffix List text filename |
||
124 | * |
||
125 | * @return array Associative, multidimensional array representation of the |
||
126 | * public suffx list |
||
127 | * |
||
128 | * @throws \Exception Throws \Exception if unable to read file |
||
129 | */ |
||
130 | 2 | public function parseListToArray($textFile): array |
|
162 | |||
163 | /** |
||
164 | * Recursive method to build the array representation of the Public Suffix List. |
||
165 | * |
||
166 | * This method is based heavily on the code found in generateEffectiveTLDs.php |
||
167 | * |
||
168 | * @link https://github.com/usrflo/registered-domain-libs/blob/master/generateEffectiveTLDs.php |
||
169 | * A copy of the Apache License, Version 2.0, is provided with this |
||
170 | * distribution |
||
171 | * |
||
172 | * @param array $publicSuffixListArray Initially an empty array, this eventually |
||
173 | * becomes the array representation of the Public Suffix List |
||
174 | * @param array $ruleParts One line (rule) from the Public Suffix List |
||
175 | * exploded on '.', or the remaining portion of that array during recursion |
||
176 | */ |
||
177 | 2 | public function buildArray(array &$publicSuffixListArray, array $ruleParts) |
|
207 | |||
208 | /** |
||
209 | * Writes php array representation of the Public Suffix List to disk. |
||
210 | * |
||
211 | * @param array $publicSuffixList Array representation of the Public Suffix List |
||
212 | * |
||
213 | * @return int Number of bytes that were written to the file |
||
214 | */ |
||
215 | 1 | public function writePhpCache(array $publicSuffixList): int |
|
221 | |||
222 | /** |
||
223 | * Writes php array representation to disk. |
||
224 | * |
||
225 | * @param string $basename file path |
||
226 | * @param array $input input data |
||
227 | * |
||
228 | * @return int Number of bytes that were written to the file |
||
229 | */ |
||
230 | 1 | protected function varExportToFile($basename, array $input): int |
|
236 | |||
237 | /** |
||
238 | * Gets Public Suffix List. |
||
239 | * |
||
240 | * @param string $list the Public Suffix List type |
||
241 | * @param bool $withStaticCache |
||
242 | * |
||
243 | * @return PublicSuffixList Instance of Public Suffix List |
||
244 | * |
||
245 | * @throws \Exception Throws \Exception if unable to read file |
||
246 | */ |
||
247 | 4 | public function getList($list = self::ALL_DOMAINS, bool $withStaticCache = true): PublicSuffixList |
|
274 | |||
275 | /** |
||
276 | * Retrieves public suffix list from file after obtaining a shared lock. |
||
277 | * |
||
278 | * @param string $phpFile |
||
279 | * |
||
280 | * @return PublicSuffixList Instance of Public Suffix List |
||
281 | * |
||
282 | * @throws \Exception Throws \Exception if unable to read file |
||
283 | */ |
||
284 | 1 | public function getListFromFile($phpFile): PublicSuffixList |
|
302 | |||
303 | /** |
||
304 | * Parses text representation of list to associative, multidimensional array. |
||
305 | * |
||
306 | * @param string $textFile Public Suffix List text filename |
||
307 | * |
||
308 | * @return array Associative, multidimensional array representation of the |
||
309 | * public suffx list |
||
310 | */ |
||
311 | 1 | protected function convertListToArray($textFile): array |
|
336 | |||
337 | /** |
||
338 | * Convert a line from the Public Suffix list. |
||
339 | * |
||
340 | * @param string $textLine Public Suffix List text line |
||
341 | * @param array $publicSuffixListArray Associative, multidimensional array representation of the |
||
342 | * public suffx list |
||
343 | * @param array $addDomain Tell which section should be converted |
||
344 | * |
||
345 | * @return array Associative, multidimensional array representation of the |
||
346 | * public suffx list |
||
347 | */ |
||
348 | 1 | protected function convertLineToArray($textLine, array $publicSuffixListArray, array $addDomain): array |
|
359 | |||
360 | /** |
||
361 | * Update the addition status for a given line against the domain list (ICANN and PRIVATE). |
||
362 | * |
||
363 | * @param string $line the current file line |
||
364 | * @param array $addDomain the domain addition status |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | 1 | protected function validateDomainAddition($line, array $addDomain): array |
|
376 | |||
377 | /** |
||
378 | * Tell whether the line can be converted for a given domain. |
||
379 | * |
||
380 | * @param bool $previousStatus the previous status |
||
381 | * @param string $line the current file line |
||
382 | * @param string $section the section to be considered |
||
383 | * |
||
384 | * @return bool |
||
385 | */ |
||
386 | 1 | protected function isValidSection($previousStatus, $line, $section): bool |
|
398 | |||
399 | /** |
||
400 | * Writes to file after obtaining an exclusive lock. |
||
401 | * |
||
402 | * @param string $filename Filename in cache dir where data will be written |
||
403 | * @param mixed $data Data to write |
||
404 | * |
||
405 | * @return int Number of bytes that were written to the file |
||
406 | * |
||
407 | * @throws \Exception <p>Throws \Exception if unable to write file.</p> |
||
408 | */ |
||
409 | 2 | protected function write($filename, $data): int |
|
439 | |||
440 | /** |
||
441 | * Returns http adapter. Returns default http adapter if one is not set. |
||
442 | * |
||
443 | * @return \Pdp\HttpAdapter\HttpAdapterInterface Http adapter |
||
444 | */ |
||
445 | 2 | public function getHttpAdapter(): HttpAdapterInterface |
|
457 | |||
458 | /** |
||
459 | * Sets http adapter. |
||
460 | * |
||
461 | * @param \Pdp\HttpAdapter\HttpAdapterInterface $httpAdapter |
||
462 | */ |
||
463 | 10 | public function setHttpAdapter(HttpAdapter\HttpAdapterInterface $httpAdapter) |
|
467 | } |
||
468 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.