Skip to content

Commit

Permalink
MDL-79890 core: Add polyfill for array_keys with filter
Browse files Browse the repository at this point in the history
This is a Moodle version of a proposed function `array_keys_filter`.

Since this method has not yet been created in upstream PHP, I have
elected to name it with a Moodle prefix as the signature is not known.

In the future, if and when this method is created, we can replace its
content with new method and deprecate it.
  • Loading branch information
andrewnicols committed Nov 15, 2023
1 parent e734858 commit 0efbc79
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/moodlelib.php
Expand Up @@ -11037,3 +11037,29 @@ function site_is_public() {
function exceeds_password_length(string $password, int $pepperlength = 0): bool {
return (strlen($password) > (MAX_PASSWORD_CHARACTERS + $pepperlength));
}

/**
* A helper to replace PHP 8.3 usage of array_keys with two args.
*
* There is an indication that this will become a new method in PHP 8.4, but that has not happened yet.
* Therefore this non-polyfill has been created with a different naming convention.
* In the future it can be deprecated if a core PHP method is created.
*
* https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#array_keys
*
* @param array $array
* @param mixed $filter The value to filter on
* @return array
*/
function moodle_array_keys_filter(array $array, mixed $filter, bool $strict): array {
return array_keys(array_filter(
$array,
function($value, $key) use ($filter, $strict): bool {
if ($strict) {
return $value === $filter;
}
return $value == $filter;
},
ARRAY_FILTER_USE_BOTH,
));
}
83 changes: 83 additions & 0 deletions lib/tests/moodlelib_test.php
Expand Up @@ -5445,4 +5445,87 @@ public function test_is_proxybypass(string $url, string $proxybypass, bool $expe
$this->assertEquals($expected, is_proxybypass($url));
}

/**
* Test that the moodle_array_keys_filter method behaves in the same way
* that array_keys behaved before Moodle 8.3.
*
* @dataProvider moodle_array_keys_filter_provider
* @param array $array
* @param mixed $filter
* @param bool $strict
* @param array $expected
* @covers ::moodle_array_keys_filter
*/
public function test_moodle_array_keys_filter(
array $array,
mixed $filter,
bool $strict,
array $expected,
): void {
$this->assertSame(
$expected,
moodle_array_keys_filter($array, $filter, $strict),
);
}

/**
* Data provider for moodle_array_keys_filter tests.
*
* @return array
*/
public static function moodle_array_keys_filter_provider(): array {
return [
[['a', 'b', 'c'], 'b', false, [1]],
[
[
'alpha' => 'a',
'bravo' => 'b',
'charlie' => 'c',
],
'b',
false,
['bravo'],
],
[
[
'zero' => 0,
'one' => 1,
'true' => true,
],
'1',
false,
['one', 'true'],
],
[
[
'zero' => 0,
'one' => 1,
'true' => true,
],
true,
false,
['one', 'true'],
],
[
[
'zero' => 0,
'one' => 1,
'true' => true,
],
true,
true,
['true'],
],
[
[
'zero' => 0,
'one' => 1,
'true' => true,
],
1,
true,
['one'],
],
];
}
}

0 comments on commit 0efbc79

Please sign in to comment.