Skip to content

Commit

Permalink
Remove array_get
Browse files Browse the repository at this point in the history
The function cannot be properly typed as it
is and fixing this would take a while, removing
it is shorter and better.
  • Loading branch information
jchaffraix committed Oct 12, 2024
1 parent e725f76 commit 0fea8ce
Show file tree
Hide file tree
Showing 79 changed files with 187 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

require_login();

$L_input = array_get($_POST, "Ltex", "");
$R_input = array_get($_POST, "Rtex", "");
$L_input = $_POST["Ltex"] ?? "";
$R_input = $_POST["Rtex"] ?? "";
$unwrap = isset($_POST['Unwrap']); // can only be 'on'
$ignore_case = isset($_POST['IgnoreCase']);

Expand Down
4 changes: 2 additions & 2 deletions SETUP/tests/unittests/MiscUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class MiscUtilsTest extends PHPUnit\Framework\TestCase
public function test_array_get_exists()
{
$arr = ["param" => "value"];
$value = array_get($arr, "param", "default");
$value = $arr["param"] ?? "default";
$this->assertEquals($arr["param"], $value);
}

public function test_array_get_doesnt_exists()
{
$arr = [];
$value = array_get($arr, "param", "default");
$value = $arr["param"] ?? "default";
$this->assertEquals("default", $value);
}

Expand Down
4 changes: 2 additions & 2 deletions SETUP/upgrade/20/20240205_alter_queue_defns.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@

$targets_array = convert($release_criterion, $queue_ident);

$setters = 'projects_target = ' . array_get($targets_array, 'projects', 0)
. ', pages_target = ' . array_get($targets_array, 'pages', 0);
$setters = 'projects_target = ' . ($targets_array['projects'] ?? 0)
. ', pages_target = ' . ($targets_array['pages'] ?? 0);
$sql = "UPDATE queue_defns SET $setters WHERE id=$id\n";
// echo "$sql\n";
$res = mysqli_query(DPDatabase::get_connection(), $sql) or die("At $queue_ident, " . mysqli_error(DPDatabase::get_connection()));
Expand Down
2 changes: 1 addition & 1 deletion accounts/activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// The current param value is "reg_token" but the prior value was "id"
// so we support both here.
$reg_token = array_get($_GET, "reg_token", array_get($_GET, "id", ""));
$reg_token = $_GET["reg_token"] ?? ($_GET["id"] ?? "");

