Complex classes like PublicSuffixList 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 PublicSuffixList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class PublicSuffixList |
||
| 15 | { |
||
| 16 | protected $sourceURL = 'https://publicsuffix.org/list/public_suffix_list.dat'; |
||
| 17 | protected $localPSL = 'public_suffix_list.dat'; |
||
| 18 | protected $cachedPrefix = 'cached_'; |
||
| 19 | |||
| 20 | protected $tree; |
||
| 21 | protected $url; |
||
| 22 | protected $dataDir = '/../data/'; // relative to __DIR__ |
||
| 23 | |||
| 24 | /** |
||
| 25 | * PublicSuffixList constructor. |
||
| 26 | * @param string|null $url URL for the PSL or null to use default |
||
| 27 | */ |
||
| 28 | public function __construct($url = null) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Set the URL, and clear any existing tree |
||
| 35 | * |
||
| 36 | * @param string|null $url URL for the PSL or null to use default |
||
| 37 | * |
||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | public function setURL($url) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set a fallback (default) for the URL. If we have a locally saved version, prefer it, but use a |
||
| 48 | * remote URL if there is no local source. |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | protected function setFallbackURL() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * load the PSL tree, automatically handling caches |
||
| 62 | * |
||
| 63 | * @return void (results in $this->tree) |
||
| 64 | * |
||
| 65 | * @throws \RuntimeException |
||
| 66 | */ |
||
| 67 | protected function loadTree() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Parse the PSL data |
||
| 90 | * |
||
| 91 | * @param string $fileData the PSL data |
||
| 92 | * |
||
| 93 | * @return void (results in $this->tree) |
||
| 94 | */ |
||
| 95 | protected function parsePSL($fileData) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Does $search start with $startString? |
||
| 113 | * |
||
| 114 | * @param string $search the string to test |
||
| 115 | * @param string $startString the starting string to match |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | protected function startsWith($search, $startString) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Add domains to tree |
||
| 126 | * |
||
| 127 | * @param array $node tree array by reference |
||
| 128 | * @param string[] $tldParts array of domain parts |
||
| 129 | * |
||
| 130 | * @return void - changes made to $node by reference |
||
| 131 | */ |
||
| 132 | protected function buildSubDomain(&$node, $tldParts) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Return the current tree, loading it if needed |
||
| 157 | * |
||
| 158 | * @return array the PSL tree |
||
| 159 | */ |
||
| 160 | public function getTree() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Read PSL from the URL or file specified in $this->url. |
||
| 170 | * If we process a remote URL, save a local copy. |
||
| 171 | * |
||
| 172 | * @return bool|string PSL file contents or false on error |
||
| 173 | */ |
||
| 174 | protected function readPSL() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Determine cache file name for a specified source |
||
| 207 | * |
||
| 208 | * @param string $url URL/filename of source PSL |
||
| 209 | * |
||
| 210 | * @return string cache file name for given resource |
||
| 211 | */ |
||
| 212 | protected function getCacheFileName($url) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Attempt to load a cached Public Suffix List tree for a given source |
||
| 219 | * |
||
| 220 | * @param string $url URL/filename of source PSL |
||
| 221 | * |
||
| 222 | * @return bool|string[] PSL tree |
||
| 223 | */ |
||
| 224 | protected function readCachedPSL($url) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Cache the current Public Suffix List tree and associate with the specified source |
||
| 236 | * |
||
| 237 | * @param string $url URL/filename of source PSL |
||
| 238 | * |
||
| 239 | * @return bool|int the number of bytes that were written to the file, or false on failure |
||
| 240 | */ |
||
| 241 | protected function cachePSL($url) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Save a local copy of a retrieved Public Suffix List |
||
| 248 | * |
||
| 249 | * @param string $fileContents URL/filename of source PSL |
||
| 250 | * |
||
| 251 | * @return bool|int the number of bytes that were written to the file, or false on failure |
||
| 252 | */ |
||
| 253 | protected function saveLocalPSL($fileContents) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Set localPSL name based on URL |
||
| 260 | * |
||
| 261 | * @param null|string $url the URL for the PSL |
||
| 262 | * |
||
| 263 | * @return void (sets $this->localPSL) |
||
| 264 | */ |
||
| 265 | protected function setLocalPSLName($url) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Delete files in the data directory |
||
| 277 | * |
||
| 278 | * @param bool $cacheOnly true to limit clearing to cached serialized PSLs, false to clear all |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | public function clearDataDirectory($cacheOnly = false) |
||
| 298 | } |
||
| 299 |
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.