We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 52 |
| Total Lines | 657 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like tx_dlf_solr 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 tx_dlf_solr, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class tx_dlf_solr { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * This holds the core name |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | * @access protected |
||
| 28 | */ |
||
| 29 | protected $core = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * This holds the PID for the configuration |
||
| 33 | * |
||
| 34 | * @var integer |
||
| 35 | * @access protected |
||
| 36 | */ |
||
| 37 | protected $cPid = 0; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The extension key |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | * @access public |
||
| 44 | */ |
||
| 45 | public static $extKey = 'dlf'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * This holds the max results |
||
| 49 | * |
||
| 50 | * @var integer |
||
| 51 | * @access protected |
||
| 52 | */ |
||
| 53 | protected $limit = 50000; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * This holds the number of hits for last search |
||
| 57 | * |
||
| 58 | * @var integer |
||
| 59 | * @access protected |
||
| 60 | */ |
||
| 61 | protected $numberOfHits = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * This holds the additional query parameters |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | * @access protected |
||
| 68 | */ |
||
| 69 | protected $params = array (); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Is the search instantiated successfully? |
||
| 73 | * |
||
| 74 | * @var boolean |
||
| 75 | * @access protected |
||
| 76 | */ |
||
| 77 | protected $ready = FALSE; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * This holds the singleton search objects with their core as array key |
||
| 81 | * |
||
| 82 | * @var array(tx_dlf_solr) |
||
| 83 | * @access protected |
||
| 84 | */ |
||
| 85 | protected static $registry = array (); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * This holds the Solr service object |
||
| 89 | * |
||
| 90 | * @var Solarium\Client |
||
| 91 | * @access protected |
||
| 92 | */ |
||
| 93 | protected $service; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Escape all special characters in a query string |
||
| 97 | * |
||
| 98 | * @access public |
||
| 99 | * |
||
| 100 | * @param string $query: The query string |
||
| 101 | * |
||
| 102 | * @return string The escaped query string |
||
| 103 | */ |
||
| 104 | public static function escapeQuery($query) { |
||
| 105 | |||
| 106 | $helper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Solarium\Core\Query\Helper'); |
||
| 107 | |||
| 108 | // Escape query phrase or term. |
||
| 109 | if (preg_match('/^".*"$/', $query)) { |
||
| 110 | |||
| 111 | return '"'.$helper->escapePhrase(trim($query, '"')).'"'; |
||
| 112 | |||
| 113 | } else { |
||
| 114 | |||
| 115 | return $helper->escapeTerm($query); |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Escape all special characters in a query string while retaining valid field queries |
||
| 123 | * |
||
| 124 | * @access public |
||
| 125 | * |
||
| 126 | * @param string $query: The query string |
||
| 127 | * @param integer $pid: The PID for the field configuration |
||
| 128 | * |
||
| 129 | * @return string The escaped query string |
||
| 130 | */ |
||
| 131 | public static function escapeQueryKeepField($query, $pid) { |
||
| 179 | |||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * This is a singleton class, thus instances must be created by this method |
||
| 184 | * |
||
| 185 | * @access public |
||
| 186 | * |
||
| 187 | * @param mixed $core: Name or UID of the core to load |
||
| 188 | * |
||
| 189 | * @return tx_dlf_solr Instance of this class |
||
| 190 | */ |
||
| 191 | public static function getInstance($core) { |
||
| 244 | |||
| 245 | } |
||
| 246 | |||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Returns the connection information for Solr |
||
| 251 | * |
||
| 252 | * @access public |
||
| 253 | * |
||
| 254 | * @return string The connection parameters for a specific Solr core |
||
| 255 | */ |
||
| 256 | public static function getSolrConnectionInfo() { |
||
| 257 | |||
| 258 | $solrInfo = array (); |
||
| 259 | |||
| 260 | // Extract extension configuration. |
||
| 261 | $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]); |
||
| 262 | |||
| 263 | // Derive Solr scheme |
||
| 264 | $solrInfo['scheme'] = empty($conf['solrHttps']) ? 'http' : 'https'; |
||
| 265 | |||
| 266 | // Derive Solr host name. |
||
| 267 | $solrInfo['host'] = ($conf['solrHost'] ? $conf['solrHost'] : '127.0.0.1'); |
||
| 268 | |||
| 269 | // Set username and password. |
||
| 270 | $solrInfo['username'] = $conf['solrUser']; |
||
| 271 | $solrInfo['password'] = $conf['solrPass']; |
||
| 272 | |||
| 273 | // Set port if not set. |
||
| 274 | $solrInfo['port'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($conf['solrPort'], 1, 65535, 8983); |
||
| 275 | |||
| 276 | // Append core name to path. |
||
| 277 | $solrInfo['path'] = trim($conf['solrPath'], '/'); |
||
| 278 | |||
| 279 | // Timeout |
||
| 280 | $solrInfo['timeout'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($conf['solrTimeout'], 1, intval(ini_get('max_execution_time')), 10); |
||
| 281 | |||
| 282 | return $solrInfo; |
||
| 283 | |||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the request URL for a specific Solr core |
||
| 288 | * |
||
| 289 | * @access public |
||
| 290 | * |
||
| 291 | * @param string $core: Name of the core to load |
||
| 292 | * |
||
| 293 | * @return string The request URL for a specific Solr core |
||
| 294 | */ |
||
| 295 | public static function getSolrUrl($core = '') { |
||
| 312 | |||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get next unused Solr core number |
||
| 317 | * |
||
| 318 | * @access public |
||
| 319 | * |
||
| 320 | * @param integer $start: Number to start with |
||
| 321 | * |
||
| 322 | * @return integer First unused core number found |
||
| 323 | */ |
||
| 324 | public static function solrGetCoreNumber($start = 0) { |
||
| 336 | |||
| 337 | } |
||
| 338 | |||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Processes a search request. |
||
| 343 | * |
||
| 344 | * @access public |
||
| 345 | * |
||
| 346 | * @return tx_dlf_list The result list |
||
| 347 | */ |
||
| 348 | public function search() { |
||
| 437 | |||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Processes a search request and returns the raw Apache Solr Documents. |
||
| 442 | * |
||
| 443 | * @access public |
||
| 444 | * |
||
| 445 | * @param string $query: The search query |
||
| 446 | * @param array $parameters: Additional search parameters |
||
| 447 | * |
||
| 448 | * @return array The Apache Solr Documents that were fetched |
||
| 449 | */ |
||
| 450 | public function search_raw($query = '', $parameters = array ()) { |
||
| 451 | |||
| 452 | // Set additional query parameters. |
||
| 453 | $parameters['start'] = 0; |
||
| 454 | $parameters['rows'] = $this->limit; |
||
| 455 | |||
| 456 | // Set query. |
||
| 457 | $parameters['query'] = $query; |
||
| 458 | |||
| 459 | // Perform search. |
||
| 460 | $selectQuery = $this->service->createSelect(array_merge($this->params, $parameters)); |
||
| 461 | $result = $this->service->select($selectQuery); |
||
| 462 | |||
| 463 | $resultSet = array (); |
||
| 464 | |||
| 465 | foreach ($result as $doc) { |
||
| 466 | |||
| 467 | $resultSet[] = $doc; |
||
| 468 | |||
| 469 | } |
||
| 470 | |||
| 471 | return $resultSet; |
||
| 472 | |||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * This returns $this->limit via __get() |
||
| 477 | * |
||
| 478 | * @access protected |
||
| 479 | * |
||
| 480 | * @return integer The max number of results |
||
| 481 | */ |
||
| 482 | protected function _getLimit() { |
||
| 483 | |||
| 484 | return $this->limit; |
||
| 485 | |||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * This returns $this->numberOfHits via __get() |
||
| 490 | * |
||
| 491 | * @access protected |
||
| 492 | * |
||
| 493 | * @return integer Total number of hits for last search |
||
| 494 | */ |
||
| 495 | protected function _getNumberOfHits() { |
||
| 496 | |||
| 497 | return $this->numberOfHits; |
||
| 498 | |||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * This returns $this->ready via __get() |
||
| 503 | * |
||
| 504 | * @access protected |
||
| 505 | * |
||
| 506 | * @return boolean Is the search instantiated successfully? |
||
| 507 | */ |
||
| 508 | protected function _getReady() { |
||
| 509 | |||
| 510 | return $this->ready; |
||
| 511 | |||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * This returns $this->service via __get() |
||
| 516 | * |
||
| 517 | * @access protected |
||
| 518 | * |
||
| 519 | * @return Solarium\Client Apache Solr service object |
||
| 520 | */ |
||
| 521 | protected function _getService() { |
||
| 524 | |||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * This sets $this->cPid via __set() |
||
| 529 | * |
||
| 530 | * @access protected |
||
| 531 | * |
||
| 532 | * @param integer $value: The new PID for the metadata definitions |
||
| 533 | * |
||
| 534 | * @return void |
||
| 535 | */ |
||
| 536 | protected function _setCPid($value) { |
||
| 537 | |||
| 538 | $this->cPid = max(intval($value), 0); |
||
| 539 | |||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * This sets $this->limit via __set() |
||
| 544 | * |
||
| 545 | * @access protected |
||
| 546 | * |
||
| 547 | * @param integer $value: The max number of results |
||
| 548 | * |
||
| 549 | * @return void |
||
| 550 | */ |
||
| 551 | protected function _setLimit($value) { |
||
| 552 | |||
| 553 | $this->limit = max(intval($value), 0); |
||
| 554 | |||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * This sets $this->params via __set() |
||
| 559 | * |
||
| 560 | * @access protected |
||
| 561 | * |
||
| 562 | * @param array $value: The query parameters |
||
| 563 | * |
||
| 564 | * @return void |
||
| 565 | */ |
||
| 566 | protected function _setParams(array $value) { |
||
| 567 | |||
| 568 | $this->params = $value; |
||
| 569 | |||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * This magic method is called each time an invisible property is referenced from the object |
||
| 574 | * |
||
| 575 | * @access public |
||
| 576 | * |
||
| 577 | * @param string $var: Name of variable to get |
||
| 578 | * |
||
| 579 | * @return mixed Value of $this->$var |
||
| 580 | */ |
||
| 581 | public function __get($var) { |
||
| 582 | |||
| 583 | $method = '_get'.ucfirst($var); |
||
| 584 | |||
| 585 | if (!property_exists($this, $var) || !method_exists($this, $method)) { |
||
| 586 | |||
| 587 | if (TYPO3_DLOG) { |
||
| 588 | |||
| 589 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_solr->__get('.$var.')] There is no getter function for property "'.$var.'"', self::$extKey, SYSLOG_SEVERITY_WARNING); |
||
| 590 | |||
| 591 | } |
||
| 592 | |||
| 593 | return; |
||
| 594 | |||
| 595 | } else { |
||
| 596 | |||
| 597 | return $this->$method(); |
||
| 598 | |||
| 599 | } |
||
| 600 | |||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * This magic method is called each time an invisible property is referenced from the object |
||
| 605 | * |
||
| 606 | * @access public |
||
| 607 | * |
||
| 608 | * @param string $var: Name of variable to set |
||
| 609 | * @param mixed $value: New value of variable |
||
| 610 | * |
||
| 611 | * @return void |
||
| 612 | */ |
||
| 613 | public function __set($var, $value) { |
||
| 614 | |||
| 615 | $method = '_set'.ucfirst($var); |
||
| 616 | |||
| 617 | if (!property_exists($this, $var) || !method_exists($this, $method)) { |
||
| 618 | |||
| 619 | if (TYPO3_DLOG) { |
||
| 620 | |||
| 621 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_solr->__set('.$var.', [data])] There is no setter function for property "'.$var.'"', self::$extKey, SYSLOG_SEVERITY_WARNING, $value); |
||
| 622 | |||
| 623 | } |
||
| 624 | |||
| 625 | } else { |
||
| 626 | |||
| 627 | $this->$method($value); |
||
| 628 | |||
| 629 | } |
||
| 630 | |||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * This is a singleton class, thus the constructor should be private/protected |
||
| 635 | * |
||
| 636 | * @access protected |
||
| 637 | * |
||
| 638 | * @param string $core: The name of the core to use |
||
| 639 | * |
||
| 640 | * @return void |
||
| 641 | */ |
||
| 642 | protected function __construct($core) { |
||
| 678 | |||
| 679 | // Nothing to do here. |
||
| 680 | |||
| 681 | } |
||
| 682 | |||
| 683 | } |
||
| 684 | |||
| 685 | } |
||
| 686 |