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:
| 1 | <?php |
||
| 16 | class FunctionScoreTest extends BaseTest |
||
| 17 | { |
||
| 18 | protected $locationOrigin = '32.804654, -117.242594'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @group unit |
||
| 22 | */ |
||
| 23 | public function testToArray(): void |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @group unit |
||
| 61 | */ |
||
| 62 | public function testDecayWeight(): void |
||
| 63 | { |
||
| 64 | $priceOrigin = 0; |
||
| 65 | $locationScale = '2mi'; |
||
| 66 | $priceScale = 9.25; |
||
| 67 | $query = new FunctionScore(); |
||
| 68 | $childQuery = new MatchAll(); |
||
| 69 | $query->setQuery($childQuery); |
||
| 70 | $query->addDecayFunction( |
||
| 71 | FunctionScore::DECAY_GAUSS, |
||
| 72 | 'location', |
||
| 73 | $this->locationOrigin, |
||
| 74 | $locationScale, |
||
| 75 | null, |
||
| 76 | null, |
||
| 77 | .5, |
||
| 78 | null, |
||
| 79 | FunctionScore::MULTI_VALUE_MODE_AVG |
||
| 80 | ); |
||
| 81 | $query->addDecayFunction( |
||
| 82 | FunctionScore::DECAY_GAUSS, |
||
| 83 | 'price', |
||
| 84 | $priceOrigin, |
||
| 85 | $priceScale, |
||
| 86 | null, |
||
| 87 | null, |
||
| 88 | 2, |
||
| 89 | null, |
||
| 90 | FunctionScore::MULTI_VALUE_MODE_MAX |
||
| 91 | ); |
||
| 92 | $expected = [ |
||
| 93 | 'function_score' => [ |
||
| 94 | 'query' => $childQuery->toArray(), |
||
| 95 | 'functions' => [ |
||
| 96 | [ |
||
| 97 | 'gauss' => [ |
||
| 98 | 'location' => [ |
||
| 99 | 'origin' => $this->locationOrigin, |
||
| 100 | 'scale' => $locationScale, |
||
| 101 | ], |
||
| 102 | 'multi_value_mode' => FunctionScore::MULTI_VALUE_MODE_AVG, |
||
| 103 | ], |
||
| 104 | 'weight' => .5, |
||
| 105 | ], |
||
| 106 | [ |
||
| 107 | 'gauss' => [ |
||
| 108 | 'price' => [ |
||
| 109 | 'origin' => $priceOrigin, |
||
| 110 | 'scale' => $priceScale, |
||
| 111 | ], |
||
| 112 | 'multi_value_mode' => FunctionScore::MULTI_VALUE_MODE_MAX, |
||
| 113 | ], |
||
| 114 | 'weight' => 2, |
||
| 115 | ], |
||
| 116 | ], |
||
| 117 | ], |
||
| 118 | ]; |
||
| 119 | $this->assertEquals($expected, $query->toArray()); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @group functional |
||
| 124 | */ |
||
| 125 | public function testGauss(): void |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @group functional |
||
| 140 | */ |
||
| 141 | public function testGaussMultiValue(): void |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @group unit |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function testAddWeightFunction(): void |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @group unit |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function testLegacyFilterAddWeightFunction(): void |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @group functional |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function testWeight(): void |
|
| 199 | { |
||
| 200 | $filter = new Term(['price' => 4.5]); |
||
| 201 | $query = new FunctionScore(); |
||
| 202 | $query->addWeightFunction(5.0, $filter); |
||
| 203 | |||
| 204 | $expected = [ |
||
| 205 | 'function_score' => [ |
||
| 206 | 'functions' => [ |
||
| 207 | [ |
||
| 208 | 'weight' => 5.0, |
||
| 209 | 'filter' => [ |
||
| 210 | 'term' => [ |
||
| 211 | 'price' => 4.5, |
||
| 212 | ], |
||
| 213 | ], |
||
| 214 | ], |
||
| 215 | ], |
||
| 216 | ], |
||
| 217 | ]; |
||
| 218 | |||
| 219 | $this->assertEquals($expected, $query->toArray()); |
||
| 220 | |||
| 221 | $response = $this->_getIndexForTest()->search($query); |
||
| 222 | $results = $response->getResults(); |
||
| 223 | |||
| 224 | // the document with price = 4.5 should be scored highest |
||
| 225 | $result0 = $results[0]->getData(); |
||
| 226 | $this->assertEquals("Mr. Frostie's", $result0['name']); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @group functional |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function testWeightWithLegacyFilter(): void |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @group functional |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function testRandomScore(): void |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @group functional |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function testRandomScoreWithLegacyFilter(): void |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @group unit |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function testRandomScoreWeight(): void |
|
| 343 | { |
||
| 344 | $filter = new Term(['price' => 4.5]); |
||
| 345 | $query = new FunctionScore(); |
||
| 346 | $query->addRandomScoreFunction(2, $filter, 2, '_id'); |
||
| 347 | |||
| 348 | $expected = [ |
||
| 349 | 'function_score' => [ |
||
| 350 | 'functions' => [ |
||
| 351 | [ |
||
| 352 | 'random_score' => [ |
||
| 353 | 'seed' => 2, |
||
| 354 | 'field' => '_id', |
||
| 355 | ], |
||
| 356 | 'filter' => [ |
||
| 357 | 'term' => [ |
||
| 358 | 'price' => 4.5, |
||
| 359 | ], |
||
| 360 | ], |
||
| 361 | 'weight' => 2, |
||
| 362 | ], |
||
| 363 | ], |
||
| 364 | ], |
||
| 365 | ]; |
||
| 366 | |||
| 367 | $this->assertEquals($expected, $query->toArray()); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @group unit |
||
| 372 | */ |
||
| 373 | View Code Duplication | public function testRandomScoreWeightWithLegacyFilter(): void |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @group functional |
||
| 403 | */ |
||
| 404 | public function testRandomScoreWithoutSeed(): void |
||
| 405 | { |
||
| 406 | $query = new FunctionScore(); |
||
| 407 | $query->setRandomScore(); |
||
| 408 | |||
| 409 | $response = $this->_getIndexForTest()->search($query); |
||
| 410 | |||
| 411 | $this->assertEquals(2, $response->count()); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @group functional |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function testRandomScoreWithoutField(): void |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @group functional |
||
| 453 | */ |
||
| 454 | public function testScriptScore(): void |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @group functional |
||
| 487 | */ |
||
| 488 | public function testSetMinScore(): void |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @group functional |
||
| 524 | */ |
||
| 525 | public function testFieldValueFactor(): void |
||
| 555 | |||
| 556 | protected function _getIndexForTest() |
||
| 585 | } |
||
| 586 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: