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