Skip to content

Commit

Permalink
refactor: use native str_starts_with
Browse files Browse the repository at this point in the history
Available from PHP 8. The future is now.
  • Loading branch information
MHajoha committed Nov 18, 2024
1 parent d738bf6 commit 56ff7ab
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 19 deletions.
2 changes: 1 addition & 1 deletion classes/form/context/render_context.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ abstract public function hide_if(string $dependant, string $dependency, string $
* @return string name of the element qualified by this context's prefix
*/
public function mangle_name(string $name): string {
if (utils::str_starts_with($name, $this->prefix)) {
if (str_starts_with($name, $this->prefix)) {
// Already mangled, perhaps by an array_render_context.
return $name;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/question_ui_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private function mangle_ids_and_names(): void {
") as $attr
) {
$original = $attr->value;
if ($attr->name === "usemap" && utils::str_starts_with($original, "#")) {
if ($attr->name === "usemap" && str_starts_with($original, "#")) {
// See https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/useMap.
$attr->value = "#" . $this->attempt->get_qt_field_name(substr($original, 1));
} else {
Expand Down
12 changes: 0 additions & 12 deletions classes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ public static function &ensure_exists(array &$array, string $key): array {
return $array[$key];
}

/**
* Determines whether a string starts with another.
*
* @param string $haystack
* @param string $needle
* @return bool
*/
public static function str_starts_with(string $haystack, string $needle): bool {
// From https://stackoverflow.com/a/10473026.
return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;
}

/**
* Given an array and a key such as `abc[def]`, returns `$array["abc"]["def"]`.
*
Expand Down
6 changes: 1 addition & 5 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ public function apply_attempt_state(question_attempt_step $step) {
$this->scoringstate = $this->get_behaviour()->get_qa()->get_last_qt_var(constants::QT_VAR_SCORING_STATE);

$lastqtdata = $this->get_behaviour()->get_qa()->get_last_qt_data(null);
$lastresponse = $lastqtdata === null ? null : array_filter(
$lastqtdata,
fn($key) => !utils::str_starts_with($key, "_"),
ARRAY_FILTER_USE_KEY
);
$lastresponse = $lastqtdata === null ? null : utils::filter_for_response($lastqtdata);

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

0 comments on commit 56ff7ab

Please sign in to comment.