Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8 behaves slightly different with in_array and usort #2706

Merged
merged 1 commit into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib-php/ParameterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getSet($sName, $aValues, $sDefault = false)
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));
}

Expand Down
7 changes: 6 additions & 1 deletion lib-php/SimpleWordList.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,18 @@ public function getWordSets($oTokens)
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;
Expand Down
5 changes: 4 additions & 1 deletion test/php/Nominatim/ParameterParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public function testGetString()

public function testGetSet()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Parameter 'val3' must be one of: foo, bar");

$oParams = new ParameterParser(array(
'val1' => 'foo',
'val2' => '',
Expand All @@ -148,7 +151,7 @@ public function testGetSet()
$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'));
}


Expand Down
18 changes: 17 additions & 1 deletion test/php/Nominatim/SimpleWordListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testMultiWordPhrase()

$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()))
);

Expand All @@ -88,6 +88,22 @@ public function testMultiWordPhrase()
);
}

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()
{
Expand Down