WebRequest: Fix PHP 8.1 deprecations in getFuzzyBool()

WebRequest::getFuzzyBool() will emit a deprecation warning on PHP 8.1 or
newer if the parameter to be fetched is absent and the $default value is
set to `true`, because strcasecmp() no longer accepts nulls. Fix it by
returning out if the parameter is wholly absent and add a test for this
scenario.

Bug: T351088
Change-Id: I85bbfec6aabef4e85859a76b6e50c80781024ae5
This commit is contained in:
Máté Szabó 2023-11-13 15:08:23 +01:00
parent d674367af4
commit ddbd5735a0
2 changed files with 11 additions and 2 deletions

View file

@ -668,8 +668,12 @@ class WebRequest {
* @return bool
*/
public function getFuzzyBool( $name, $default = false ): bool {
return $this->getBool( $name, $default )
&& strcasecmp( $this->getRawVal( $name ), 'false' ) !== 0;
$value = $this->getRawVal( $name );
if ( $value === null ) {
return (bool)$default;
}
return $value && strcasecmp( $value, 'false' ) !== 0;
}
/**

View file

@ -297,6 +297,11 @@ class WebRequestTest extends MediaWikiIntegrationTestCase {
$this->assertFalse( $req->getFuzzyBool( 'z' ), 'Not found' );
}
public function testGetFuzzyBoolDefaultTrue() {
$req = $this->mockWebRequest();
$this->assertTrue( $req->getFuzzyBool( 'z', true ), 'Not found, default true' );
}
public function testGetCheck() {
$req = $this->mockWebRequest( [ 'x' => 'Value', 'zero' => '0' ] );
$this->assertFalse( $req->getCheck( 'z' ), 'Not found' );