Skip to content

Commit 56ff7ab

Browse files
committed
refactor: use native str_starts_with
Available from PHP 8. The future is now.
1 parent d738bf6 commit 56ff7ab

File tree

4 files changed

+3
-19
lines changed

4 files changed

+3
-19
lines changed

classes/form/context/render_context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ abstract public function hide_if(string $dependant, string $dependency, string $
146146
* @return string name of the element qualified by this context's prefix
147147
*/
148148
public function mangle_name(string $name): string {
149-
if (utils::str_starts_with($name, $this->prefix)) {
149+
if (str_starts_with($name, $this->prefix)) {
150150
// Already mangled, perhaps by an array_render_context.
151151
return $name;
152152
}

classes/question_ui_renderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private function mangle_ids_and_names(): void {
249249
") as $attr
250250
) {
251251
$original = $attr->value;
252-
if ($attr->name === "usemap" && utils::str_starts_with($original, "#")) {
252+
if ($attr->name === "usemap" && str_starts_with($original, "#")) {
253253
// See https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/useMap.
254254
$attr->value = "#" . $this->attempt->get_qt_field_name(substr($original, 1));
255255
} else {

classes/utils.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ public static function &ensure_exists(array &$array, string $key): array {
4242
return $array[$key];
4343
}
4444

45-
/**
46-
* Determines whether a string starts with another.
47-
*
48-
* @param string $haystack
49-
* @param string $needle
50-
* @return bool
51-
*/
52-
public static function str_starts_with(string $haystack, string $needle): bool {
53-
// From https://stackoverflow.com/a/10473026.
54-
return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;
55-
}
56-
5745
/**
5846
* Given an array and a key such as `abc[def]`, returns `$array["abc"]["def"]`.
5947
*

question.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,7 @@ public function apply_attempt_state(question_attempt_step $step) {
160160
$this->scoringstate = $this->get_behaviour()->get_qa()->get_last_qt_var(constants::QT_VAR_SCORING_STATE);
161161

162162
$lastqtdata = $this->get_behaviour()->get_qa()->get_last_qt_data(null);
163-
$lastresponse = $lastqtdata === null ? null : array_filter(
164-
$lastqtdata,
165-
fn($key) => !utils::str_starts_with($key, "_"),
166-
ARRAY_FILTER_USE_KEY
167-
);
163+
$lastresponse = $lastqtdata === null ? null : utils::filter_for_response($lastqtdata);
168164

169165
/* TODO: This method is also called from question_attempt->regrade and
170166
question_attempt->start_question_based_on, where we shouldn't need to get the UI. */

0 commit comments

Comments
 (0)