Skip to content

Commit

Permalink
Fix typing for showOutputFieldForObject (Dolibarr#28709)
Browse files Browse the repository at this point in the history
# Fix typing for showOutputFieldForObject

Type hint that $value can also be a list of strings.
Also updated the "else-if" sequence so that a static tool checker can
still appropriately detect when $value is a string and when it is
a list of strings so that it provides correct feedback.
Added an error message to the syslog in case we got an array but not
"array" as the value for "$type"
  • Loading branch information
mdeweerd authored Mar 8, 2024
1 parent 7bf8f48 commit d93fd18
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions htdocs/webportal/class/html.formwebportal.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ public function showInputField($val, $key, $value, $moreparam = '', $keysuffix =
* @param CommonObject $object Common object
* @param array $val Array of properties of field to show
* @param string $key Key of attribute
* @param string $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value)
* @param string|string[] $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value)
* @param string $moreparam To add more parameters on html input tag
* @param string $keysuffix Prefix string to add into name and id of field (can be used to avoid duplicate names)
* @param string $keyprefix Suffix string to add into name and id of field (can be used to avoid duplicate names)
Expand Down Expand Up @@ -1026,6 +1026,9 @@ public function showOutputFieldForObject($object, $val, $key, $value, $moreparam
}

// Format output value differently according to properties of field
//
// First the cases that do not use $value from the arguments:
//
if (in_array($key, array('rowid', 'ref'))) {
if (property_exists($object, 'ref')) {
$value = $object->ref;
Expand All @@ -1036,6 +1039,20 @@ public function showOutputFieldForObject($object, $val, $key, $value, $moreparam
}
} elseif ($key == 'status' && method_exists($object, 'getLibStatut')) {
$value = $object->getLibStatut(3);
//
// Then the cases where $value is an array
//
} elseif (is_array($value)) {
// Handle array early to get type identification solve for static
// analysis
if ($type == 'array') {
$value = implode('<br>', $value);
} else {
dol_syslog(__METHOD__ . 'ERROR unexpected type=$type for array value='.((string) json_encode($value)), LOG_ERR);
}
//
// Then the cases where $value is not an array (hence string)
//
} elseif ($type == 'date') {
if (!empty($value)) {
$value = dol_print_date($value, 'day'); // We suppose dates without time are always gmt (storage of course + output)
Expand Down Expand Up @@ -1305,8 +1322,6 @@ public function showOutputFieldForObject($object, $val, $key, $value, $moreparam
}
} elseif ($type == 'password') {
$value = preg_replace('/./i', '*', $value);
} elseif ($type == 'array') {
$value = implode('<br>', $value);
} else { // text|html|varchar
$value = dol_htmlentitiesbr($value);
}
Expand Down

0 comments on commit d93fd18

Please sign in to comment.