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 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. 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 Solr, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Solr { |
||
| 24 | /** |
||
| 25 | * This holds the core name |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | * @access protected |
||
| 29 | */ |
||
| 30 | protected $core = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * This holds the PID for the configuration |
||
| 34 | * |
||
| 35 | * @var integer |
||
| 36 | * @access protected |
||
| 37 | */ |
||
| 38 | protected $cPid = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The extension key |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | * @access public |
||
| 45 | */ |
||
| 46 | public static $extKey = 'dlf'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This holds the max results |
||
| 50 | * |
||
| 51 | * @var integer |
||
| 52 | * @access protected |
||
| 53 | */ |
||
| 54 | protected $limit = 50000; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * This holds the number of hits for last search |
||
| 58 | * |
||
| 59 | * @var integer |
||
| 60 | * @access protected |
||
| 61 | */ |
||
| 62 | protected $numberOfHits = 0; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * This holds the additional query parameters |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | * @access protected |
||
| 69 | */ |
||
| 70 | protected $params = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Is the search instantiated successfully? |
||
| 74 | * |
||
| 75 | * @var boolean |
||
| 76 | * @access protected |
||
| 77 | */ |
||
| 78 | protected $ready = FALSE; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * This holds the singleton search objects with their core as array key |
||
| 82 | * |
||
| 83 | * @var array (\Kitodo\Dlf\Common\Solr) |
||
| 84 | * @access protected |
||
| 85 | */ |
||
| 86 | protected static $registry = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * This holds the Solr service object |
||
| 90 | * |
||
| 91 | * @var \Solarium\Client |
||
| 92 | * @access protected |
||
| 93 | */ |
||
| 94 | protected $service; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Escape all special characters in a query string |
||
| 98 | * |
||
| 99 | * @access public |
||
| 100 | * |
||
| 101 | * @param string $query: The query string |
||
|
|
|||
| 102 | * |
||
| 103 | * @return string The escaped query string |
||
| 104 | */ |
||
| 105 | public static function escapeQuery($query) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Escape all special characters in a query string while retaining valid field queries |
||
| 117 | * |
||
| 118 | * @access public |
||
| 119 | * |
||
| 120 | * @param string $query: The query string |
||
| 121 | * @param integer $pid: The PID for the field configuration |
||
| 122 | * |
||
| 123 | * @return string The escaped query string |
||
| 124 | */ |
||
| 125 | public static function escapeQueryKeepField($query, $pid) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * This is a singleton class, thus instances must be created by this method |
||
| 163 | * |
||
| 164 | * @access public |
||
| 165 | * |
||
| 166 | * @param mixed $core: Name or UID of the core to load |
||
| 167 | * |
||
| 168 | * @return \Kitodo\Dlf\Common\Solr Instance of this class |
||
| 169 | */ |
||
| 170 | public static function getInstance($core) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the connection information for Solr |
||
| 201 | * |
||
| 202 | * @access public |
||
| 203 | * |
||
| 204 | * @return string The connection parameters for a specific Solr core |
||
| 205 | */ |
||
| 206 | public static function getSolrConnectionInfo() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the request URL for a specific Solr core |
||
| 228 | * |
||
| 229 | * @access public |
||
| 230 | * |
||
| 231 | * @param string $core: Name of the core to load |
||
| 232 | * |
||
| 233 | * @return string The request URL for a specific Solr core |
||
| 234 | */ |
||
| 235 | public static function getSolrUrl($core = '') { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get next unused Solr core number |
||
| 250 | * |
||
| 251 | * @access public |
||
| 252 | * |
||
| 253 | * @param integer $start: Number to start with |
||
| 254 | * |
||
| 255 | * @return integer First unused core number found |
||
| 256 | */ |
||
| 257 | public static function solrGetCoreNumber($start = 0) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Processes a search request. |
||
| 269 | * |
||
| 270 | * @access public |
||
| 271 | * |
||
| 272 | * @return \Kitodo\Dlf\Common\DocumentList The result list |
||
| 273 | */ |
||
| 274 | public function search() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Processes a search request and returns the raw Apache Solr Documents. |
||
| 341 | * |
||
| 342 | * @access public |
||
| 343 | * |
||
| 344 | * @param string $query: The search query |
||
| 345 | * @param array $parameters: Additional search parameters |
||
| 346 | * |
||
| 347 | * @return array The Apache Solr Documents that were fetched |
||
| 348 | */ |
||
| 349 | public function search_raw($query = '', $parameters = []) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * This returns $this->limit via __get() |
||
| 367 | * |
||
| 368 | * @access protected |
||
| 369 | * |
||
| 370 | * @return integer The max number of results |
||
| 371 | */ |
||
| 372 | protected function _getLimit() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * This returns $this->numberOfHits via __get() |
||
| 378 | * |
||
| 379 | * @access protected |
||
| 380 | * |
||
| 381 | * @return integer Total number of hits for last search |
||
| 382 | */ |
||
| 383 | protected function _getNumberOfHits() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * This returns $this->ready via __get() |
||
| 389 | * |
||
| 390 | * @access protected |
||
| 391 | * |
||
| 392 | * @return boolean Is the search instantiated successfully? |
||
| 393 | */ |
||
| 394 | protected function _getReady() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * This returns $this->service via __get() |
||
| 400 | * |
||
| 401 | * @access protected |
||
| 402 | * |
||
| 403 | * @return \Solarium\Client Apache Solr service object |
||
| 404 | */ |
||
| 405 | protected function _getService() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * This sets $this->cPid via __set() |
||
| 411 | * |
||
| 412 | * @access protected |
||
| 413 | * |
||
| 414 | * @param integer $value: The new PID for the metadata definitions |
||
| 415 | * |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | protected function _setCPid($value) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * This sets $this->limit via __set() |
||
| 424 | * |
||
| 425 | * @access protected |
||
| 426 | * |
||
| 427 | * @param integer $value: The max number of results |
||
| 428 | * |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | protected function _setLimit($value) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * This sets $this->params via __set() |
||
| 437 | * |
||
| 438 | * @access protected |
||
| 439 | * |
||
| 440 | * @param array $value: The query parameters |
||
| 441 | * |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | protected function _setParams(array $value) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * This magic method is called each time an invisible property is referenced from the object |
||
| 450 | * |
||
| 451 | * @access public |
||
| 452 | * |
||
| 453 | * @param string $var: Name of variable to get |
||
| 454 | * |
||
| 455 | * @return mixed Value of $this->$var |
||
| 456 | */ |
||
| 457 | View Code Duplication | public function __get($var) { |
|
| 467 | |||
| 468 | /** |
||
| 469 | * This magic method is called each time an invisible property is referenced from the object |
||
| 470 | * |
||
| 471 | * @access public |
||
| 472 | * |
||
| 473 | * @param string $var: Name of variable to set |
||
| 474 | * @param mixed $value: New value of variable |
||
| 475 | * |
||
| 476 | * @return void |
||
| 477 | */ |
||
| 478 | View Code Duplication | public function __set($var, $value) { |
|
| 487 | |||
| 488 | /** |
||
| 489 | * This is a singleton class, thus the constructor should be private/protected |
||
| 490 | * |
||
| 491 | * @access protected |
||
| 492 | * |
||
| 493 | * @param string $core: The name of the core to use |
||
| 494 | * |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | protected function __construct($core) { |
||
| 527 | } |
||
| 528 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.