Complex classes like Site 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 Site, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Site |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Cache for ApacheSolrForTypo3\Solr\Site objects |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected static $sitesCache = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Small cache for the list of pages in a site, so that the results of this |
||
| 51 | * rather expensive operation can be used by all initializers without having |
||
| 52 | * each initializer do it again. |
||
| 53 | * |
||
| 54 | * TODO Move to caching framework once TYPO3 4.6 is the minimum required |
||
| 55 | * version. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected static $sitePagesCache = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Root page record. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $rootPage = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The site's sys_language_mode |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $sysLanguageMode = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Constructor. |
||
| 77 | * |
||
| 78 | * @param int $rootPageId Site root page ID (uid). The page must be marked as site root ("Use as Root Page" flag). |
||
| 79 | */ |
||
| 80 | 97 | public function __construct($rootPageId) |
|
| 81 | { |
||
| 82 | 97 | $page = BackendUtility::getRecord('pages', $rootPageId); |
|
| 83 | |||
| 84 | 97 | if (!self::isRootPage($page)) { |
|
|
|
|||
| 85 | 4 | throw new \InvalidArgumentException( |
|
| 86 | 4 | 'The page for the given page ID \'' . $rootPageId |
|
| 87 | 4 | . '\' is not marked as root page and can therefore not be used as site root page.', |
|
| 88 | 4 | 1309272922 |
|
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | 93 | $this->rootPage = $page; |
|
| 93 | 93 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Gets the Site for a specific page Id. |
||
| 97 | * |
||
| 98 | * @param int $pageId The page Id to get a Site object for. |
||
| 99 | * @return Site Site for the given page Id. |
||
| 100 | */ |
||
| 101 | 57 | public static function getSiteByPageId($pageId) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Creates a dropdown selector of available TYPO3 sites with Solr |
||
| 115 | * configured. |
||
| 116 | * |
||
| 117 | * @param string $selectorName Name to be used in the select's name attribute |
||
| 118 | * @param Site $selectedSite Optional, currently selected site |
||
| 119 | * @return string Site selector HTML code |
||
| 120 | * @todo Extract into own class like indexing configuration selector |
||
| 121 | */ |
||
| 122 | public static function getAvailableSitesSelector( |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Gets all available TYPO3 sites with Solr configured. |
||
| 147 | * |
||
| 148 | * @param bool $stopOnInvalidSite |
||
| 149 | * |
||
| 150 | * @return Site[] An array of available sites |
||
| 151 | */ |
||
| 152 | 59 | public static function getAvailableSites($stopOnInvalidSite = false) |
|
| 153 | { |
||
| 154 | 59 | static $sitesCached; |
|
| 155 | 59 | $sites = []; |
|
| 156 | |||
| 157 | // Check if $sites has been cached |
||
| 158 | 59 | if (isset($sitesCached)) { |
|
| 159 | 15 | return $sitesCached; |
|
| 160 | } |
||
| 161 | |||
| 162 | 59 | $servers = self::getSolrServersFromRegistry(); |
|
| 163 | |||
| 164 | 59 | foreach ($servers as $server) { |
|
| 165 | 59 | if (isset($sites[$server['rootPageUid']])) { |
|
| 166 | //get each site only once |
||
| 167 | 1 | continue; |
|
| 168 | } |
||
| 169 | |||
| 170 | try { |
||
| 171 | 59 | $sites[$server['rootPageUid']] = GeneralUtility::makeInstance(__CLASS__, $server['rootPageUid']); |
|
| 172 | 4 | } catch (\InvalidArgumentException $e) { |
|
| 173 | 4 | if ($stopOnInvalidSite) { |
|
| 174 | 59 | throw $e; |
|
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | 59 | $sitesCached = $sites; |
|
| 180 | |||
| 181 | 59 | return $sitesCached; |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns the first available Site. |
||
| 186 | * |
||
| 187 | * @param bool $stopOnInvalidSite |
||
| 188 | * |
||
| 189 | * @return Site |
||
| 190 | */ |
||
| 191 | 20 | public static function getFirstAvailableSite($stopOnInvalidSite = false) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Clears the $sitePagesCache |
||
| 199 | * |
||
| 200 | */ |
||
| 201 | public static function clearSitePagesCache() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Takes an pagerecord and checks whether the page is marked as root page. |
||
| 208 | * |
||
| 209 | * @param array $page pagerecord |
||
| 210 | * @return bool true if the page is marked as root page, false otherwise |
||
| 211 | */ |
||
| 212 | 103 | public static function isRootPage($page) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Gets the site's root page ID (uid). |
||
| 223 | * |
||
| 224 | * @return int The site's root page ID. |
||
| 225 | */ |
||
| 226 | 32 | public function getRootPageId() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Gets the site's label. The label is build from the the site title and root |
||
| 233 | * page ID (uid). |
||
| 234 | * |
||
| 235 | * @return string The site's label. |
||
| 236 | */ |
||
| 237 | 9 | public function getLabel() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Retrieves the configured solr servers from the registry. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | 59 | protected static function getSolrServersFromRegistry() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*) |
||
| 265 | * |
||
| 266 | * @return \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration |
||
| 267 | */ |
||
| 268 | 49 | public function getSolrConfiguration() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Gets the system languages (IDs) for which Solr connections have been |
||
| 275 | * configured. |
||
| 276 | * |
||
| 277 | * @return array Array of system language IDs for which connections have been configured on this site. |
||
| 278 | */ |
||
| 279 | public function getLanguages() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Gets the site's default language as configured in |
||
| 299 | * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to |
||
| 300 | * be the default. |
||
| 301 | * |
||
| 302 | * @return int The site's default language. |
||
| 303 | */ |
||
| 304 | 1 | public function getDefaultLanguage() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Generates a list of page IDs in this site. Attention, this includes |
||
| 324 | * all page types! Deleted pages are not included. |
||
| 325 | * |
||
| 326 | * @param int|string $rootPageId Page ID from where to start collection sub pages |
||
| 327 | * @param int $maxDepth Maximum depth to descend into the site tree |
||
| 328 | * @return array Array of pages (IDs) in this site |
||
| 329 | */ |
||
| 330 | 8 | public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * This method retrieves the pages ids from the current tree level an calls getPages recursive, |
||
| 362 | * when the maxDepth has not been reached. |
||
| 363 | * |
||
| 364 | * @param int $maxDepth |
||
| 365 | * @param int $recursionRootPageId |
||
| 366 | * @param array $pageIds |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | 8 | protected function getPageIdsFromCurrentDepthAndCallRecursive($maxDepth, $recursionRootPageId, $pageIds) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Generates the site's unique Site Hash. |
||
| 397 | * |
||
| 398 | * The Site Hash is build from the site's main domain, the system encryption |
||
| 399 | * key, and the extension "tx_solr". These components are concatenated and |
||
| 400 | * sha1-hashed. |
||
| 401 | * |
||
| 402 | * @return string Site Hash. |
||
| 403 | */ |
||
| 404 | 52 | public function getSiteHash() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Gets the site's main domain. More specifically the first domain record in |
||
| 413 | * the site tree. |
||
| 414 | * |
||
| 415 | * @return string The site's main domain. |
||
| 416 | */ |
||
| 417 | 56 | public function getDomain() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the site's root page. |
||
| 427 | * |
||
| 428 | * @return array The site's root page. |
||
| 429 | */ |
||
| 430 | 1 | public function getRootPage() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Gets the site's root page's title. |
||
| 437 | * |
||
| 438 | * @return string The site's root page's title |
||
| 439 | */ |
||
| 440 | public function getTitle() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Gets the site's config.sys_language_mode setting |
||
| 447 | * |
||
| 448 | * @return string The site's config.sys_language_mode |
||
| 449 | */ |
||
| 450 | 11 | public function getSysLanguageMode() |
|
| 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.