From ccf119206d56a024f21cde36c729e4b7da4283d1 Mon Sep 17 00:00:00 2001 From: Marc Tobias Date: Tue, 10 May 2022 18:30:49 +0200 Subject: [PATCH] PHP 8 behaves slightly different with in_array and usort --- lib-php/ParameterParser.php | 2 +- lib-php/SimpleWordList.php | 7 ++++++- test/php/Nominatim/ParameterParserTest.php | 5 ++++- test/php/Nominatim/SimpleWordListTest.php | 18 +++++++++++++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib-php/ParameterParser.php b/lib-php/ParameterParser.php index 98b95388..070be36c 100644 --- a/lib-php/ParameterParser.php +++ b/lib-php/ParameterParser.php @@ -70,7 +70,7 @@ class ParameterParser return $sDefault; } - if (!in_array($this->aParams[$sName], $aValues)) { + if (!in_array($this->aParams[$sName], $aValues, true)) { userError("Parameter '$sName' must be one of: ".join(', ', $aValues)); } diff --git a/lib-php/SimpleWordList.php b/lib-php/SimpleWordList.php index ecd02153..7009d370 100644 --- a/lib-php/SimpleWordList.php +++ b/lib-php/SimpleWordList.php @@ -120,13 +120,18 @@ class SimpleWordList return array_slice($aWordSets, 0, SimpleWordList::MAX_WORDSETS); } + /** + * Custom search routine which takes two arrays. The array with the fewest + * items wins. If same number of items then the one with the longest first + * element wins. + */ public static function cmpByArraylen($aA, $aB) { $iALen = count($aA); $iBLen = count($aB); if ($iALen == $iBLen) { - return 0; + return strlen($aB[0]) <=> strlen($aA[0]); } return ($iALen < $iBLen) ? -1 : 1; diff --git a/test/php/Nominatim/ParameterParserTest.php b/test/php/Nominatim/ParameterParserTest.php index 1488c987..7381bdf8 100644 --- a/test/php/Nominatim/ParameterParserTest.php +++ b/test/php/Nominatim/ParameterParserTest.php @@ -137,6 +137,9 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase public function testGetSet() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Parameter 'val3' must be one of: foo, bar"); + $oParams = new ParameterParser(array( 'val1' => 'foo', 'val2' => '', @@ -148,7 +151,7 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase $this->assertSame('foo', $oParams->getSet('val1', array('foo', 'bar'))); $this->assertSame(false, $oParams->getSet('val2', array('foo', 'bar'))); - $this->assertSame(0, $oParams->getSet('val3', array('foo', 'bar'))); + $oParams->getSet('val3', array('foo', 'bar')); } diff --git a/test/php/Nominatim/SimpleWordListTest.php b/test/php/Nominatim/SimpleWordListTest.php index fe543b3f..69cb5180 100644 --- a/test/php/Nominatim/SimpleWordListTest.php +++ b/test/php/Nominatim/SimpleWordListTest.php @@ -77,7 +77,7 @@ class SimpleWordListTest extends \PHPUnit\Framework\TestCase $oList = new SimpleWordList('a b c'); $this->assertEquals( - '(a b c),(a|b c),(a b|c),(a|b|c)', + '(a b c),(a b|c),(a|b c),(a|b|c)', $this->serializeSets($oList->getWordSets(new TokensFullSet())) ); @@ -88,6 +88,22 @@ class SimpleWordListTest extends \PHPUnit\Framework\TestCase ); } + public function testCmpByArraylen() + { + // Array elements are phrases, we want to sort so longest phrases are first + $aList1 = array('hackney', 'bridge', 'london', 'england'); + $aList2 = array('hackney', 'london', 'bridge'); + $aList3 = array('bridge', 'hackney', 'london', 'england'); + + $this->assertEquals(0, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList1)); + + // list2 "wins". Less array elements + $this->assertEquals(1, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList2)); + $this->assertEquals(-1, \Nominatim\SimpleWordList::cmpByArraylen($aList2, $aList3)); + + // list1 "wins". Same number of array elements but longer first element + $this->assertEquals(-1, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList3)); + } public function testMaxWordSets() { -- 2.45.2