Skip to content

Commit

Permalink
[7/7] 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.

The change removes usage in the last remaining
files. It also removes 2 tests for the function
that were converted instead of removed.

TESTS=Tested site/author searching and some
project updates.
  • Loading branch information
jchaffraix authored and cpeel committed Dec 9, 2024
1 parent 349b6e1 commit 29ce7a0
Show file tree
Hide file tree
Showing 27 changed files with 60 additions and 75 deletions.
14 changes: 0 additions & 14 deletions SETUP/tests/unittests/MiscUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@

class MiscUtilsTest extends PHPUnit\Framework\TestCase
{
public function test_array_get_exists()
{
$arr = ["param" => "value"];
$value = $arr["param"] ?? "default";
$this->assertEquals($arr["param"], $value);
}

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

public function test_get_changed_fields_for_objects()
{
$obj1 = new stdClass();
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
2 changes: 1 addition & 1 deletion pinc/DPage.inc
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ function get_normalize_page_text_changes(string $text, string $projectid): array

$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 @@ -754,7 +754,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 @@ -556,7 +556,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 @@ -585,7 +585,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_lengths[$column]) . '|';
}

$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 @@ -346,7 +346,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
5 changes: 2 additions & 3 deletions pinc/misc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use voku\helper\UTF8;
*/
function array_get($arr, $key, $default)
{
// TODO(jchaffraix): Enable once we have no users in the codebase.
// trigger_error("array_get() is deprecated, use the null coalescing operator instead", E_USER_DEPRECATED);
trigger_error("array_get() is deprecated, use the null coalescing operator instead", E_USER_DEPRECATED);
if (isset($arr[$key])) {
return $arr[$key];
} else {
Expand Down Expand Up @@ -61,7 +60,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
6 changes: 3 additions & 3 deletions pinc/upload_file.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ define("RESUMABLE_UPLOAD_SIZE", 1024 * 1024 * 1024); // 1GB
function detect_too_large()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) &&
empty($_FILES) && array_get($_SERVER, 'CONTENT_LENGTH', 0) > 0) {
empty($_FILES) && ($_SERVER['CONTENT_LENGTH'] ?? 0) > 0) {
throw new LengthException(sprintf(_("Uploaded file is too large. Maximum file size is %s."), humanize_bytes(get_max_upload_size())));
}
}
Expand Down Expand Up @@ -191,8 +191,8 @@ function validate_uploaded_file($verbose)
} else {
// resumable upload
$root_staging_dir = "/tmp/resumable_uploads";
$original_name = array_get($_POST, "resumable_filename", "");
$identifier = array_get($_POST, "resumable_identifier", "");
$original_name = $_POST["resumable_filename"] ?? "";
$identifier = $_POST["resumable_identifier"] ?? "";
$hashed_filename = md5($identifier);
$file_path = "$root_staging_dir/$hashed_filename";
if (!file_exists($file_path)) {
Expand Down
12 changes: 6 additions & 6 deletions pinc/wordcheck_engine.inc
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ function get_bad_words_for_text($text, $languages, $word_lists = [])

// The site
{
$acc->remove_good_words(array_get($word_lists, "site_good", []));
$acc->add_bad_words(array_get($word_lists, "site_bad", []), WC_SITE);
$acc->remove_good_words($word_lists["site_good"] ?? []);
$acc->add_bad_words($word_lists["site_bad"] ?? [], WC_SITE);

$acc->add_bad_words(
get_bad_words_via_pattern($input_words_w_freq, $languages),
Expand All @@ -234,14 +234,14 @@ function get_bad_words_for_text($text, $languages, $word_lists = [])

// The project
{
$acc->remove_good_words(array_get($word_lists, "project_good", []));
$acc->remove_good_words($word_lists["project_good"] ?? []);

$acc->add_bad_words(array_get($word_lists, "project_bad", []), WC_PROJECT);
$acc->add_bad_words($word_lists["project_bad"] ?? [], WC_PROJECT);
}

// The page
{
$acc->remove_good_words(array_get($word_lists, "adhoc_good", []));
$acc->remove_good_words($word_lists["adhoc_good"] ?? []);
}

return [$input_words_w_freq, $acc->words, $acc->messages];
Expand Down Expand Up @@ -480,7 +480,7 @@ function get_bad_words_via_pattern($input_words_w_freq, $languages)
];
$exceptions = [];
foreach ($langcode3s as $langcode3) {
$exceptions[] = array_get($exceptions_for_lang, $langcode3, "");
$exceptions[] = $exceptions_for_lang[$langcode3] ?? "";
}
$exceptions = join('|', $exceptions);
$exceptions_pattern = "/^($exceptions)$/u";
Expand Down
6 changes: 3 additions & 3 deletions tools/authors/search.inc
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ function sql_like_encode(string $str): string
function echo_search_form(): void
{
global $last_name, $other_names;
$view = array_get($_REQUEST, 'view', null);
$orderby = array_get($_REQUEST, 'orderby', null);
$desc = array_get($_REQUEST, 'desc', null);
$view = $_REQUEST['view'] ?? null;
$orderby = $_REQUEST['orderby'] ?? null;
$desc = $_REQUEST['desc'] ?? null;
echo "<h2>" . _("Search") . "</h2>";
echo "<p>" . _('Case insensitive.') . " ";
echo sprintf(
Expand Down
2 changes: 1 addition & 1 deletion tools/charsuites.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

require_login();

$charsuite_name = array_get($_GET, "charsuite", null);
$charsuite_name = $_GET["charsuite"] ?? null;
$projectid = get_projectID_param($_REQUEST, "projectid", true);

$charsuite = null;
Expand Down
4 changes: 2 additions & 2 deletions tools/modify_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

require_login();

$subject_username = array_get($_POST, 'subject_username', null);
$notify_user = array_get($_POST, 'notify_user', null);
$subject_username = $_POST['subject_username'] ?? null;
$notify_user = $_POST['notify_user'] ?? null;

$user = new User($subject_username);

Expand Down
Loading

0 comments on commit 29ce7a0

Please sign in to comment.