| Total Complexity | 44 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ext_update 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.
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 ext_update, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ext_update { |
||
| 24 | /** |
||
| 25 | * This holds the output ready to return |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | * @access protected |
||
| 29 | */ |
||
| 30 | protected $content = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Triggers the update option in the extension manager |
||
| 34 | * |
||
| 35 | * @access public |
||
| 36 | * |
||
| 37 | * @return boolean Should the update option be shown? |
||
| 38 | */ |
||
| 39 | public function access() { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get all outdated metadata configuration records |
||
| 54 | * |
||
| 55 | * @access protected |
||
| 56 | * |
||
| 57 | * @return array Array of UIDs of outdated records |
||
| 58 | */ |
||
| 59 | protected function getMetadataConfig() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The main method of the class |
||
| 87 | * |
||
| 88 | * @access public |
||
| 89 | * |
||
| 90 | * @return string The content that is displayed on the website |
||
| 91 | */ |
||
| 92 | public function main() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Check for old format classes |
||
| 113 | * |
||
| 114 | * @access protected |
||
| 115 | * |
||
| 116 | * @return boolean true if old format classes exist |
||
| 117 | */ |
||
| 118 | protected function oldFormatClasses() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check for old index related colums |
||
| 138 | * |
||
| 139 | * @access protected |
||
| 140 | * |
||
| 141 | * @return boolean TRUE if old index related columns exist |
||
| 142 | */ |
||
| 143 | protected function oldIndexRelatedTableNames() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Copy the data of the old index related columns to the new columns |
||
| 165 | * |
||
| 166 | * @access protected |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | protected function renameIndexRelatedColumns() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Update all outdated format records |
||
| 197 | * |
||
| 198 | * @access protected |
||
| 199 | * |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | protected function updateFormatClasses() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Update all outdated metadata configuration records |
||
| 223 | * |
||
| 224 | * @access protected |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | protected function updateMetadataConfig() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Check all configured Solr cores |
||
| 280 | * |
||
| 281 | * @access protected |
||
| 282 | * |
||
| 283 | * @return boolean |
||
| 284 | */ |
||
| 285 | protected function solariumSolrUpdateRequired() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Create all configured Solr cores |
||
| 307 | * |
||
| 308 | * @access protected |
||
| 309 | * |
||
| 310 | * @return void |
||
| 311 | */ |
||
| 312 | protected function doSolariumSolrUpdate() { |
||
| 313 | // Get all Solr cores that were not deleted. |
||
| 314 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 315 | 'index_name', |
||
| 316 | 'tx_dlf_solrcores', |
||
| 317 | 'deleted=0', |
||
| 318 | '', |
||
| 319 | '', |
||
| 320 | '' |
||
| 321 | ); |
||
| 322 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 323 | // Instantiate search object. |
||
| 324 | $solr = Solr::getInstance($resArray['index_name']); |
||
| 325 | if (!$solr->ready) { |
||
| 326 | $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['dlf']); |
||
| 327 | $solrInfo = Solr::getSolrConnectionInfo(); |
||
| 328 | // Prepend username and password to hostname. |
||
| 329 | View Code Duplication | if ($solrInfo['username'] |
|
| 330 | && $solrInfo['password']) { |
||
| 331 | $host = $solrInfo['username'].':'.$solrInfo['password'].'@'.$solrInfo['host']; |
||
| 332 | } else { |
||
| 333 | $host = $solrInfo['host']; |
||
| 334 | } |
||
| 335 | $context = stream_context_create([ |
||
| 336 | 'http' => [ |
||
| 337 | 'method' => 'GET', |
||
| 338 | 'user_agent' => ($conf['useragent'] ? $conf['useragent'] : ini_get('user_agent')) |
||
| 339 | ] |
||
| 340 | ]); |
||
| 341 | // Build request for adding new Solr core. |
||
| 342 | // @see http://wiki.apache.org/solr/CoreAdmin |
||
| 343 | $url = $solrInfo['scheme'].'://'.$host.':'.$solrInfo['port'].'/'.$solrInfo['path'].'/admin/cores?wt=xml&action=CREATE&name='.$resArray['index_name'].'&instanceDir='.$resArray['index_name'].'&dataDir=data&configSet=dlf'; |
||
| 344 | $response = @simplexml_load_string(file_get_contents($url, FALSE, $context)); |
||
| 345 | // Process response. |
||
| 346 | if ($response) { |
||
| 347 | $status = $response->xpath('//lst[@name="responseHeader"]/int[@name="status"]'); |
||
| 348 | if ($status |
||
| 349 | && $status[0] == 0) { |
||
| 350 | continue; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | Helper::addMessage( |
||
| 354 | $GLOBALS['LANG']->getLL('update.solariumSolrUpdateNotOkay', TRUE), |
||
| 355 | sprintf($GLOBALS['LANG']->getLL('update.solariumSolrUpdate', TRUE), $resArray['index_name']), |
||
| 356 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR |
||
| 357 | ); |
||
| 358 | $this->content .= Helper::renderFlashMessages(); |
||
| 359 | return; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | Helper::addMessage( |
||
| 363 | $GLOBALS['LANG']->getLL('update.solariumSolrUpdateOkay', TRUE), |
||
| 364 | $GLOBALS['LANG']->getLL('update.solariumSolrUpdate', TRUE), |
||
| 365 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK |
||
| 366 | ); |
||
| 367 | $this->content .= Helper::renderFlashMessages(); |
||
| 368 | } |
||
| 370 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.