Skip to content

Commit

Permalink
Cleaning and refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
Skouat committed Jul 2, 2023
1 parent bbf6181 commit b6d44e3
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 238 deletions.
39 changes: 21 additions & 18 deletions acp/ppde_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function main($id, $mode)
/** @type \phpbb\language\language $language Language object */
$language = $phpbb_container->get('language');

if ($this->in_array_field($mode, 'module_name', $this::$available_mode))
if ($this->in_array_field($mode, 'module_name', self::$available_mode))
{
$this->module_info = $this->array_value($mode, 'module_name', $this::$available_mode);
$this->module_info = $this->array_value($mode, 'module_name', self::$available_mode);

// Load the module language file currently in use
$language->add_lang('acp_' . $mode, 'skouat/ppde');
Expand All @@ -74,16 +74,17 @@ public function main($id, $mode)
}

/**
* Check if value is in array
* Checks if a given value exists in a specific field of an array of items.
*
* @param mixed $needle
* @param mixed $needle_field
* @param array $haystack
* @param mixed $needle The value to search for.
* @param string $needle_field The field name to search in.
* @param array $haystack The array to search in.
*
* @return bool
* @return bool Returns true if the value is found in the specified field of any item in the array,
* otherwise returns false.
* @access private
*/
private function in_array_field($needle, $needle_field, $haystack)
private function in_array_field($needle, $needle_field, $haystack): bool
{
foreach ($haystack as $item)
{
Expand All @@ -97,16 +98,18 @@ private function in_array_field($needle, $needle_field, $haystack)
}

/**
* Return the selected array if value is in array
* Finds and returns the first item in the haystack array that matches the provided needle value in the specified
* needle field.
*
* @param mixed $needle
* @param mixed $needle_field
* @param array $haystack
* @param mixed $needle The value to search for.
* @param string $needle_field The field to search for the needle value in each item of the haystack array.
* @param array $haystack The array to search through.
*
* @return array
* @access private
* The first item in the haystack array that matches the provided needle value in the specified needle field. If
* no match is found, an empty array is returned.
*/
private function array_value($needle, $needle_field, $haystack)
private function array_value($needle, $needle_field, $haystack): array
{
foreach ($haystack as $item)
{
Expand All @@ -120,7 +123,7 @@ private function array_value($needle, $needle_field, $haystack)
}

/**
* Switch to the mode selected
* Switches the mode of the admin controller based on the given parameters.
*
* @param int $id
* @param string $mode
Expand All @@ -130,7 +133,7 @@ private function array_value($needle, $needle_field, $haystack)
* @throws \Exception
* @access private
*/
private function switch_mode($id, $mode, $admin_controller)
private function switch_mode($id, $mode, $admin_controller): void
{
global $phpbb_container;

Expand Down Expand Up @@ -183,7 +186,7 @@ private function switch_mode($id, $mode, $admin_controller)
* @throws \Exception
* @access private
*/
private function do_action($action, $controller)
private function do_action($action, $controller): void
{
global $phpbb_container;

Expand Down Expand Up @@ -221,7 +224,7 @@ private function do_action($action, $controller)
break;
}

// Request confirmation from the user to do the action for selected item
// Request confirmation from the user to perform the action for selected item
confirm_box(false, $language->lang($this->module_info['lang_key_prefix'] . 'CONFIRM_OPERATION'), build_hidden_fields($controller->get_hidden_fields()));

// Clear $action status
Expand Down
89 changes: 53 additions & 36 deletions actions/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public function get_currency_data($iso_code): array
}

/**
* Get default currency symbol
* Retrieves the default currency data.
*
* @param int $id Currency identifier; default: 0
* @param int $id The ID of the currency (optional).
*
* @return array
* @return array The default currency data as an array.
* @access public
*/
public function get_default_currency_data($id = 0): array
Expand All @@ -72,15 +72,15 @@ public function get_default_currency_data($id = 0): array
}

/**
* Format currency value, based on the PHP intl extension.
* If this PHP Extension is not available, we switch on a basic currency formatter.
*
* @param float $value
* @param string $currency_iso_code
* @param string $currency_symbol
* @param bool $on_left
* Formats the given value as currency based on the PHP intl extension, if available.
* Otherwise, a basic currency formatter is used.
*
* @return string
* @param float $value The value to be formatted as currency.
* @param string $currency_iso_code The ISO code of the currency.
* @param string $currency_symbol The symbol of the currency.
* @param bool $on_left Determines whether the currency symbol should be placed on the left (default:
* true).
* @return string The formatted currency string.
* @access public
*/
public function format_currency($value, $currency_iso_code, $currency_symbol, $on_left = true): string
Expand All @@ -90,35 +90,35 @@ public function format_currency($value, $currency_iso_code, $currency_symbol, $o
return $this->locale->numfmt_format_currency($this->locale->numfmt_create(), $value, $currency_iso_code);
}

return $this->currency_on_left($value, $currency_symbol, $on_left);
return $this->legacy_currency_format($value, $currency_symbol, $on_left);
}

