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 |
||
21 | class PublicSuffixListManager |
||
22 | { |
||
23 | const ALL_DOMAINS = 'ALL'; |
||
24 | |||
25 | const PDP_PSL_TEXT_FILE = 'public-suffix-list.txt'; |
||
26 | const PDP_PSL_PHP_FILE = 'public-suffix-list.php'; |
||
27 | |||
28 | const ICANN_DOMAINS = 'ICANN'; |
||
29 | const ICANN_PSL_PHP_FILE = 'icann-public-suffix-list.php'; |
||
30 | |||
31 | const PRIVATE_DOMAINS = 'PRIVATE'; |
||
32 | const PRIVATE_PSL_PHP_FILE = 'private-public-suffix-list.php'; |
||
33 | |||
34 | /** |
||
35 | * @var string Public Suffix List URL |
||
36 | */ |
||
37 | protected $publicSuffixListUrl = 'https://publicsuffix.org/list/effective_tld_names.dat'; |
||
38 | |||
39 | /** |
||
40 | * @var string Directory where text and php versions of list will be cached |
||
41 | */ |
||
42 | protected $cacheDir; |
||
43 | |||
44 | /** |
||
45 | * @var PublicSuffixList Public Suffix List |
||
46 | */ |
||
47 | protected static $domainList = array( |
||
48 | self::ALL_DOMAINS => self::PDP_PSL_PHP_FILE, |
||
49 | self::ICANN_DOMAINS => self::ICANN_PSL_PHP_FILE, |
||
50 | self::PRIVATE_DOMAINS => self::PRIVATE_PSL_PHP_FILE, |
||
51 | 11 | ); |
|
52 | |||
53 | 11 | /** |
|
54 | 1 | * @var \Pdp\HttpAdapter\HttpAdapterInterface Http adapter |
|
55 | 1 | */ |
|
56 | 1 | protected $httpAdapter; |
|
57 | 1 | ||
58 | /** |
||
59 | 11 | * Public constructor. |
|
60 | 11 | * |
|
61 | * @param string $cacheDir Optional cache directory |
||
62 | */ |
||
63 | public function __construct($cacheDir = null) |
||
73 | 1 | ||
74 | /** |
||
75 | * Downloads Public Suffix List and writes text cache and PHP cache. If these files |
||
76 | * already exist, they will be overwritten. |
||
77 | */ |
||
78 | public function refreshPublicSuffixList() |
||
94 | |||
95 | /** |
||
96 | * Obtain Public Suffix List from its online source and write to cache dir. |
||
97 | * |
||
98 | * @return int|bool Number of bytes that were written to the file OR false in case of error |
||
99 | */ |
||
100 | public function fetchListFromSource() |
||
110 | 4 | ||
111 | 4 | /** |
|
112 | 1 | * Parses text representation of list to associative, multidimensional array. |
|
113 | * |
||
114 | * This method is based heavily on the code found in generateEffectiveTLDs.php |
||
115 | 3 | * |
|
116 | 3 | * @link https://github.com/usrflo/registered-domain-libs/blob/master/generateEffectiveTLDs.php |
|
117 | 3 | * A copy of the Apache License, Version 2.0, is provided with this |
|
118 | 3 | * distribution |
|
119 | * |
||
120 | 3 | * @param string $textFile Public Suffix List text filename |
|
121 | 3 | * |
|
122 | * @return array Associative, multidimensional array representation of the |
||
123 | 3 | * public suffx list |
|
124 | 3 | * |
|
125 | 3 | * @throws \Exception Throws \Exception if unable to read file |
|
126 | 3 | */ |
|
127 | public function parseListToArray($textFile) |
||
159 | |||
160 | /** |
||
161 | * Recursive method to build the array representation of the Public Suffix List. |
||
162 | * |
||
163 | * This method is based heavily on the code found in generateEffectiveTLDs.php |
||
164 | 3 | * |
|
165 | 3 | * @link https://github.com/usrflo/registered-domain-libs/blob/master/generateEffectiveTLDs.php |
|
166 | * A copy of the Apache License, Version 2.0, is provided with this |
||
167 | 3 | * distribution |
|
168 | 3 | * |
|
169 | 3 | * @param array $publicSuffixListArray Initially an empty array, this eventually |
|
170 | 3 | * becomes the array representation of the Public Suffix List |
|
171 | * @param array $ruleParts One line (rule) from the Public Suffix List |
||
172 | 3 | * exploded on '.', or the remaining portion of that array during recursion |
|
173 | 3 | */ |
|
174 | 3 | public function buildArray(array &$publicSuffixListArray, array $ruleParts) |
|
204 | |||
205 | /** |
||
206 | 3 | * Writes php array representation of the Public Suffix List to disk. |
|
207 | * |
||
208 | 3 | * @param array $publicSuffixList Array representation of the Public Suffix List |
|
209 | * |
||
210 | 3 | * @return int Number of bytes that were written to the file |
|
211 | 1 | */ |
|
212 | 1 | public function writePhpCache(array $publicSuffixList) |
|
218 | |||
219 | /** |
||
220 | * Writes php array representation to disk. |
||
221 | * |
||
222 | * @param string $basename file path |
||
223 | * @param array $input input data |
||
224 | * |
||
225 | * @return int Number of bytes that were written to the file |
||
226 | 4 | */ |
|
227 | protected function varExportToFile($basename, array $input) |
||
233 | |||
234 | /** |
||
235 | 3 | * Gets Public Suffix List. |
|
236 | * |
||
237 | 3 | * @param string $list the Public Suffix List type |
|
238 | * @param bool $withStaticCache |
||
239 | 3 | * |
|
240 | 3 | * @return PublicSuffixList Instance of Public Suffix List |
|
241 | * |
||
242 | 3 | * @throws \Exception Throws \Exception if unable to read file |
|
243 | */ |
||
244 | public function getList($list = self::ALL_DOMAINS, $withStaticCache = true) |
||
267 | |||
268 | 4 | /** |
|
269 | 1 | * Retrieves public suffix list from file after obtaining a shared lock. |
|
270 | 1 | * |
|
271 | * @param string $phpFile |
||
272 | * |
||
273 | 3 | * @return PublicSuffixList Instance of Public Suffix List |
|
274 | 3 | * |
|
275 | * @throws \Exception Throws \Exception if unable to read file |
||
276 | 3 | */ |
|
277 | public function getListFromFile($phpFile) |
||
295 | |||
296 | /** |
||
297 | * Parses text representation of list to associative, multidimensional array. |
||
298 | * |
||
299 | * @param string $textFile Public Suffix List text filename |
||
300 | * |
||
301 | * @return array Associative, multidimensional array representation of the |
||
302 | 11 | * public suffx list |
|
303 | */ |
||
304 | 11 | protected function convertListToArray($textFile) |
|
329 | |||
330 | /** |
||
331 | * Convert a line from the Public Suffix list. |
||
332 | * |
||
333 | * @param string $textLine Public Suffix List text line |
||
334 | * @param array $publicSuffixListArray Associative, multidimensional array representation of the |
||
335 | * public suffx list |
||
336 | * @param array $addDomain Tell which section should be converted |
||
337 | * |
||
338 | * @return array Associative, multidimensional array representation of the |
||
339 | * public suffx list |
||
340 | */ |
||
341 | protected function convertLineToArray($textLine, array $publicSuffixListArray, array $addDomain) |
||
352 | |||
353 | /** |
||
354 | * Update the addition status for a given line against the domain list (ICANN and PRIVATE). |
||
355 | * |
||
356 | * @param string $line the current file line |
||
357 | * @param array $addDomain the domain addition status |
||
358 | * |
||
359 | * @return array |
||
360 | */ |
||
361 | protected function validateDomainAddition($line, array $addDomain) |
||
369 | |||
370 | /** |
||
371 | * Tell whether the line can be converted for a given domain. |
||
372 | * |
||
373 | * @param bool $previousStatus the previous status |
||
374 | * @param string $line the current file line |
||
375 | * @param string $section the section to be considered |
||
376 | * |
||
377 | * @return bool |
||
378 | */ |
||
379 | protected function isValidSection($previousStatus, $line, $section) |
||
391 | |||
392 | /** |
||
393 | * Writes to file after obtaining an exclusive lock. |
||
394 | * |
||
395 | * @param string $filename Filename in cache dir where data will be written |
||
396 | * @param mixed $data Data to write |
||
397 | * |
||
398 | * @return int Number of bytes that were written to the file |
||
399 | * |
||
400 | * @throws \Exception Throws \Exception if unable to write file |
||
401 | */ |
||
402 | protected function write($filename, $data) |
||
430 | |||
431 | /** |
||
432 | * Returns http adapter. Returns default http adapter if one is not set. |
||
433 | * |
||
434 | * @return \Pdp\HttpAdapter\HttpAdapterInterface Http adapter |
||
435 | */ |
||
436 | public function getHttpAdapter() |
||
448 | |||
449 | /** |
||
450 | * Sets http adapter. |
||
451 | * |
||
452 | * @param \Pdp\HttpAdapter\HttpAdapterInterface $httpAdapter |
||
453 | */ |
||
454 | public function setHttpAdapter(HttpAdapter\HttpAdapterInterface $httpAdapter) |
||
458 | } |
||
459 |
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.