From 619998f38c0c4df46011378808e3bc6a7d99d316 Mon Sep 17 00:00:00 2001 From: MDW Date: Sat, 30 Mar 2024 17:11:14 +0100 Subject: [PATCH] Typing improvements for html.form.class --- htdocs/core/class/html.form.class.php | 269 ++++++++++++++------------ 1 file changed, 140 insertions(+), 129 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 5839f63192b7d..9389676608608 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2207,7 +2207,7 @@ public function select_dolusers($selected = '', $htmlname = 'userid', $show_empt if (!empty($disableline)) { $out .= ' disabled'; } - if ((is_object($selected[0]) && $selected[0]->id == $obj->rowid) || (!is_object($selected) && in_array($obj->rowid, $selected))) { + if ((!empty($selected[0]) && is_object($selected[0])) ? $selected[0]->id == $obj->rowid : in_array($obj->rowid, $selected)) { $out .= ' selected'; } $out .= ' data-html="'; @@ -2293,8 +2293,10 @@ public function select_dolusers_forevent($action = '', $htmlname = 'userid', $sh if (!empty($_SESSION['assignedtouser'])) { $assignedtouser = json_decode($_SESSION['assignedtouser'], true); - } - if (!is_array($assignedtouser)) { + if (!is_array($assignedtouser)) { + $assignedtouser = array(); + } + } else { $assignedtouser = array(); } $nbassignetouser = count($assignedtouser); @@ -2364,16 +2366,16 @@ public function select_dolusers_forevent($action = '', $htmlname = 'userid', $sh * @param string $action Value for $action * @param string $htmlname Field name in form * @param int $show_empty 0=list without the empty value, 1=add empty value - * @param array $exclude Array list of users id to exclude - * @param int $disabled If select list must be disabled - * @param array $include Array list of users id to include or 'hierarchy' to have only supervised users - * @param array $enableonly Array list of users id to be enabled. All other must be disabled + * @param int[] $exclude Array list of users id to exclude + * @param int<0,1> $disabled If select list must be disabled + * @param int[]|string $include Array list of users id to include or 'hierarchy' to have only supervised users + * @param int[] $enableonly Array list of users id to be enabled. All other must be disabled * @param string $force_entity '0' or Ids of environment to force * @param int $maxlength Maximum length of string into list (0=no limit) - * @param int $showstatus 0=show user status only if status is disabled, 1=always show user status into label, -1=never show user status + * @param int<-1,1> $showstatus 0=show user status only if status is disabled, 1=always show user status into label, -1=never show user status * @param string $morefilter Add more filters into sql request * @param int $showproperties Show properties of each attendees - * @param array $listofresourceid Array with properties of each resource + * @param array}> $listofresourceid Array with properties of each resource * @return string HTML select string */ public function select_dolresources_forevent($action = '', $htmlname = 'userid', $show_empty = 0, $exclude = null, $disabled = 0, $include = array(), $enableonly = array(), $force_entity = '0', $maxlength = 0, $showstatus = 0, $morefilter = '', $showproperties = 0, $listofresourceid = array()) @@ -2387,9 +2389,13 @@ public function select_dolresources_forevent($action = '', $htmlname = 'userid', $resourcestatic = new Dolresource($this->db); $out = ''; - $assignedtoresource = array(); if (!empty($_SESSION['assignedtoresource'])) { $assignedtoresource = json_decode($_SESSION['assignedtoresource'], true); + if (!is_array($assignedtoresource)) { + $assignedtoresource = array(); + } + } else { + $assignedtoresource = array(); } $nbassignetoresource = count($assignedtoresource); @@ -2437,7 +2443,7 @@ public function select_dolresources_forevent($action = '', $htmlname = 'userid', $events = array(); $out .= img_picto('', 'resource', 'class="pictofixedwidth"'); - $out .= $formresources->select_resource_list(0, $htmlname, [], 1, 1, 0, $events, '', 2, 0); + $out .= $formresources->select_resource_list(0, $htmlname, [], 1, 1, 0, $events, array(), 2, 0); //$out .= $this->select_dolusers('', $htmlname, $show_empty, $exclude, $disabled, $include, $enableonly, $force_entity, $maxlength, $showstatus, $morefilter); $out .= ' '; $out .= '
'; @@ -2460,19 +2466,19 @@ public function select_dolresources_forevent($action = '', $htmlname = 'userid', * @param int $finished 2=all, 1=finished, 0=raw material * @param string $selected_input_value Value of preselected input text (for use with ajax) * @param int $hidelabel Hide label (0=no, 1=yes, 2=show search icon (before) and placeholder, 3 search icon after) - * @param array $ajaxoptions Options for ajax_autocompleter + * @param array $ajaxoptions Options for ajax_autocompleter * @param int $socid Thirdparty Id (to get also price dedicated to this customer) * @param string|int<0,1> $showempty '' to not show empty line. Translation key to show an empty line. '1' show empty line with no text. * @param int $forcecombo Force to use combo box * @param string $morecss Add more css on select - * @param int $hidepriceinlabel 1=Hide prices in label + * @param int<0,1> $hidepriceinlabel 1=Hide prices in label * @param string $warehouseStatus Warehouse status filter to count the quantity in stock. Following comma separated filter options can be used * 'warehouseopen' = count products from open warehouses, * 'warehouseclosed' = count products from closed warehouses, * 'warehouseinternal' = count products from warehouses for internal correct/transfer only - * @param array $selected_combinations Selected combinations. Format: array([attrid] => attrval, [...]) - * @param int $nooutput No print if 1, return the output into a string - * @param int $status_purchase Purchase status: -1=No filter on purchase status, 0=Products not on purchase, 1=Products on purchase + * @param ?mixed[] $selected_combinations Selected combinations. Format: array([attrid] => attrval, [...]) + * @param int<0,1> $nooutput No print if 1, return the output into a string + * @param int<-1,1> $status_purchase Purchase status: -1=No filter on purchase status, 0=Products not on purchase, 1=Products on purchase * @return void|string */ public function select_produits($selected = 0, $htmlname = 'productid', $filtertype = '', $limit = 0, $price_level = 0, $status = 1, $finished = 2, $selected_input_value = '', $hidelabel = 0, $ajaxoptions = array(), $socid = 0, $showempty = '1', $forcecombo = 0, $morecss = '', $hidepriceinlabel = 0, $warehouseStatus = '', $selected_combinations = null, $nooutput = 0, $status_purchase = -1) @@ -2622,17 +2628,17 @@ public function select_produits($selected = 0, $htmlname = 'productid', $filtert /** * Return list of BOM for customer in Ajax if Ajax activated or go to select_produits_list * - * @param string $selected Preselected BOM id - * @param string $htmlname Name of HTML select field (must be unique in page). - * @param int $limit Limit on number of returned lines - * @param int $status Sell status -1=Return all bom, 0=Draft BOM, 1=Validated BOM - * @param int $type type of the BOM (-1=Return all BOM, 0=Return disassemble BOM, 1=Return manufacturing BOM) + * @param string $selected Preselected BOM id + * @param string $htmlname Name of HTML select field (must be unique in page). + * @param int $limit Limit on number of returned lines + * @param int $status Sell status -1=Return all bom, 0=Draft BOM, 1=Validated BOM + * @param int $type type of the BOM (-1=Return all BOM, 0=Return disassemble BOM, 1=Return manufacturing BOM) * @param string|int<0,1> $showempty '' to not show empty line. Translation key to show an empty line. '1' show empty line with no text. - * @param string $morecss Add more css on select - * @param string $nooutput No print, return the output into a string - * @param int $forcecombo Force to use combo box - * @param array $TProducts Add filter on a defined product - * @return void|string + * @param string $morecss Add more css on select + * @param string $nooutput No print, return the output into a string + * @param int $forcecombo Force to use combo box + * @param string[] $TProducts Add filter on a defined product + * @return void|string */ public function select_bom($selected = '', $htmlname = 'bom_id', $limit = 0, $status = 1, $type = 0, $showempty = '1', $morecss = '', $nooutput = '', $forcecombo = 0, $TProducts = []) { @@ -2956,7 +2962,7 @@ public function select_produits_list($selected = 0, $htmlname = 'productid', $fi $num = $this->db->num_rows($result); - $events = null; + $events = array(); if (!$forcecombo) { include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php'; @@ -3040,7 +3046,7 @@ public function select_produits_list($selected = 0, $htmlname = 'productid', $fi $objp->price = $price_result; $objp->unitprice = $price_result; //Calculate the VAT - $objp->price_ttc = price2num($objp->price) * (1 + ($objp->tva_tx / 100)); + $objp->price_ttc = (float) price2num($objp->price) * (1 + ($objp->tva_tx / 100)); $objp->price_ttc = price2num($objp->price_ttc, 'MU'); } } @@ -3077,14 +3083,14 @@ public function select_produits_list($selected = 0, $htmlname = 'productid', $fi * This define value for &$opt and &$optJson. * This function is called by select_produits_list(). * - * @param object $objp Resultset of fetch + * @param stdClass $objp Resultset of fetch * @param string $opt Option (var used for returned value in string option format) - * @param array $optJson Option (var used for returned value in json format) + * @param array{key:string,value:string,label:string,label2:string,desc:string,type:string,price_ht:string,price_ttc:string,price_ht_locale:string,price_ttc_locale:string,pricebasetype:string,tva_tx:string,default_vat_code:string,qty:string,discount:string,duration_value:string,duration_unit:string,pbq:string,labeltrans:string,desctrans:string,ref_customer:string} $optJson Option (var used for returned value in json format) * @param int $price_level Price level * @param int $selected Preselected value - * @param int $hidepriceinlabel Hide price in label + * @param int<0,1> $hidepriceinlabel Hide price in label * @param string $filterkey Filter key to highlight - * @param int $novirtualstock Do not load virtual stock, even if slow option STOCK_SHOW_VIRTUAL_STOCK_IN_PRODUCTS_COMBO is on. + * @param int<0,1> $novirtualstock Do not load virtual stock, even if slow option STOCK_SHOW_VIRTUAL_STOCK_IN_PRODUCTS_COMBO is on. * @return void */ protected function constructProductListOption(&$objp, &$opt, &$optJson, $price_level, $selected, $hidepriceinlabel = 0, $filterkey = '', $novirtualstock = 0) @@ -3420,9 +3426,9 @@ protected function constructProductListOption(&$objp, &$opt, &$optJson, $price_l * @param string $htmlname Name of HTML Select * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service) * @param string $filtre For a SQL filter - * @param array $ajaxoptions Options for ajax_autocompleter - * @param int $hidelabel Hide label (0=no, 1=yes) - * @param int $alsoproductwithnosupplierprice 1=Add also product without supplier prices + * @param array $ajaxoptions Options for ajax_autocompleter + * @param int<0,1> $hidelabel Hide label (0=no, 1=yes) + * @param int<0,1> $alsoproductwithnosupplierprice 1=Add also product without supplier prices * @param string $morecss More CSS * @param string $placeholder Placeholder * @return void @@ -3837,7 +3843,7 @@ public function select_produits_fournisseurs_list($socid, $selected = '', $htmln 'type' => $outtype, 'duration_value' => $outdurationvalue, 'duration_unit' => $outdurationunit, - 'disabled' => (empty($objp->idprodfournprice) ? true : false), + 'disabled' => empty($objp->idprodfournprice), 'description' => $objp->description ); if (isModEnabled('multicurrency')) { @@ -3876,7 +3882,7 @@ public function select_produits_fournisseurs_list($socid, $selected = '', $htmln 'type' => $outtype, 'duration_value' => $outdurationvalue, 'duration_unit' => $outdurationunit, - 'disabled' => (empty($objp->idprodfournprice) ? true : false), + 'disabled' => empty($objp->idprodfournprice), 'description' => $objp->description ); if (isModEnabled('multicurrency')) { @@ -5198,7 +5204,7 @@ public function select_all_categories($type, $selected = '', $htmlname = "parent * @param string $title Title * @param string $question Question * @param string $action Action - * @param array $formquestion An array with forms complementary inputs + * @param array $formquestion An array with complementary inputs to add into forms: array(array('label'=> ,'type'=> , 'size'=>, 'morecss'=>, 'moreattr'=>'autofocus' or 'style=...')) * @param string $selectedchoice "" or "no" or "yes" * @param int|string $useajax 0=No, 1=Yes use Ajax to show the popup, 2=Yes and also submit page with &confirm=no if choice is No, 'xxx'=Yes and preoutput confirm box with div id=dialog-confirm-xxx * @param int $height Force height of box @@ -5229,9 +5235,9 @@ public function form_confirm($page, $title, $question, $action, $formquestion = * @param string $title Title * @param string $question Question * @param string $action Action - * @param array|string|null $formquestion An array with complementary inputs to add into forms: array(array('label'=> ,'type'=> , 'size'=>, 'morecss'=>, 'moreattr'=>'autofocus' or 'style=...')) - * 'type' can be 'text', 'password', 'checkbox', 'radio', 'date', 'datetime', 'select', 'multiselect', 'morecss', - * 'other', 'onecolumn' or 'hidden'... + * @param array}>|string|null $formquestion An array with complementary inputs to add into forms: array(array('label'=> ,'type'=> , 'size'=>, 'morecss'=>, 'moreattr'=>'autofocus' or 'style=...')) + * 'type' can be 'text', 'password', 'checkbox', 'radio', 'date', 'datetime', 'select', 'multiselect', 'morecss', + * 'other', 'onecolumn' or 'hidden'... * @param int|string $selectedchoice '' or 'no', or 'yes' or '1', 1, '0' or 0 * @param int|string $useajax 0=No, 1=Yes use Ajax to show the popup, 2=Yes and also submit page with &confirm=no if choice is No, 'xxx'=Yes and preoutput confirm box with div id=dialog-confirm-xxx * @param int|string $height Force height of box (0 = auto) @@ -5439,7 +5445,7 @@ public function formconfirm($page, $title, $question, $action, $formquestion = ' } } // Add name of fields to propagate with the GET when submitting the form with button KO. - if (isset($input['inputko']) && $input['inputko'] == 1) { + if (isset($input['inputko']) && $input['inputko'] == 1 && isset($input['name'])) { array_push($inputko, $input['name']); } } @@ -5453,7 +5459,7 @@ public function formconfirm($page, $title, $question, $action, $formquestion = ' if (!empty($more)) { $formconfirm .= '
' . $more . '
' . "\n"; } - $formconfirm .= ($question ? '
' . img_help('', '') . ' ' . $question . '
' : ''); + $formconfirm .= ($question ? '
' . img_help(0, '') . ' ' . $question . '
' : ''); $formconfirm .= '' . "\n"; $formconfirm .= "\n\n"; @@ -5882,8 +5888,8 @@ public function form_date($page, $selected, $htmlname, $displayhour = 0, $displa * @param string $page Page * @param string $selected Id of user preselected * @param string $htmlname Name of input html field. If 'none', we just output the user link. - * @param array $exclude List of users id to exclude - * @param array $include List of users id to include + * @param int[] $exclude List of users id to exclude + * @param int[] $include List of users id to include * @return void */ public function form_users($page, $selected = '', $htmlname = 'userid', $exclude = array(), $include = array()) @@ -6215,7 +6221,7 @@ public function form_contacts($page, $societe, $selected = '', $htmlname = 'cont * @param int<0,1> $forcecombo Force to use combo box * @param array}> $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) * @param int $nooutput No print output. Return it only. - * @param array $excludeids Exclude IDs from the select combo + * @param int[] $excludeids Exclude IDs from the select combo * @param string $textifnothirdparty Text to show if no thirdparty * @return string HTML output or '' */ @@ -7228,7 +7234,7 @@ public function selectDate($set_time = '', $prefix = 're', $h = 0, $m = 0, $empt * * @param string $prefix Prefix * @param string $selected Selected duration type - * @param array $excludetypes Array of duration types to exclude. Example array('y', 'm') + * @param string[] $excludetypes Array of duration types to exclude. Example array('y', 'm') * @return string HTML select string */ public function selectTypeDuration($prefix, $selected = 'i', $excludetypes = array()) @@ -7330,7 +7336,7 @@ public function select_duration($prefix, $iSecond = '', $disabled = 0, $typehour if ($typehour == 'select' || $typehour == 'textselect') { $retstring .= '