From 320f0590d2efe86d46ae9eaebe72408ad2e70d84 Mon Sep 17 00:00:00 2001 From: MDW Date: Mon, 16 Sep 2024 01:46:20 +0200 Subject: [PATCH] Qual: More specific phpstan exceptions (#30967) 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: More specific phpstan exceptions # Qual: More specific phpstan exceptions Avoid ignoring new types of code errors --- htdocs/core/lib/website.lib.php | 10 +++--- htdocs/website/index.php | 2 +- phpstan.neon.dist | 61 ++++++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/htdocs/core/lib/website.lib.php b/htdocs/core/lib/website.lib.php index 3fb8d673db3..371c2cec1b8 100644 --- a/htdocs/core/lib/website.lib.php +++ b/htdocs/core/lib/website.lib.php @@ -1325,7 +1325,7 @@ function getPagesFromSearchCriterias($type, $algo, $searchstring, $max = 25, $so * To get image from an external URL to download first, see getAllImages() * * @param string $htmlContent HTML content - * @param string $imageNumber The position of image. 1 by default = first image found + * @param int $imageNumber The position of image. 1 by default = first image found * @return string URL of image or '' if not foud */ function getImageFromHtmlContent($htmlContent, $imageNumber = 1) @@ -1353,7 +1353,7 @@ function getImageFromHtmlContent($htmlContent, $imageNumber = 1) if ($imageNumber > 0 && $imageNumber <= $images->length) { // Récupère l'image correspondante (index - 1 car $imageNumber est 1-based) $img = $images->item($imageNumber - 1); - if ($img) { + if ($img instanceof DOMElement) { return $img->getAttribute('src'); } } @@ -1372,9 +1372,9 @@ function getImageFromHtmlContent($htmlContent, $imageNumber = 1) * @param string $urltograb URL to grab (example: https://www.nltechno.com/ or s://www.nltechno.com/dir1/ or https://www.nltechno.com/dir1/mapage1) * @param string $tmp Content to parse * @param string $action Var $action - * @param int $modifylinks 0=Do not modify content, 1=Replace links with a link to viewimage - * @param int $grabimages 0=Do not grab images, 1=Grab images - * @param string $grabimagesinto 'root' or 'subpage' + * @param int<0,1> $modifylinks 0=Do not modify content, 1=Replace links with a link to viewimage + * @param int<0,1> $grabimages 0=Do not grab images, 1=Grab images + * @param 'root'|'subpage' $grabimagesinto 'root' or 'subpage' * @return void */ function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modifylinks = 0, $grabimages = 1, $grabimagesinto = 'subpage') diff --git a/htdocs/website/index.php b/htdocs/website/index.php index a96eeb73516..beb7826fa35 100644 --- a/htdocs/website/index.php +++ b/htdocs/website/index.php @@ -764,7 +764,7 @@ if ($action == 'addcontainer' && $usercanedit) { if (GETPOSTISSET('fetchexternalurl')) { // Fetch from external url $urltograb = GETPOST('externalurl', 'alpha'); - $grabimages = GETPOST('grabimages', 'alpha'); + $grabimages = GETPOSTINT('grabimages') ? 1 : 0; $grabimagesinto = GETPOST('grabimagesinto', 'alpha'); include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php'; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 8b9a9198ce2..6e7a45eb06f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -66,13 +66,16 @@ parameters: - '#expects \(callable#' - '#expects bool\|string#' - '#expects bool, int#' - - '#expects bool, string given.#' - - '#expects int\|string#' - - '#expects int\|null#' + - '# (dol_getdate|dol_concatdesc|vatrate) expects bool, string given.#' + - '# SMTPs::(getFrom|getErrorsTo|getReplyTo)\(\) expects bool, string given.#' + - '# (dolibarr_set_const|yn|getLocalTaxesFromRate) expects int\|string#' + - '#::(options)\(\) expects int\|string#' + - '# (print_barre_liste|getCurrentPeriodOfFiscalYear|dol_substr|get_next_value) expects int\|null#' - '#color.* expects int, string#' - '#imap.* expects int, string#' - '#convert.* expects int, string#' - - '#\(\) expects int, string#' + - '# (Setup|Asset|Proposals|Orders|Invoices|FactureRec|BonPrelevement|Products|Thirdparties)::_fetch\S*\(\) expects int, string#' + - '#(getFullName|sendTicketMessageByEmail|addItemsFact|update_price|setPhoneNumber|recur|addDepreciationLine|addline|generate|buildRumNumber|update|update_label|getSelectConditionsPaiements|select_state|listBoxes|addLineRec|literalBarcodeType)\(\) expects int, string#' - '#run_sql expects int, string#' - '#on array{url: mixed} in empty\(\) does not exist.#' - '#dol_copy expects string, int given#' @@ -80,25 +83,51 @@ parameters: - '#EvalMath::trigger\(\) expects string, int given#' - '#get_next_value expects string, int given#' - '#pdf_writeLinkedObjects expects string, int given#' + - '# Diff::generatePartialDiff\(\) expects array#' + - '# EmailCollector::getpart\(\) expects string#' - '#expects int, float#' - - '#expects int, array\|string given.#' + - '#::saveboxorder\(\) expects int, array#' + - '# (print_projecttasks(_array)?|dol_time_plus_duree|get_default_tva|get_default_npr) expects int, array#' + - '# (fetchObjectByElement|print_actions_filter|dol_mktime|dol_get_first_day|dol_get_last_day|dol_remove_file_process|displayUsersListWithPicto) expects int, array\|string given.#' + - '# (CSMSFile) constructor expects int, array\|string given.#' - "# (img_picto) expects int<0, 1>, (false|'') given.#" - '#expects float\|string#' - '#expects float\|null#' - '#expects float, string given.#' - '#expects string\|null,#' - - '#expects string, \(float\|int\) given.#' - - '#expects string, (float(\|(int|array))(\<.*\>)?)+ given.#' - - '#expects string, (false|true|null|DateTime|float|array(<.*>)?) given.#' + - '# (envoi_mail|sendEmailTo) expects string, \(float\|int\) given.#' + - '#::printStdColumnContent\(\) expects string, float(\|(int|array)(\<.*\>)?)* given.#' + - '#::HTML2OpenIDServer\(\) expects string, array given.#' + - '# (dol_strlen|vatrate) expects string, float(\|(int|array)(\<.*\>)?)* given.#' + - '# dol_escape_htmltag expects string, float given.#' + - '#create_line\(\) expects string, float given.#' + - '# dol_stringtotime expects string, DateTime given.#' + - '# (load_fiche_titre|llxHeader|dol_add_file_process|setEventMessages) expects string, null given.#' + - '# (show_ldap_content|projectLinesa) expects string, true given.#' + - '# DolEditor constructor expects string, false given.#' + - '# dol_eval expects string, array given.#' - '#expects string, bool\|mysqli_result\|resource given.#' - - '#expects array\{#' - - '#expects array