/**
* Put the currency on the left or on the right of the amount
* Format a value as a legacy currency string
*
* @param float $value
* @param string $currency_symbol
* @param bool $on_left
* @param string $dec_point
* @param string $thousands_sep
*
* @return string
* @param float $value The value to format as currency
* @param string $currency_symbol The symbol to use as the currency symbol
* @param bool $on_left Optional. Determines whether the currency symbol should be placed on the left or
* right of the formatted value. Default is true (left side).
* @param string $dec_point Optional. The string to use as the decimal separator. Default is '.'.
* @param string $thousands_sep Optional. The string to use as the thousands separator. Default is an empty
* string.
* @return string The formatted value as a currency string
* @access public
*/
public function currency_on_left($value, $currency_symbol, $on_left = true, $dec_point = '.', $thousands_sep = ''): string
public function legacy_currency_format($value, $currency_symbol, $on_left = true, $dec_point = '.', $thousands_sep = ''): string
{
if ($on_left)
{
return $currency_symbol . number_format(round($value, 2), 2, $dec_point, $thousands_sep);
}
$formatted_value = number_format(round($value, 2), 2, $dec_point, $thousands_sep);

return number_format(round($value, 2), 2, $dec_point, $thousands_sep) . $currency_symbol;
return $on_left
? $currency_symbol . $formatted_value
: $formatted_value . $currency_symbol;
}

/**
* Build pull down menu options of available currency
* Builds a currency select menu.
*
* @param int $config_value Currency identifier; default: 0
* @param int $config_value The selected currency value from the configuration (default is 0).
*
* @return void
* @access public
Expand All @@ -131,15 +131,32 @@ public function build_currency_select_menu($config_value = 0): void
// Process each menu item for pull-down
foreach ($currency_items as $currency_item)
{
// Set output block vars for display in the template
$this->template->assign_block_vars('options', [
'CURRENCY_ID' => (int) $currency_item['currency_id'],
'CURRENCY_ISO_CODE' => $currency_item['currency_iso_code'],
'CURRENCY_NAME' => $currency_item['currency_name'],
'CURRENCY_SYMBOL' => $currency_item['currency_symbol'],
'S_CURRENCY_DEFAULT' => (int) $config_value === (int) $currency_item['currency_id'],
]);
$this->assign_currency_to_template($currency_item, $config_value);
}
unset ($currency_items);
}

/**
* Assign currency information to the template.
*
* @param array $currency_item The currency item with the following keys:
* - currency_id: The ID of the currency (integer).
* - currency_iso_code: The ISO code of the currency (string).
* - currency_name: The name of the currency (string).
* - currency_symbol: The symbol of the currency (string).
* @param int $config_value The configuration value used to determine the default currency (integer).
*
* @return void
* @access private
*/
private function assign_currency_to_template(array $currency_item, int $config_value): void
{
$this->template->assign_block_vars('options', [
'CURRENCY_ID' => (int) $currency_item['currency_id'],
'CURRENCY_ISO_CODE' => $currency_item['currency_iso_code'],
'CURRENCY_NAME' => $currency_item['currency_name'],
'CURRENCY_SYMBOL' => $currency_item['currency_symbol'],
'S_CURRENCY_DEFAULT' => $config_value === (int) $currency_item['currency_id'],
]);
}
}
49 changes: 35 additions & 14 deletions actions/locale_icu.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,38 @@ public function build_locale_select_menu($config_value = ''): void
// Grab the list of all available locales
$locale_list = $this->get_locale_list();

// Process each locale item for pull-down
$this->process_locales($locale_list, $config_value);
}

/**
* Process each locale item for pull-down
*
* @param array $locale_list
* @param string $config_value
*/
private function process_locales(array $locale_list, string $config_value): void
{
foreach ($locale_list as $locale => $locale_name)
{
// Set output block vars for display in the template
$this->template->assign_block_vars('locale_options', [
'LOCALE_ID' => $locale,
'LOCALE_NAME' => $locale_name,
'S_LOCALE_DEFAULT' => $config_value === $locale,
]);
$this->assign_locale_to_template($locale, $locale_name, $config_value);
}
unset ($locale, $locale_list);
unset ($locale);
}

/**
* Assign locale options to the template
*
* @param string $locale
* @param string $locale_name
* @param string $config_value
*/
private function assign_locale_to_template(string $locale, string $locale_name, string $config_value): void
{
$this->template->assign_block_vars('locale_options', [
'LOCALE_ID' => $locale,
'LOCALE_NAME' => $locale_name,
'S_LOCALE_DEFAULT' => $config_value === $locale,
]);
}

/**
Expand All @@ -82,10 +103,10 @@ public function icu_requirements(): bool
/**
* Build an array of all locales
*
* @return mixed
* @return array
* @access private
*/
private function get_locale_list()
private function get_locale_list(): array
{
$locale_items = \ResourceBundle::getLocales('');
foreach ($locale_items as $locale)
Expand Down Expand Up @@ -152,10 +173,10 @@ public function numfmt_create()
* @param float $value
* @param string $currency_iso_code
*
* @return string
* @return false|string
* @access public
*/
public function numfmt_format_currency($fmt, $value, $currency_iso_code): string
public function numfmt_format_currency($fmt, $value, $currency_iso_code)
{
return numfmt_format_currency($fmt, (float) $value, (string) $currency_iso_code);
}
Expand Down Expand Up @@ -208,11 +229,11 @@ private function icu_available_features(): bool
/**
* Checks if ICU version matches with requirement
*
* @return bool
* @return bool|int
* @throws \ReflectionException
* @access private
*/
private function icu_version_compare(): bool
private function icu_version_compare()
{
$icu_min_version = '1.1.0';
$icu_version = $this->get_php_extension_version('intl', $this->icu_available_features());
Expand Down

0 comments on commit b6d44e3

Please sign in to comment.