From 747820da7cb36f86302ea6e55140a6d29fd36506 Mon Sep 17 00:00:00 2001 From: thibdrev Date: Sat, 20 Jan 2024 21:29:46 +0100 Subject: [PATCH 1/8] qual: phpstan htdocs/fourn/facture/card-rec.php 197 Property FactureFournisseurRec::$fk_project (int) does not accept array|string. htdocs/fourn/facture/card-rec.php 389 Property CommonObjectLine::$id (int) does not accept array|string. --- htdocs/fourn/facture/card-rec.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/fourn/facture/card-rec.php b/htdocs/fourn/facture/card-rec.php index d196d562ba6..3c505e20bc5 100644 --- a/htdocs/fourn/facture/card-rec.php +++ b/htdocs/fourn/facture/card-rec.php @@ -62,7 +62,7 @@ $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("pa // Security check $id = (GETPOST('facid', 'int') ? GETPOST('facid', 'int') : GETPOST('id', 'int')); -$lineid = GETPOST('lineid', 'int'); +$lineid = GETPOSTINT('lineid'); $title = GETPOST('title', 'alpha'); $libelle = GETPOST('libelle', 'alpha'); $ref_supplier = GETPOST('ref_supplier', 'alpha'); @@ -194,7 +194,7 @@ if (empty($reshook)) { $object->title = GETPOST('title', 'alphanohtml'); $object->libelle = GETPOST('libelle', 'alpha'); // deprecated $object->label = GETPOST('libelle', 'alpha'); - $object->fk_project = GETPOST('projectid', 'int'); + $object->fk_project = GETPOSTINT('projectid'); $object->ref_supplier = GETPOST('ref_supplier', 'alphanohtml'); $object->note_private = GETPOST('note_private', 'restricthtml'); From 54adf6a28541531d0b41fe0158c4b3b5c021384c Mon Sep 17 00:00:00 2001 From: thibdrev Date: Sat, 20 Jan 2024 21:43:27 +0100 Subject: [PATCH 2/8] qual: phpstan htdocs/holiday/card_group.php 310 Property Holiday::$fk_validator (int) does not accept array|string. --- htdocs/holiday/card_group.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 9c7bc6bbeeb..e7ce2fd79d2 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -183,7 +183,7 @@ if (empty($reshook)) { $halfday = 1; } - $approverid = GETPOST('valideur', 'int'); + $approverid = GETPOSTINT('valideur'); $description = trim(GETPOST('description', 'restricthtml')); // Check that leave is for a user inside the hierarchy or advanced permission for all is set From 2ea1254c6b89dd575c67616bcc2c17b4d7a8a7be Mon Sep 17 00:00:00 2001 From: thibdrev Date: Sat, 20 Jan 2024 21:46:38 +0100 Subject: [PATCH 3/8] qual: phpstn htdocs/loan/card.php 135 Property Loan::$fk_bank (int) does not accept array|string. htdocs/loan/card.php 143 Property Loan::$fk_project (int) does not accept array|string. --- htdocs/loan/card.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/loan/card.php b/htdocs/loan/card.php index 3a19d530054..b9f5f672bc2 100644 --- a/htdocs/loan/card.php +++ b/htdocs/loan/card.php @@ -132,7 +132,7 @@ if (empty($reshook)) { if (!$error) { $object->label = GETPOST('label'); - $object->fk_bank = GETPOST('accountid'); + $object->fk_bank = GETPOSTINT('accountid'); $object->capital = $capital; $object->datestart = $datestart; $object->dateend = $dateend; @@ -140,7 +140,7 @@ if (empty($reshook)) { $object->rate = $rate; $object->note_private = GETPOST('note_private', 'restricthtml'); $object->note_public = GETPOST('note_public', 'restricthtml'); - $object->fk_project = GETPOST('projectid', 'int'); + $object->fk_project = GETPOSTINT('projectid'); $object->insurance_amount = GETPOST('insurance_amount', 'int'); $accountancy_account_capital = GETPOST('accountancy_account_capital'); From 8e69be7f8d2d6347a208a6864bd36f853964307c Mon Sep 17 00:00:00 2001 From: thibdrev Date: Sat, 20 Jan 2024 21:55:24 +0100 Subject: [PATCH 4/8] qual: phpstan htdocs/product/card.php 578 Property CommonObject::$barcode_type (int) does not accept array|string. htdocs/product/card.php 820 Property CommonObject::$barcode_type (int) does not accept array|string. --- htdocs/product/card.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/product/card.php b/htdocs/product/card.php index a3c487bff35..6e9d0b6ed82 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -575,7 +575,7 @@ if (empty($reshook)) { // Set barcode_type_xxx from barcode_type id $stdobject = new GenericObject($db); $stdobject->element = 'product'; - $stdobject->barcode_type = GETPOST('fk_barcode_type'); + $stdobject->barcode_type = GETPOSTINT('fk_barcode_type'); $result = $stdobject->fetch_barcode(); if ($result < 0) { $error++; @@ -817,7 +817,7 @@ if (empty($reshook)) { // Set barcode_type_xxx from barcode_type id $stdobject = new GenericObject($db); $stdobject->element = 'product'; - $stdobject->barcode_type = GETPOST('fk_barcode_type'); + $stdobject->barcode_type = GETPOSTINT('fk_barcode_type'); $result = $stdobject->fetch_barcode(); if ($result < 0) { $error++; From b4bb255f046847adeb494625d2429130859eb4a8 Mon Sep 17 00:00:00 2001 From: thibdrev Date: Sat, 20 Jan 2024 22:00:38 +0100 Subject: [PATCH 5/8] qual:phpstan htdocs/product/class/product.class.php 666 Property Product::$price_ttc (float) does not accept string. htdocs/product/class/product.class.php 667 Property Product::$price (float) does not accept string. htdocs/product/class/product.class.php 668 Property Product::$price_min_ttc (float) does not accept string. htdocs/product/class/product.class.php 669 Property Product::$price_min (float) does not accept string. htdocs/product/class/product.class.php 2404 Property Product::$price (float) does not accept string. htdocs/product/class/product.class.php 2405 Property Product::$price_ttc (float) does not accept string. htdocs/product/class/product.class.php 2863 Property Product::$price_ttc (float) does not accept string. --- htdocs/product/class/product.class.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 26890169fb8..b4ad60ec516 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -663,10 +663,10 @@ class Product extends CommonObject $this->ref = dol_sanitizeFileName(dol_string_nospecial(trim($this->ref))); } $this->label = trim($this->label); - $this->price_ttc = price2num($this->price_ttc); - $this->price = price2num($this->price); - $this->price_min_ttc = price2num($this->price_min_ttc); - $this->price_min = price2num($this->price_min); + $this->price_ttc = (float) price2num($this->price_ttc); + $this->price = (float) price2num($this->price); + $this->price_min_ttc = (float) price2num($this->price_min_ttc); + $this->price_min = (float) price2num($this->price_min); if (empty($this->tva_tx)) { $this->tva_tx = 0; } @@ -4542,7 +4542,7 @@ class Product extends CommonObject * * @param int $fk_parent Id of parent kit product * @param int $fk_child Id of child product - * @return int Return integer <0 if KO, >0 if OK + * @return bool|int Return true or false ; -1 if error */ public function is_sousproduit($fk_parent, $fk_child) { From 39bbe766b56e55de3689c48879aa27c7d8797d97 Mon Sep 17 00:00:00 2001 From: MDW Date: Sun, 21 Jan 2024 13:29:17 +0100 Subject: [PATCH 6/8] Qual: codespell: Limit adding exceptions to git files only addCodespellIgnores.sh was applied on all files, now limit to git controlled files only. --- dev/tools/codespell/addCodespellIgnores.sh | 16 ++++++++++++---- dev/tools/codespell/codespell-dict.txt | 9 +++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/dev/tools/codespell/addCodespellIgnores.sh b/dev/tools/codespell/addCodespellIgnores.sh index 0b73c4c16b2..00a93e1a03b 100755 --- a/dev/tools/codespell/addCodespellIgnores.sh +++ b/dev/tools/codespell/addCodespellIgnores.sh @@ -1,4 +1,6 @@ #!/bin/bash +# Copyright (C) 2024 MDW + # # Script to add codespell exceptions to the ignores lines file. # @@ -18,7 +20,7 @@ # # :warning: # -# This script only works properly if codespell is install for your CLI. +# This script only works properly if codespell is installed for your CLI. # As the configuration is in pyproject.toml, you also need tomli. # # ```shell @@ -50,11 +52,17 @@ fi [ -r "${codespell_ignore_file}" ] || { echo "${codespell_ignore_file} not found" ; exit 1 ; } # Then: # - Run codespell; +# - Identify files that have fixes; +# - Limit to files under git control; +# - Run codespell on selected files; # - For each line, create a grep command to find the lines; # - Execute that command by evaluation -codespell . | sed -n -E 's@^([^:]+):[[:digit:]]+:[[:space:]](\S+)[[:space:]].*@grep -P '\''\\b\2\\b'\'' "\1" >> '"${codespell_ignore_file}"'@p' | \ - while read -r line ; do eval "$line" ; done +codespell . \ + | sed -n -E 's@^([^:]+)@\1@p' \ + | xargs -r git ls-files -- \ + | xargs -r codespell -- \ + | sed -n -E 's@^([^:]+):[[:digit:]]+:[[:space:]](\S+)[[:space:]].*@grep -P '\''\\b\2\\b'\'' -- "\1" >> '"${codespell_ignore_file}"'@p' \ + | while read -r line ; do eval "$line" ; done # Finally, sort and remove duplicates to make merges easier. sort -u -o "${codespell_ignore_file}"{,} - diff --git a/dev/tools/codespell/codespell-dict.txt b/dev/tools/codespell/codespell-dict.txt index fbab9df6c3c..e5f9ed51f86 100644 --- a/dev/tools/codespell/codespell-dict.txt +++ b/dev/tools/codespell/codespell-dict.txt @@ -1,9 +1,10 @@ +# Please add in alphabetical order->(`sort -u` like). +EPR->ERP +alpahnohtml->alphanohtml +choosed->chosen dolibar->dolibarr dollibar->dolibarr dollibarr->dolibarr -not de passe->password mot de passe->password -choosed->chosen +not de passe->password tableau de bord->state board -#DoliDB->DoliDB -alpahnohtml->alphanohtml From 37620bcbbeb2a8c5a64c276eec950a533448a42c Mon Sep 17 00:00:00 2001 From: MDW Date: Sat, 20 Jan 2024 22:11:22 +0100 Subject: [PATCH 7/8] NEW: dev: Add tool to update licence with developer info This is a tool in php to update license information in the header base on the git user setting. --- .pre-commit-config.yaml | 9 +++ dev/tools/updatelicense.php | 154 ++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100755 dev/tools/updatelicense.php diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6fefa14ed25..490373ea632 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,6 +34,15 @@ repos: args: [--tab] # Run local script + # + # For instance to update the license in edited files, you could add to local.sh: + # + # ```shell + # #!/bin/bash + # MYDIR=$(dirname "$0") + # CHANGED_INTERNALS=$(git diff --name-only | grep -v includes) + # "$MYDIR/dev/tools/updatelicense.php" $CHANGED_INTERNALS + # ``` - repo: local hooks: - id: local-precommit-script diff --git a/dev/tools/updatelicense.php b/dev/tools/updatelicense.php new file mode 100755 index 00000000000..6c69785e7e0 --- /dev/null +++ b/dev/tools/updatelicense.php @@ -0,0 +1,154 @@ +#!/usr/bin/env php + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Script to update year periods and user/email in headers. + */ + +/** + * Retrieve Git user information + * + * @return array{name:string,email:string} + */ +function getGitUserInfo() +{ + $name = trim(shell_exec('git config user.name')); + $email = trim(shell_exec('git config user.email')); + return ['name' => $name, 'email' => $email]; +} + +const PREFIXES = [ + 'sh' => ['# ', '# ', '', '#!'], + 'php' => ['/* ', ' * ', ' */', '\d{4}))\s+{$r_name}\s*\<{$r_email}>~"; + + // Check if the lines match the pattern + if (preg_match($pattern, $lines, $matches)) { + $existingYear = $matches['year']; + + // Check if the existing year is different from the current year + if ($existingYear !== date('Y')) { + // Update the year range to include or be up to the current year + $updatedNotice = preg_replace('/(\d{4})(-\d{4})?\s+/', $existingYear . '-' . date('Y') . "\t", $matches[0]); + + // Replace the old notice with the updated one in the file + file_put_contents($filename, preg_replace($pattern, $updatedNotice, file_get_contents($filename))); + return true; // Change detected + } + // If the existing year is the same, no need to update + } else { + // Adjust tabs for proper alignment + $emailTabs = str_repeat("\t", (int) (max(0, (31 - mb_strlen($name)) / 4))); + + // No match found, add a new line to the header + $newNotice = "Copyright (C) " . date('Y') . "\t\t" . $name . $emailTabs . "<" . $email . ">"; + + // Read the file content + $fileContent = file_get_contents($filename); + + // Check if there are existing copyright notices + $pos = max(strrpos($fileContent, "{$prefix0}Copyright"), strrpos($fileContent, "{$prefix1}Copyright")); + + if ($pos !== false) { + // Add the new notice behind the last preceding copyright notices + $pos = strpos($fileContent, "\n", $pos) + 1; + $fileContent = substr_replace($fileContent, $prefix1 . $newNotice . "\n", $pos, 0); + } elseif (strpos($fileContent, $prefix3) !== false) { + // Add the new notice after the shebang or ' [ ...]" . PHP_EOL; + exit(1); +} + +// Process each filename provided +$changesDetected = false; +for ($i = 1; $i < $argc; $i++) { + $filename = $argv[$i]; + + // Determine file type based on extension + $fileType = pathinfo($filename, PATHINFO_EXTENSION); + + // Retrieve Git user information + $gitUserInfo = getGitUserInfo(); + $name = $gitUserInfo['name']; + $email = $gitUserInfo['email']; + + // Update or add copyright notice based on file type + $changeDetected = updateCopyrightNotice($filename, $fileType, $name, $email); + $changesDetected |= $changeDetected; + if ($changeDetected) { + echo "Copyright notice updated in '$filename'" . PHP_EOL; + } +} + +if (!$changesDetected) { + echo "No changes needed in any file" . PHP_EOL; +} From e42700014a56fd4aa1a380daa6056ce4ba24edf9 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 21 Jan 2024 17:19:37 +0100 Subject: [PATCH 8/8] Update product.class.php --- htdocs/product/class/product.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index b4ad60ec516..5225a0ec57a 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -4542,7 +4542,7 @@ class Product extends CommonObject * * @param int $fk_parent Id of parent kit product * @param int $fk_child Id of child product - * @return bool|int Return true or false ; -1 if error + * @return int Return 1 or 0; -1 if error */ public function is_sousproduit($fk_parent, $fk_child) { @@ -4562,9 +4562,9 @@ class Product extends CommonObject $this->is_sousproduit_qty = $obj->qty; $this->is_sousproduit_incdec = $obj->incdec; - return true; + return 1; } else { - return false; + return 0; } } else { dol_print_error($this->db);