From 9b250f51fbd26c7e366e5001eb6006395d7cd87a Mon Sep 17 00:00:00 2001 From: Julien Chaffraix Date: Thu, 12 Dec 2024 14:51:20 -0800 Subject: [PATCH 1/2] Type pinc/ProjectSearch* Typing all those files together made sense as they are all helping the search feature. TESTS=Run searches by different fields. --- pinc/ProjectSearchForm.inc | 44 +++++++++-------- pinc/ProjectSearchResults.inc | 74 +++++++++++++++-------------- pinc/ProjectSearchResultsConfig.inc | 26 +++++----- 3 files changed, 76 insertions(+), 68 deletions(-) diff --git a/pinc/ProjectSearchForm.inc b/pinc/ProjectSearchForm.inc index d612ed5f7..c28ce5932 100644 --- a/pinc/ProjectSearchForm.inc +++ b/pinc/ProjectSearchForm.inc @@ -16,7 +16,7 @@ class ProjectSearchWidget private int $size; private array $options; - public function __construct($properties) + public function __construct(array $properties) { foreach ($properties as $property => $value) { $this->$property = $value; @@ -76,7 +76,7 @@ class ProjectSearchWidget } } - public function echo_search_item() + public function echo_search_item(): void { // could fix width of heading cells so they line up when 2nd table is below echo " @@ -86,11 +86,11 @@ class ProjectSearchWidget "; } - public function get_sql_contribution() + public function get_sql_contribution(): string { $value = $_GET[$this->id] ?? ''; if ($value == '') { - return null; + return ''; } [$column_name, $comparator] = $this->q_contrib; if (@$this->can_be_multiple) { @@ -102,7 +102,7 @@ class ProjectSearchWidget // If $value isn't an array, someone is mucking with // the URL -- return instead of erroring out below. if (!is_array($values)) { - return null; + return ''; } // If the user picks the 'any' option as well as some others, // remove the any option. @@ -110,7 +110,7 @@ class ProjectSearchWidget unset($values[$key]); } if (empty($values)) { - return null; + return ''; } } @@ -118,7 +118,7 @@ class ProjectSearchWidget } else { // if $value is an array, someone is mucking with URL if (is_array($value)) { - return null; + return ''; } $value = DPDatabase::escape($value); @@ -132,7 +132,7 @@ class ProjectSearchWidget return $contribution; } - public function sql_from_multi($values, $column_name, $comparator) + public function sql_from_multi(array $values, string $column_name, string $comparator): ?string { // if any $value is an array, someone is mucking with the URL foreach ($values as $value) { @@ -157,7 +157,7 @@ class ProjectSearchWidget class SpecialDayWidget extends ProjectSearchWidget { - public function sql_from_multi($values, $column_name, $comparator) + public function sql_from_multi(array $values, string $column_name, string $comparator): ?string { // if any $value is an array, someone is mucking with the URL foreach ($values as $value) { @@ -189,7 +189,7 @@ class HoldWidget extends ProjectSearchWidget return ""; } - public function get_sql_contribution() + public function get_sql_contribution(): string { if (isset($_GET[$this->id])) { // can only be 'on' return 'project_holds.projectid IS NOT NULL'; @@ -208,7 +208,8 @@ class ProjectSearchForm $this->define_form_widgets(); } - public function all_special_day_options() + /** @return array */ + public function all_special_day_options(): array { return [ '' => _('Any day'), @@ -218,7 +219,8 @@ class ProjectSearchForm ] + $this->special_day_options(); } - public static function special_day_options() + /** @return array */ + public static function special_day_options(): array { $special_days = load_special_days(); sort_special_days($special_days, "open_month,open_day"); @@ -228,7 +230,8 @@ class ProjectSearchForm ); } - public static function language_options() + /** @return array */ + public static function language_options(): array { $lang_options[''] = _('Any'); @@ -249,17 +252,20 @@ class ProjectSearchForm return $lang_options; } - public static function genre_options() + /** @return array */ + public static function genre_options(): array { return array_merge(['' => _('Any')], load_genre_translation_array()); } - public static function difficulty_options() + /** @return array */ + public static function difficulty_options(): array { return array_merge(['' => _('Any')], get_project_difficulties()); } - public static function state_options() + /** @return array */ + public static function state_options(): array { $state_options[''] = _('Any state'); foreach (ProjectStates::get_states() as $proj_state) { @@ -268,7 +274,7 @@ class ProjectSearchForm return $state_options; } - public function define_form_widgets() + public function define_form_widgets(): void { $this->widgets = [ new ProjectSearchWidget([ @@ -374,7 +380,7 @@ class ProjectSearchForm ]; } - public function render() + public function render(): void { echo "

" . _("Search for projects matching the following criteria:")."

\n"; echo "
@@ -416,7 +422,7 @@ class ProjectSearchForm "; } - public function get_condition() + public function get_condition(): string { $condition = '1'; foreach ($this->widgets as $widget) { diff --git a/pinc/ProjectSearchResults.inc b/pinc/ProjectSearchResults.inc index 2dada7242..3eac9d52f 100644 --- a/pinc/ProjectSearchResults.inc +++ b/pinc/ProjectSearchResults.inc @@ -20,7 +20,7 @@ class Column private Settings $userSettings; private string $search_origin; - public function __construct($id, $label, $long_label, $css_class, $default_display, $sortable, $db_column) + public function __construct(string $id, string $label, string $long_label, string $css_class, bool $default_display, bool $sortable, string $db_column) { $this->id = $id; $this->label = $label; @@ -31,17 +31,17 @@ class Column $this->db_column = $db_column; } - public function get_label() + public function get_label(): string { return $this->label; } - public function get_warning() + public function get_warning(): string { return ''; } - public function get_long_label() + public function get_long_label(): string { // can also be overridden if (empty($this->long_label)) { @@ -50,7 +50,7 @@ class Column return $this->long_label; } - public function get_tooltip() + public function get_tooltip(): string { return $this->get_long_label(); } @@ -60,13 +60,13 @@ class Column return $this->db_column; // overridden by state column } - public function set_datastore($userSettings, $search_origin) + public function set_datastore(Settings $userSettings, string $search_origin): void { $this->userSettings = $userSettings; $this->search_origin = $search_origin; } - public function set_active($setting) + public function set_active(bool $setting): void { // We can't use set_boolean() because this actually has three states, not two: // * true @@ -81,7 +81,7 @@ class Column $this->userSettings->set_value("search:{$this->search_origin}.column:$this->id", $value); } - public function is_active() + public function is_active(): bool { // See comment in set_active() for why we can't use get/set_boolean() $value = $this->userSettings->get_value("search:{$this->search_origin}.column:$this->id"); @@ -92,7 +92,7 @@ class Column return $value === 'yes'; } - public function echo_header_cell($sort_column_id, $sql_sort_dir, $sec_sort) + public function echo_header_cell(string $sort_column_id, string $sql_sort_dir, string $sec_sort): void { $label = $this->get_label(); if ($this->sortable) { @@ -126,14 +126,14 @@ class Column echo "$label\n"; } - public function echo_data_cell($project) + public function echo_data_cell(ProjectSearchResultEntry $project): void { echo ""; $this->print_cell_data($project); echo "\n"; } - public function print_cell_data($project) + public function print_cell_data(ProjectSearchResultEntry $project): void { echo html_safe($project->{$this->db_column}); } @@ -143,13 +143,13 @@ class TimeColumn extends Column { private string $time_format; - public function __construct($id, $label, $long_label, $css_class, $default_display, $sortable, $db_column, $time_format) + public function __construct(string $id, string $label, string $long_label, string $css_class, bool $default_display, bool $sortable, string $db_column, string $time_format) { parent::__construct($id, $label, $long_label, $css_class, $default_display, $sortable, $db_column); $this->time_format = $time_format; } - public function get_label() + public function get_label(): string { $label = $this->label; if ($this->time_format == 'days') { @@ -159,7 +159,7 @@ class TimeColumn extends Column return $label; } - public function print_cell_data($project) + public function print_cell_data(ProjectSearchResultEntry $project): void { $data = $project->{$this->db_column}; if (0 == $data) { @@ -179,7 +179,7 @@ class TimeColumn extends Column class PGColumn extends Column { - public function print_cell_data($project) + public function print_cell_data(ProjectSearchResultEntry $project): void { $postednum = $project->postednum; echo "$postednum"; @@ -188,7 +188,7 @@ class PGColumn extends Column class TitleColumn extends Column { - public function print_cell_data($project) + public function print_cell_data(ProjectSearchResultEntry $project): void { global $code_url; echo "" . html_safe($project->nameofwork) . ""; @@ -197,7 +197,7 @@ class TitleColumn extends Column class LinkColumn extends Column { - public function print_cell_data($project) + public function print_cell_data(ProjectSearchResultEntry $project): void { $name = $project->{$this->db_column}; if ($name != '') { @@ -209,12 +209,12 @@ class LinkColumn extends Column class GenreColumn extends Column { - public function get_long_label() + public function get_long_label(): string { return _('Difficulty') . " + " . $this->label; } - public function print_cell_data($project) + public function print_cell_data(ProjectSearchResultEntry $project): void { $diff_prefix = [ "beginner" => _("BEGINNERS"), @@ -233,12 +233,12 @@ class GenreColumn extends Column class StateChangeColumn extends TimeColumn { - public function get_warning() + public function get_warning(): string { return _("The 'Last State Change' column tracks forward progress through the proofreading and formatting rounds well, but does not reflect Post-processing state changes or state changes to Unavailable accurately."); } - public function get_tooltip() + public function get_tooltip(): string { return _("Last State Change (see note above)"); } @@ -251,7 +251,7 @@ class StateColumn extends Column return sql_collator_for_project_state('state'); } - public function print_cell_data($project) + public function print_cell_data(ProjectSearchResultEntry $project): void { global $pguser, $code_url; @@ -280,13 +280,13 @@ class StateColumn extends Column onchange='this.form.submit()'> "; - $this->_echo_project_state_option($project->state, 1); + $this->_echo_project_state_option($project->state, true); foreach ($transitions as $transition) { $disabled = $transition->is_disabled($project); $optional_reason = $transition->why_disabled($project); - $this->_echo_project_state_option($transition->to_state, 0, $disabled, $optional_reason); + $this->_echo_project_state_option($transition->to_state, false, $disabled, $optional_reason); } echo " @@ -298,7 +298,7 @@ class StateColumn extends Column } } - private function _echo_project_state_option($project_state, $selected, $disabled = 0, $reason = '') + private function _echo_project_state_option(string $project_state, bool $selected, bool $disabled = false, $reason = ''): void { echo "