// If the user is already logged in, redirect them to the Activity Hub.
if (User::current_username()) {
Expand Down
18 changes: 9 additions & 9 deletions accounts/addproofer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
include_once($relPath.'theme.inc');
include_once($relPath.'User.inc');

$real_name = array_get($_POST, 'real_name', '');
$username = array_get($_POST, 'userNM', '');
$userpass = array_get($_POST, 'userPW', '');
$userpass2 = array_get($_POST, 'userPW2', '');
$email = array_get($_POST, 'email', '');
$email2 = array_get($_POST, 'email2', '');
$real_name = $_POST['real_name'] ?? '';
$username = $_POST['userNM'] ?? '';
$userpass = $_POST['userPW'] ?? '';
$userpass2 = $_POST['userPW2'] ?? '';
$email = $_POST['email'] ?? '';
$email2 = $_POST['email2'] ?? '';
$email_updates = get_bool_param($_POST, 'email_updates', true);
$referrer = array_get($_POST, 'referrer', '');
$referrer_details = array_get($_POST, 'referrer_details', '');
$referrer = $_POST['referrer'] ?? '';
$referrer_details = $_POST['referrer_details'] ?? '';

$form_data_inserters = [];
$form_validators = [
Expand Down Expand Up @@ -64,7 +64,7 @@
$register->email_updates = $email_updates;
$register->referrer = $referrer;
$register->referrer_details = $referrer_details;
$register->http_referrer = array_get($_COOKIE, "http_referer", "");
$register->http_referrer = $_COOKIE["http_referer"] ?? "";
$register->u_intlang = $intlang;
$register->user_password = forum_password_hash($userpass);

Expand Down
6 changes: 3 additions & 3 deletions activity_hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
// show_filtered_numbers userSetting for use the next time they visit the
// page.
$user_filtered_projects_setting = $userSettings->get_boolean("show_filtered_numbers");
$page_filtered_projects_setting = (array_get($_GET, "show_filtered", $user_filtered_projects_setting) == 1);
$page_filtered_projects_setting = ($_GET["show_filtered"] ?? $user_filtered_projects_setting) == 1;
if ($user_filtered_projects_setting != $page_filtered_projects_setting) {
$userSettings->set_boolean("show_filtered_numbers", $page_filtered_projects_setting);
}
Expand Down Expand Up @@ -344,9 +344,9 @@ function summarize_stage($stage, $desired_states, $show_filtered_projects = fals
// (Use '@' to suppress "Undefined property" notice:
// not every stage has a 'project_complete_state'.)
if ($stage_state == @$stage->project_complete_state) {
$count = array_get($n_projects_transitioned_to_state_, $stage_state, 0);
$count = $n_projects_transitioned_to_state_[$stage_state] ?? 0;
} else {
$count = array_get($n_projects_in_state_, $stage_state, 0);
$count = $n_projects_in_state_[$stage_state] ?? 0;
$total_projects += $count;
}

Expand Down
4 changes: 2 additions & 2 deletions api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function api()
api_rate_limit($username);

$query_params = $_GET;
$path = array_get($query_params, "url", "");
$path = $query_params["url"] ?? "";
unset($query_params["url"]);

$router = ApiRouter::get_router();
Expand Down Expand Up @@ -183,7 +183,7 @@ function api_send_pagination_header($query_params, $total_rows, $per_page, $page
// NB We don't use $_SERVER['SCRIPT_URI'] because not all servers set
// it. Most notably, the `php -S` CLI server we use for testing.
// See https://www.php.net/manual/en/reserved.variables.server.php
$proto = !empty(array_get($_SERVER, 'HTTPS', '')) ? 'https://' : 'http://';
$proto = !empty($_SERVER['HTTPS'] ?? '');
$script_uri = $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$link_base = $script_uri . "?";
if (stripos($link_base, $_GET["url"]) === false) {
Expand Down
2 changes: 1 addition & 1 deletion api/v1_projects.inc
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ function api_v1_project_pagedetails($method, $data, $query_params)
{
// optional page round IDs (one or more) to filter down to
$only_rounds = null;
$pageroundids = array_get($query_params, "pageroundid", null);
$pageroundids = $query_params["pageroundid"] ?? null;
if ($pageroundids) {
$only_rounds = [];
if (!is_array($pageroundids)) {
Expand Down
6 changes: 3 additions & 3 deletions api/v1_queues.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include_once("api_common.inc");
// queues -- list queues, optionally filtered by round
function api_v1_queues($method, $data, $query_params)
{
$roundid = array_get($query_params, "roundid", null);
$roundid = $query_params["roundid"] ?? null;
$round = !is_null($roundid) ? validate_round($roundid, null) : null;

$show = get_enumerated_param($query_params, "show", "all", ["enabled", "populated", "all"]);
Expand Down Expand Up @@ -42,8 +42,8 @@ function api_v1_queues($method, $data, $query_params)
$return_fields = array_get_as_array($query_params, "field", $valid_return_fields);
$return_fields = array_intersect($return_fields, $valid_return_fields);

$name_selector = array_get($query_params, "name", null);
$group_selector = array_get($query_params, "group", null);
$name_selector = $query_params["name"] ?? null;
$group_selector = $query_params["group"] ?? null;

$output = [];
foreach (fetch_queues_data($round, $show, false, $name_selector, $group_selector) as $queue_data) {
Expand Down
6 changes: 3 additions & 3 deletions locale/debug_ui_language.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
$user = User::load_current();

$detected_language = get_desired_language();
$url_language = array_get($_GET, 'lang', "<i>not set</i>");
$url_language = $_GET['lang'] ?? "<i>not set</i>";
$pref_language = $user ? $user->u_intlang : "<i>not set</i>";
$user_logged_in = $user ? $user->username : "<i>not logged in</i>";
if ($pref_language == "") {
$pref_language = "<i>browser detect</i>";
}
$cookie_language = array_get($_COOKIE, 'language', "<i>not set</i>");
$http_accept_language = array_get($_SERVER, 'HTTP_ACCEPT_LANGUAGE', "<i>not set </i>");
$cookie_language = $_COOKIE['language'] ?? "<i>not set</i>";
$http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? "<i>not set </i>";
$browser_locale = get_locale_matching_browser_accept_language(@$_SERVER['HTTP_ACCEPT_LANGUAGE']);

$enabled_languages = implode(", ", array_merge(get_installed_locale_translations("all"), ["en_US"]));
Expand Down
2 changes: 1 addition & 1 deletion pinc/DPage.inc
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ function get_normalize_page_text_changes($text, $projectid)

$invalid_chars = get_invalid_characters($text, $project->get_valid_codepoints());
if ($invalid_chars) {
$whitespace_codepoints = array_get(get_utf8_to_ascii_codepoints(), " ", []);
$whitespace_codepoints = get_utf8_to_ascii_codepoints()[" "] ?? [];
$unicode_whitespace_chars = array_map('voku\helper\UTF8::hex_to_chr', $whitespace_codepoints);
$found_whitespace_chars = array_intersect(array_keys($invalid_chars), $unicode_whitespace_chars);
if ($found_whitespace_chars) {
Expand Down
2 changes: 1 addition & 1 deletion pinc/MARCRecord.inc
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class MARCRecord
// Character 6 - Type of record
$leader = $this->record[0][1]; // The leader is always the first record.
$code = substr($leader, 6, 1);
return array_get($this->type_of_record_array, $code, "");
return $this->type_of_record_array[$code] ?? "";
}

public function get_language(): string
Expand Down
2 changes: 1 addition & 1 deletion pinc/Project.inc
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ class Project
}

$image_sources = load_image_sources();
$imso_res = array_get($image_sources, (string)$this->image_source, null);
$imso_res = $image_sources[(string)$this->image_source] ?? null;
if ($imso_res) {
$this->_image_source_name = $imso_res['full_name'];
$this->image_source_credit = $imso_res['credit'];
Expand Down
2 changes: 1 addition & 1 deletion pinc/ProjectSearchForm.inc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ProjectSearchWidget

public function get_sql_contribution()
{
$value = array_get($_GET, $this->id, '');
$value = $_GET[$this->id] ?? '';
if ($value == '') {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions pinc/ProjectSearchResults.inc
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ class ProjectSearchResults
// else default to 'state'
// store for next time
$saved_sort = $this->userSettings->get_value("search:{$this->search_origin}.sort", " ");
$sort_param = array_get($_GET, "sort", $saved_sort);
$sort_param = $_GET["sort"] ?? $saved_sort;

// parse and check if valid
// remove terminating char A or D
Expand Down Expand Up @@ -586,7 +586,7 @@ class ProjectSearchResults

// get secondary sorting
$saved_sec_sort = $this->userSettings->get_value("search:{$this->search_origin}.sort_sec", " ");
$this->sec_sort = array_get($_GET, "sec_sort", $saved_sec_sort);
$this->sec_sort = $_GET["sec_sort"] ?? $saved_sec_sort;
$sec_sort_sql = $this->get_sort_sql($this->sec_sort);
if (null == $sec_sort_sql) {
$this->sec_sort = 'title';
Expand Down
6 changes: 3 additions & 3 deletions pinc/ProjectSearchResultsConfig.inc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ConfigForm extends ColumnData
global $code_url, $pguser;

echo "<h1>$page_title</h1>\n";
$origin = array_get($_GET, 'origin', "$code_url/activity_hub.php");
$origin = $_GET['origin'] ?? "$code_url/activity_hub.php";
echo "<div style='clear: both;'>
<form method='GET'>
<input type='hidden' name='show' value='set_columns'>
Expand Down Expand Up @@ -212,7 +212,7 @@ class ConfigSaver extends ColumnData
}
// get the option values
foreach ($option_data->options as $option) {
$value = array_get($_GET, $option->id, '');
$value = $_GET[$option->id] ?? '';
$option->save_value($value);
}
}
Expand All @@ -237,7 +237,7 @@ function handle_set_cols($show_view, $search_origin)
$userSettings = & Settings::get_Settings($pguser);
$config_saver = new ConfigSaver($userSettings, $search_origin);
$config_saver->store_data();
$origin = array_get($_GET, 'origin', "$code_url/activity_hub.php");
$origin = $_GET['origin'] ?? "$code_url/activity_hub.php";
metarefresh(0, $origin);
}
}
Expand Down
4 changes: 2 additions & 2 deletions pinc/TableDocumentation.inc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class TableDocumentation

foreach ($contents as $row) {
foreach ($row as $column => $value) {
$column_lengths[$column] = max(strlen($value), array_get($column_lengths, $column, strlen($column)));
$column_lengths[$column] = max(strlen($value), $column_lengths[$column] ?? strlen($column));
}
}

Expand All @@ -105,7 +105,7 @@ class TableDocumentation
$output_row = '|';

foreach ($table_columns as $column) {
$output_row .= str_pad(array_get($row, $column, ''), $column_lengths[$column]) . '|';
$output_row .= str_pad($row[$column] ?? '', $column_lenghts[$column]) . '|';

Check failure on line 108 in pinc/TableDocumentation.inc

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $column_lenghts
}

$output_table[] = $output_row;
Expand Down
8 changes: 4 additions & 4 deletions pinc/filter_project_list.inc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ProjectFilterElement
{
echo "<select name='{$this->id}[]' id=$this->id size='4' multiple>";
$options = $this->get_options();
$selected_keys = array_get($this->data, $this->id, []);
$selected_keys = $this->data[$this->id] ?? [];
echo_option("", _('Any'), empty($selected_keys));
foreach ($options as $key => $value) {
$selected = in_array($key, $selected_keys);
Expand Down Expand Up @@ -174,7 +174,7 @@ class DifficultyElement extends ProjectFilterElement
echo "<div class='flex_row'>";
echo "<div class='entry'><b>" . html_safe("$this->label:") . "</b></div>";
echo "<div class='entry'><input type='checkbox' name='difficulty[]' id='diff-all' value=''";
$difficulty = array_get($this->data, "difficulty", []);
$difficulty = $this->data["difficulty"] ?? [];
if (!count($difficulty)) {
echo " checked";
}
Expand Down Expand Up @@ -304,7 +304,7 @@ class ProjectSearchElement

public function get_sql_component()
{
$values = array_get($this->data, $this->id, []);
$values = $this->data[$this->id] ?? [];
if (empty($values)) {
return "";
}
Expand Down Expand Up @@ -372,7 +372,7 @@ function get_project_filter_sql($pguser, $filter_type)
function get_lang_match($data)
{
// set a default if not set
return array_get($data, "lang-match", "primwith");
return $data["lang-match"] ?? "primwith";
}

function save_data($pguser, $filter_type, $data)
Expand Down
2 changes: 1 addition & 1 deletion pinc/forum_interface_json.inc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function get_forum_user_details($username)
$return_data = [];

foreach ($interested_columns as $column) {
$return_data[$column] = array_get($user, $column, null);
$return_data[$column] = $user[$column] ?? null;
}

return $return_data;
Expand Down
2 changes: 1 addition & 1 deletion pinc/forum_interface_phpbb3.inc
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ function get_forum_user_details($username)
}

foreach ($interested_columns as $column) {
$return_data[$column] = array_get($row, "user_$column", array_get($row, $column, null));
$return_data[$column] = $row["user_$column"] ?? ($row[$column] ?? null);
}

// phpBB 3.1 and later put data in phpbb_profile_fields_data with
Expand Down
2 changes: 1 addition & 1 deletion pinc/gradual.inc
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function encourage_highest_round(?string $username, $round_id = null): void
$mute_expire = $user_settings->get_value($mute_setting_key);

// if user has requested muting for this round, set the expire time
if (array_get($_GET, $mute_query_param, "") == $round_target) {
if (($_GET[$mute_query_param] ?? "") == $round_target) {
$mute_expire = time() + 60 * 60 * 24 * $mute_days;
$user_settings->set_value($mute_setting_key, $mute_expire);
}
Expand Down
14 changes: 1 addition & 13 deletions pinc/misc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@

use voku\helper\UTF8;

/**
* Return $arr[$key], or if it's not defined, $default.
*/
function array_get($arr, $key, $default)
{
if (isset($arr[$key])) {
return $arr[$key];
} else {
return $default;
}
}

/**
* Given an array of arrays/objects, return the values of the desired field.
*/
Expand Down Expand Up @@ -50,7 +38,7 @@ function array_extract_field($array, $field)
*/
function array_get_as_array($array, $key, $default)
{
$value = array_get($array, $key, $default);
$value = $array[$key] ?? $default;
if (!is_null($value) && !is_array($value)) {
$value = [$value];
}
Expand Down
2 changes: 1 addition & 1 deletion pinc/post_processing.inc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function get_pp_projects_past_threshold($PPer = null)
}

if ($PPer) {
return array_get($projects_grouped_by_PPer, $PPer, []);
return $projects_grouped_by_PPer[$PPer] ?? [];
} else {
return $projects_grouped_by_PPer;
}
Expand Down
4 changes: 2 additions & 2 deletions pinc/prefs_options.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function get_user_proofreading_font($interface = null)
if ($font_style_i == 1) { // other
$font_style = get_user_proofreading_font_other($interface);
} else {
$font_style = array_get($proofreading_font_faces, $font_style_i, '');
$font_style = $proofreading_font_faces[$font_style_i] ?? '';
}
$font_size = array_get($proofreading_font_sizes, $font_size_i, '');
$font_size = $proofreading_font_sizes[$font_size_i] ?? '';

$full_font_family = get_full_font_families($interface)[$font_style_i];
$font_size_family = $font_size ? $font_size : 'unset';
Expand Down
6 changes: 3 additions & 3 deletions pinc/theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ function html_navbar()
// of the current request, so that after logging in, they are returned
// to (the "logged-in" version of) the page that's currently being built.
//
$destination = array_get($_REQUEST, "destination", $_SERVER["REQUEST_URI"]);
$destination = $_REQUEST["destination"] ?? $_SERVER["REQUEST_URI"];

$login_form .= "<input type='hidden' name='destination' value='" . attr_safe($destination) . "'>";

Expand Down Expand Up @@ -468,8 +468,8 @@ function resolve_headerbar_entries(array $entries): array

foreach ($entries as $entry) {
$text = $entry['text'];
$title = array_get($entry, 'title', '');
$target = array_get($entry, 'target', null);
$title = $entry['title'] ?? '';
$target = $entry['target'] ?? null;

if (!empty($entry['url'])) {
// ensure we have a full url, if not prefix the $code_url
Expand Down
Loading

0 comments on commit 0fea8ce

Please sign in to comment.