From 9a9e851494a0a245e669082b837c72477c69d4b3 Mon Sep 17 00:00:00 2001 From: MDW Date: Mon, 16 Sep 2024 01:50:58 +0200 Subject: [PATCH] Qual: Fix new UndeclaredGlobalVariable notices & enable (#30963) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Qual: 🛟 Fix typing Qual: Fix typing Fix some typing currently failing ci in develop. * Fix typing for grabimages * Qual: Fix new UndeclaredGlobalVariable notices & enable # Qual: Fix new UndeclaredGlobalVariable notices & enable This fixes several new notices regarding 'undeclared globals' which mostly correspond to missing initialisations in views and some real bugs where the variable name was incorrect. Some less obvious cases are unsolved and ignored (adherents/list, and fourn/commande/card). This is in preparation of fixing the notices in the other files where the baseline was already set to ignore them where most just need a phan-var-force declaration which will help with verifying type compatibity and object member verifications --- dev/tools/phan/baseline.txt | 7 +--- dev/tools/phan/config.php | 6 +-- htdocs/admin/contract.php | 5 ++- htdocs/admin/holiday.php | 5 ++- htdocs/compta/paiement/rapport.php | 3 +- htdocs/core/class/html.formadmin.class.php | 31 +++++++-------- htdocs/core/lib/functions2.lib.php | 4 +- .../class/fournisseur.commande.class.php | 8 ++-- htdocs/fourn/commande/card.php | 38 +++++++++++-------- htdocs/fourn/facture/agenda.php | 3 ++ htdocs/fourn/facture/messaging.php | 3 ++ htdocs/theme/eldy/global.inc.php | 3 +- htdocs/variants/list.php | 4 +- 13 files changed, 68 insertions(+), 52 deletions(-) diff --git a/dev/tools/phan/baseline.txt b/dev/tools/phan/baseline.txt index 58894707c4b..068d0e5f593 100644 --- a/dev/tools/phan/baseline.txt +++ b/dev/tools/phan/baseline.txt @@ -51,6 +51,7 @@ return [ 'htdocs/accountancy/class/accountancycategory.class.php' => ['PhanPluginUnknownArrayPropertyType'], 'htdocs/accountancy/class/accountancyexport.class.php' => ['PhanUndeclaredProperty'], 'htdocs/adherents/canvas/actions_adherentcard_common.class.php' => [/* Traverses Adherent as array, ignore */ 'PhanTypeSuspiciousNonTraversableForeach'], + 'htdocs/adherents/list.php' => ['PhanUndeclaredGlobalVariable'], 'htdocs/admin/agenda_extsites.php' => ['PhanTypeMismatchArgumentProbablyReal'], 'htdocs/admin/agenda_other.php' => ['PhanPluginUnknownObjectMethodCall', 'PhanUndeclaredGlobalVariable'], 'htdocs/admin/agenda_reminder.php' => ['PhanUndeclaredGlobalVariable'], @@ -60,7 +61,6 @@ return [ 'htdocs/admin/bom.php' => ['PhanPluginUnknownObjectMethodCall', 'PhanPossiblyUndeclaredGlobalVariable'], 'htdocs/admin/chequereceipts.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchArgumentProbablyReal'], 'htdocs/admin/company.php' => ['PhanPossiblyUndeclaredGlobalVariable'], - 'htdocs/admin/contract.php' => ['PhanPossiblyUndeclaredGlobalVariable'], 'htdocs/admin/delais.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchDimFetch'], 'htdocs/admin/delivery.php' => ['PhanPossiblyUndeclaredGlobalVariable'], 'htdocs/admin/dict.php' => ['PhanPluginUnknownArrayFunctionParamType', 'PhanPossiblyUndeclaredGlobalVariable', 'PhanRedefineFunction', 'PhanTypeMismatchArgumentProbablyReal'], @@ -73,7 +73,6 @@ return [ 'htdocs/admin/fckeditor.php' => ['PhanTypeMismatchArgumentProbablyReal'], 'htdocs/admin/fichinter.php' => ['PhanPossiblyUndeclaredGlobalVariable'], 'htdocs/admin/geoipmaxmind.php' => ['PhanUndeclaredGlobalVariable'], - 'htdocs/admin/holiday.php' => ['PhanPossiblyUndeclaredGlobalVariable'], 'htdocs/admin/ihm.php' => ['PhanTypeMismatchArgumentProbablyReal'], 'htdocs/admin/invoice.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchArgumentProbablyReal'], 'htdocs/admin/invoice_situation.php' => ['PhanUndeclaredGlobalVariable'], @@ -400,7 +399,6 @@ return [ 'htdocs/core/class/fiscalyear.class.php' => ['PhanPluginUnknownArrayMethodParamType', 'PhanPluginUnknownArrayMethodReturnType', 'PhanPossiblyUndeclaredVariable'], 'htdocs/core/class/hookmanager.class.php' => ['PhanUndeclaredProperty'], 'htdocs/core/class/html.formaccounting.class.php' => ['PhanPossiblyUndeclaredVariable'], - 'htdocs/core/class/html.formadmin.class.php' => ['PhanPluginUnknownArrayMethodParamType'], 'htdocs/core/class/html.formbarcode.class.php' => ['PhanPluginUnknownArrayMethodParamType'], 'htdocs/core/class/html.formcategory.class.php' => ['PhanPluginUnknownArrayMethodParamType'], 'htdocs/core/class/html.formcompany.class.php' => ['PhanPluginUnknownArrayMethodParamType', 'PhanPluginUnknownArrayMethodReturnType', 'PhanPluginUnknownObjectMethodCall', 'PhanPossiblyUndeclaredVariable', 'PhanTypeMismatchArgumentProbablyReal', 'PhanUndeclaredProperty'], @@ -744,7 +742,7 @@ return [ 'htdocs/fourn/class/fournisseur.facture-rec.class.php' => ['PhanPluginUnknownPropertyType', 'PhanUndeclaredProperty'], 'htdocs/fourn/class/fournisseur.product.class.php' => ['PhanPluginUnknownArrayMethodParamType', 'PhanPluginUnknownObjectMethodCall', 'PhanPluginUnknownPropertyType', 'PhanTypeMismatchProperty'], 'htdocs/fourn/class/paiementfourn.class.php' => ['PhanEmptyForeach', 'PhanPluginUnknownArrayMethodParamType', 'PhanPluginUnknownPropertyType', 'PhanPossiblyNullTypeMismatchProperty', 'PhanPossiblyUndeclaredVariable', 'PhanTypeMismatchArgumentProbablyReal'], - 'htdocs/fourn/commande/card.php' => ['PhanPluginBothLiteralsBinaryOp', 'PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchProperty', 'PhanUndeclaredProperty'], + 'htdocs/fourn/commande/card.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchProperty', 'PhanUndeclaredProperty'], 'htdocs/fourn/commande/contact.php' => ['PhanUndeclaredGlobalVariable'], 'htdocs/fourn/commande/dispatch.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeExpectedObjectPropAccessButGotNull', 'PhanTypeMismatchArgumentNullableInternal', 'PhanTypeMismatchArgumentProbablyReal', 'PhanUndeclaredGlobalVariable'], 'htdocs/fourn/commande/document.php' => ['PhanUndeclaredGlobalVariable'], @@ -1127,7 +1125,6 @@ return [ 'htdocs/variants/class/ProductCombination.class.php' => ['PhanPluginSuspiciousParamPosition'], 'htdocs/variants/combinations.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchProperty'], 'htdocs/variants/lib/variants.lib.php' => ['PhanPluginUnknownArrayFunctionReturnType'], - 'htdocs/variants/list.php' => ['PhanTypeMismatchArgumentProbablyReal'], 'htdocs/variants/tpl/productattributevalueline_edit.tpl.php' => ['PhanUndeclaredGlobalVariable', 'PhanUndeclaredProperty'], 'htdocs/variants/tpl/productattributevalueline_view.tpl.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanUndeclaredGlobalVariable', 'PhanUndeclaredProperty'], 'htdocs/viewimage.php' => ['PhanPluginUnknownObjectMethodCall', 'PhanRedefineFunction'], diff --git a/dev/tools/phan/config.php b/dev/tools/phan/config.php index 840065e5fd7..23f7390071b 100644 --- a/dev/tools/phan/config.php +++ b/dev/tools/phan/config.php @@ -424,7 +424,7 @@ return [ 'PhanTypePossiblyInvalidDimOffset', // a lot of false positive, in most cases, we want to keep the code as it is // 'PhanPluginUnknownArrayFunctionReturnType', // a lot of false positive, in most cases, we want to keep the code as it is - 'PhanPluginWhitespaceTab', // Dolibarr used tabs + 'PhanPluginWhitespaceTab', // Dolibarr uses tabs 'PhanPluginCanUsePHP71Void', // Dolibarr is maintaining 7.0 compatibility 'PhanPluginShortArray', // Dolibarr uses array() 'PhanPluginShortArrayList', // Dolibarr uses array() @@ -436,7 +436,7 @@ return [ 'PhanPluginNonBoolBranch', // Not essential - 31240+ occurrences 'PhanPluginNumericalComparison', // Not essential - 19870+ occurrences - 'PhanTypeMismatchArgument', // Not essential - 12300+ occurrences + 'PhanTypeMismatchArgument', // Also reported by phpstan < lvl6 - 12300+ occurrences 'PhanPluginNonBoolInLogicalArith', // Not essential - 11040+ occurrences 'PhanPluginConstantVariableScalar', // Not essential - 5180+ occurrences 'PhanPluginDuplicateAdjacentStatement', @@ -448,7 +448,7 @@ return [ // 'PhanPluginUnknownArrayMethodParamType', // Too many troubles to manage. Is enabled in config_extended only. // 'PhanPluginUnknownArrayMethodReturnType', // Too many troubles to manage. Is enabled in config_extended only. - 'PhanUndeclaredGlobalVariable', // Too many false positives on .tpl.php files. Is enabled into config_extended only. + // 'PhanUndeclaredGlobalVariable', // Helps identify variables that are not set/defined - add '@phan-var-force TYPE $varname' in tpl or includes to help type the variable // 'PhanPluginUnknownObjectMethodCall', // False positive for some class. Is enabled in config_extended only. ], // You can put relative paths to internal stubs in this config option. diff --git a/htdocs/admin/contract.php b/htdocs/admin/contract.php index da9a81fefdb..6c02cf37ae4 100644 --- a/htdocs/admin/contract.php +++ b/htdocs/admin/contract.php @@ -62,6 +62,7 @@ $error = 0; if ($action == 'updateMask') { $maskconst = GETPOST('maskconstcontract', 'aZ09'); $maskvalue = GETPOST('maskcontract', 'alpha'); + $res = 0; if ($maskconst && preg_match('/_MASK$/', $maskconst)) { $res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity); } @@ -103,8 +104,8 @@ if ($action == 'updateMask') { header("Location: ".DOL_URL_ROOT."/document.php?modulepart=contract&file=SPECIMEN.pdf"); return; } else { - setEventMessages($obj->error, $obj->errors, 'errors'); - dol_syslog($obj->error, LOG_ERR); + setEventMessages($module->error, $module->errors, 'errors'); + dol_syslog($module->error, LOG_ERR); } } else { setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors'); diff --git a/htdocs/admin/holiday.php b/htdocs/admin/holiday.php index 9b2fa611935..3f32848b594 100644 --- a/htdocs/admin/holiday.php +++ b/htdocs/admin/holiday.php @@ -61,6 +61,7 @@ include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php'; if ($action == 'updateMask') { $maskconst = GETPOST('maskconstholiday', 'aZ09'); $maskvalue = GETPOST('maskholiday', 'alpha'); + $res = 0; if ($maskconst && preg_match('/_MASK$/', $maskconst)) { $res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity); } @@ -102,8 +103,8 @@ if ($action == 'updateMask') { header("Location: ".DOL_URL_ROOT."/document.php?modulepart=holiday&file=SPECIMEN.pdf"); return; } else { - setEventMessages($obj->error, $obj->errors, 'errors'); - dol_syslog($obj->error, LOG_ERR); + setEventMessages($module->error, $module->errors, 'errors'); + dol_syslog($module->error, LOG_ERR); } } else { setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors'); diff --git a/htdocs/compta/paiement/rapport.php b/htdocs/compta/paiement/rapport.php index 6690a9a7581..72849b4da30 100644 --- a/htdocs/compta/paiement/rapport.php +++ b/htdocs/compta/paiement/rapport.php @@ -4,6 +4,7 @@ * Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2020 Maxime DEMAREST * Copyright (C) 2024 Frédéric France + * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -78,7 +79,7 @@ if ($action == 'builddoc' && $permissiontoread) { $outputlangs->charset_output = $sav_charset_output; } else { $outputlangs->charset_output = $sav_charset_output; - dol_print_error($db, $obj->error); + dol_print_error($db, $rap->error); } $year = GETPOSTINT("reyear"); diff --git a/htdocs/core/class/html.formadmin.class.php b/htdocs/core/class/html.formadmin.class.php index 983423590f2..c00a3913872 100644 --- a/htdocs/core/class/html.formadmin.class.php +++ b/htdocs/core/class/html.formadmin.class.php @@ -3,6 +3,7 @@ * Copyright (C) 2005-2011 Regis Houssin * Copyright (C) 2007 Patrick Raguin * Copyright (C) 2024 Frédéric France + * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -55,19 +56,19 @@ class FormAdmin /** * Return html select list with available languages (key='en_US', value='United States' for example) * - * @param string|array $selected Language pre-selected. Can be an array if $multiselect is 1. + * @param string|string[] $selected Language pre-selected. Can be an array if $multiselect is 1. * @param string $htmlname Name of HTML select - * @param int $showauto Show 'auto' choice - * @param array $filter Array of keys to exclude in list (opposite of $onlykeys) - * @param int|string $showempty '1'=Add empty value or 'string to show' - * @param int $showwarning Show a warning if language is not complete - * @param int $disabled Disable edit of select + * @param int<0,1> $showauto Show 'auto' choice + * @param string[] $filter Array of keys to exclude in list (opposite of $onlykeys) + * @param int<1,1>|string $showempty '1'=Add empty value or 'string to show' + * @param int<0,1> $showwarning Show a warning if language is not complete + * @param int<0,1> $disabled Disable edit of select * @param string $morecss Add more css styles - * @param int $showcode 1=Add language code into label at beginning, 2=Add language code into label at end - * @param int $forcecombo Force to use combo box (so no ajax beautify effect) - * @param int $multiselect Make the combo a multiselect - * @param array $onlykeys Array of language keys to restrict list with the following keys (opposite of $filter). Example array('fr', 'es', ...) - * @param int $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...) + * @param int<0,2> $showcode 1=Add language code into label at beginning, 2=Add language code into label at end + * @param int<0,1> $forcecombo Force to use combo box (so no ajax beautify effect) + * @param int<0,1> $multiselect Make the combo a multiselect + * @param string[] $onlykeys Array of language keys to restrict list with the following keys (opposite of $filter). Example array('fr', 'es', ...) + * @param int<0,1> $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...) * @return string Return HTML select string with list of languages */ public function select_language($selected = '', $htmlname = 'lang_id', $showauto = 0, $filter = array(), $showempty = '', $showwarning = 0, $disabled = 0, $morecss = '', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = array(), $mainlangonly = 0) @@ -183,7 +184,7 @@ class FormAdmin * * @param string $selected Preselected menu value * @param string $htmlname Name of html select - * @param array $dirmenuarray Array of directories to scan + * @param string[] $dirmenuarray Array of directories to scan * @param string $moreattrib More attributes on html select tag * @return integer|void */ @@ -500,9 +501,9 @@ class FormAdmin /** * Function to show the combo select to chose a type of field (varchar, int, email, ...) * - * @param string $htmlname Name of HTML select component - * @param string $type Type preselected - * @param array $typewecanchangeinto Array of possible switch combination from 1 type to another one. This will grey not possible combinations. + * @param string $htmlname Name of HTML select component + * @param string $type Type preselected + * @param string[] $typewecanchangeinto Array of possible switch combination from 1 type to another one. This will grey not possible combinations. * @return string The combo HTML select component */ public function selectTypeOfFields($htmlname, $type, $typewecanchangeinto = array()) diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 72161f32e5e..07fae627c56 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -2538,7 +2538,7 @@ function colorLighten($hex, $percent) * @param string $hex color in hex * @param float|false $alpha 0 to 1 to add alpha channel * @param bool $returnArray true=return an array instead, false=return string - * @return string|array String or array + * @return string|array{r:int,g:int,b:int,a?:float} String or array */ function colorHexToRgb($hex, $alpha = false, $returnArray = false) { @@ -2569,7 +2569,7 @@ function colorHexToRgb($hex, $alpha = false, $returnArray = false) * @param string $hex Color in hex * @param float|false $alpha 0 to 1 to add alpha channel * @param bool $returnArray true=return an array instead, false=return string - * @return string|array String or array + * @return array{h:float,l:float,s:float,a:int|float}|string HSLA as string or array */ function colorHexToHsl($hex, $alpha = false, $returnArray = false) { diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php index d09b8bbe301..d9bbf9ed43a 100644 --- a/htdocs/fourn/class/fournisseur.commande.class.php +++ b/htdocs/fourn/class/fournisseur.commande.class.php @@ -1919,10 +1919,10 @@ class CommandeFournisseur extends CommonOrder * @param int $type Type of line (0=product, 1=service) * @param int $info_bits More information * @param int $notrigger Disable triggers - * @param int $date_start Date start of service - * @param int $date_end Date end of service - * @param array $array_options extrafields array - * @param int|null $fk_unit Code of the unit to use. Null to use the default one + * @param ?int $date_start Date start of service + * @param ?int $date_end Date end of service + * @param array $array_options extrafields array + * @param ?int $fk_unit Code of the unit to use. Null to use the default one * @param int|string $pu_ht_devise Amount in currency * @param string $origin 'order', ... * @param int $origin_id Id of origin object diff --git a/htdocs/fourn/commande/card.php b/htdocs/fourn/commande/card.php index 6486f472f03..343ac7a91c2 100644 --- a/htdocs/fourn/commande/card.php +++ b/htdocs/fourn/commande/card.php @@ -628,7 +628,7 @@ if (empty($reshook)) { ($price_base_type == 'TTC' ? $pu : 0), $type, $tva_npr, - '', + 0, $date_start, $date_end, $array_options, @@ -671,6 +671,7 @@ if (empty($reshook)) { if (GETPOST('price_ht') != '' || GETPOST('multicurrency_price_ht') != '') { $pu_ht = price2num($price_ht, 'MU'); // $pu_ht must be rounded according to settings + $pu_ttc = ''; } else { $pu_ttc = price2num(GETPOST('price_ttc'), 'MU'); $pu_ht = price2num((float) $pu_ttc / (1 + ((float) $tva_tx / 100)), 'MU'); // $pu_ht must be rounded according to settings @@ -678,7 +679,7 @@ if (empty($reshook)) { $price_base_type = 'HT'; $pu_ht_devise = price2num($price_ht_devise, 'CU'); - $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 0, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, '', '', $date_start, $date_end, $array_options, $fk_unit, $pu_ht_devise); + $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 0, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, 0, 0, $date_start, $date_end, $array_options, $fk_unit, $pu_ht_devise); } //print "xx".$tva_tx; exit; @@ -1123,6 +1124,7 @@ if (empty($reshook)) { // Action clone object if ($action == 'confirm_clone' && $confirm == 'yes' && $usercancreate) { + // @phan-suppress-next-line PhanPluginBothLiteralsBinaryOp if (1 == 0 && !GETPOST('clone_content') && !GETPOST('clone_receivers')) { setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors'); } else { @@ -1398,8 +1400,8 @@ if (empty($reshook)) { 'HT', 0, $lines[$i]->product_type, - '', - '', + 0, + 0, null, null, $array_option, @@ -1529,6 +1531,8 @@ if ($action == 'create') { $currency_code = $conf->currency; $societe = ''; + $objectsrc = null; + if ($socid > 0) { $societe = new Societe($db); $societe->fetch($socid); @@ -1575,6 +1579,7 @@ if ($action == 'create') { $projectid = (!empty($objectsrc->fk_project) ? $objectsrc->fk_project : ''); $ref_client = (!empty($objectsrc->ref_client) ? $objectsrc->ref_client : ''); + $fk_account = 0; if ($origin == "commande") { $cond_reglement_id = 0; $mode_reglement_id = 0; @@ -1658,7 +1663,7 @@ if ($action == 'create') { print ''; } - print dol_get_fiche_head(''); + print dol_get_fiche_head(array()); // Call Hook tabContentCreateSupplierOrder $parameters = array(); @@ -1679,7 +1684,7 @@ if ($action == 'create') { print ''; } else { $filter = '((s.fournisseur:=:1) AND (s.status:=:1))'; - print img_picto('', 'company', 'class="pictofixedwidth"').$form->select_company((empty($socid) ? '' : $socid), 'socid', $filter, 'SelectThirdParty', 1, 0, null, 0, 'minwidth175 maxwidth500 widthcentpercentminusxx'); + print img_picto('', 'company', 'class="pictofixedwidth"').$form->select_company((empty($socid) ? '' : $socid), 'socid', $filter, 'SelectThirdParty', 1, 0, array(), 0, 'minwidth175 maxwidth500 widthcentpercentminusxx'); // reload page to retrieve customer information if (!getDolGlobalString('RELOAD_PAGE_ON_SUPPLIER_CHANGE_DISABLED')) { print '