From 243fcd59222d87bb3fecd8f05c0b8a7f91aef1f1 Mon Sep 17 00:00:00 2001 From: leemyongpakvn Date: Sun, 19 Jun 2022 07:45:15 +0700 Subject: [PATCH 1/5] Simplify Type casting 1. Assign $id_xyz = (int) $id_xyz once at top, instead of using (int) $id_xyz throughout the function 2. if Validate::isUnsignedId($id_xyz ) then there is no need to type cast (int) ($id_xyz) --- ProductComment.php | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/ProductComment.php b/ProductComment.php index bfd38822..518cf8d6 100644 --- a/ProductComment.php +++ b/ProductComment.php @@ -93,8 +93,10 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer return false; } $validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'); + $id_product = (int) $id_product; $p = (int) $p; $n = (int) $n; + $id_customer = (int) $id_customer; if ($p <= 1) { $p = 1; } @@ -102,20 +104,20 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer $n = 5; } - $cache_id = 'ProductComment::getByProduct_' . (int) $id_product . '-' . (int) $p . '-' . (int) $n . '-' . (int) $id_customer . '-' . (bool) $validate; + $cache_id = 'ProductComment::getByProduct_' . $id_product . '-' . $p . '-' . $n . '-' . $id_customer . '-' . $validate; if (!Cache::isStored($cache_id)) { $result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS(' SELECT pc.`id_product_comment`, - (SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful, - (SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, ' . - ((int) $id_customer ? '(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = ' . (int) $id_customer . ') as customer_advice, ' : '') . - ((int) $id_customer ? '(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = ' . (int) $id_customer . ') as customer_report, ' : '') . ' + (SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) AS total_useful, + (SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) AS total_advice, ' . + ($id_customer ? '(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = ' . $id_customer . ') AS customer_advice, ' : '') . + ($id_customer ? '(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = ' . $id_customer . ') AS customer_report, ' : '') . ' IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title FROM `' . _DB_PREFIX_ . 'product_comment` pc LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON c.`id_customer` = pc.`id_customer` - WHERE pc.`id_product` = ' . (int) ($id_product) . ($validate ? ' AND pc.`validate` = 1' : '') . ' + WHERE pc.`id_product` = ' . $id_product . ($validate ? ' AND pc.`validate` = 1' : '') . ' ORDER BY pc.`date_add` DESC - ' . ($n ? 'LIMIT ' . (int) (($p - 1) * $n) . ', ' . (int) ($n) : '')); + ' . ($n ? 'LIMIT ' . (($p - 1) * $n) . ', ' . $n : '')); Cache::store($cache_id, $result); } @@ -169,8 +171,8 @@ public static function getGradeByProduct($id_product, $id_lang) LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_grade` pcg ON (pcg.`id_product_comment` = pc.`id_product_comment`) LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion` pcc ON (pcc.`id_product_comment_criterion` = pcg.`id_product_comment_criterion`) LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_lang` pccl ON (pccl.`id_product_comment_criterion` = pcg.`id_product_comment_criterion`) - WHERE pc.`id_product` = ' . (int) $id_product . ' - AND pccl.`id_lang` = ' . (int) $id_lang . + WHERE pc.`id_product` = ' . $id_product . ' + AND pccl.`id_lang` = ' . $id_lang . ($validate ? ' AND pc.`validate` = 1' : '')); } @@ -241,12 +243,12 @@ public static function getCommentNumber($id_product) return false; } $validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'); - $cache_id = 'ProductComment::getCommentNumber_' . (int) $id_product . '-' . $validate; + $cache_id = 'ProductComment::getCommentNumber_' . $id_product . '-' . $validate; if (!Cache::isStored($cache_id)) { $result = (int) Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue(' SELECT COUNT(`id_product_comment`) AS "nbr" FROM `' . _DB_PREFIX_ . 'product_comment` pc - WHERE `id_product` = ' . (int) ($id_product) . ($validate ? ' AND `validate` = 1' : '')); + WHERE `id_product` = ' . $id_product . ($validate ? ' AND `validate` = 1' : '')); Cache::store($cache_id, (string) $result); } @@ -268,7 +270,7 @@ public static function getGradedCommentNumber($id_product) $result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getRow(' SELECT COUNT(pc.`id_product`) AS nbr FROM `' . _DB_PREFIX_ . 'product_comment` pc - WHERE `id_product` = ' . (int) ($id_product) . ($validate == '1' ? ' AND `validate` = 1' : '') . ' + WHERE `id_product` = ' . $id_product . ($validate == '1' ? ' AND `validate` = 1' : '') . ' AND `grade` > 0'); return (int) ($result['nbr']); @@ -348,7 +350,7 @@ public function validate($validate = '1') $success = (Db::getInstance()->execute(' UPDATE `' . _DB_PREFIX_ . 'product_comment` SET `validate` = ' . (int) $validate . ' - WHERE `id_product_comment` = ' . (int) $this->id)); + WHERE `id_product_comment` = ' . $this->id)); Hook::exec('actionObjectProductCommentValidateAfter', ['object' => $this]); @@ -381,7 +383,7 @@ public static function deleteGrades($id_product_comment) return Db::getInstance()->execute(' DELETE FROM `' . _DB_PREFIX_ . 'product_comment_grade` - WHERE `id_product_comment` = ' . (int) $id_product_comment); + WHERE `id_product_comment` = ' . $id_product_comment); } /** @@ -397,7 +399,7 @@ public static function deleteReports($id_product_comment) return Db::getInstance()->execute(' DELETE FROM `' . _DB_PREFIX_ . 'product_comment_report` - WHERE `id_product_comment` = ' . (int) $id_product_comment); + WHERE `id_product_comment` = ' . $id_product_comment); } /** @@ -413,7 +415,7 @@ public static function deleteUsefulness($id_product_comment) return Db::getInstance()->execute(' DELETE FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` - WHERE `id_product_comment` = ' . (int) $id_product_comment); + WHERE `id_product_comment` = ' . $id_product_comment); } /** From 2953c4c71a32d812fa0acf05a45910f61547dc6b Mon Sep 17 00:00:00 2001 From: leemyongpakvn Date: Mon, 20 Jun 2022 07:44:49 +0700 Subject: [PATCH 2/5] Remove redandunt type caft left by previous commit if Validate::isUnsignedId($id_product) then there is no need to type cast (int) $id_product --- ProductComment.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ProductComment.php b/ProductComment.php index 518cf8d6..e4c2a070 100644 --- a/ProductComment.php +++ b/ProductComment.php @@ -92,8 +92,7 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer if (!Validate::isUnsignedId($id_product)) { return false; } - $validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'); - $id_product = (int) $id_product; + $validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'); $p = (int) $p; $n = (int) $n; $id_customer = (int) $id_customer; From 67342176bfac0e20a6e16359742563f5eabf90fe Mon Sep 17 00:00:00 2001 From: leemyongpakvn Date: Mon, 20 Jun 2022 17:33:27 +0700 Subject: [PATCH 3/5] Update ProductComment.php Co-authored-by: Matthieu Rolland --- ProductComment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProductComment.php b/ProductComment.php index e4c2a070..e2db3a7d 100644 --- a/ProductComment.php +++ b/ProductComment.php @@ -92,7 +92,7 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer if (!Validate::isUnsignedId($id_product)) { return false; } - $validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'); + $validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'); $p = (int) $p; $n = (int) $n; $id_customer = (int) $id_customer; From 285bd631bdc4277fbe34666e726f2ca53d7e10e5 Mon Sep 17 00:00:00 2001 From: leemyongpakvn Date: Fri, 24 Jun 2022 19:45:13 +0700 Subject: [PATCH 4/5] Update ProductCommentCriterion.php --- ProductCommentCriterion.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ProductCommentCriterion.php b/ProductCommentCriterion.php index 7c400468..e1c0e538 100644 --- a/ProductCommentCriterion.php +++ b/ProductCommentCriterion.php @@ -105,7 +105,7 @@ public function addProduct($id_product) return Db::getInstance()->execute(' INSERT INTO `' . _DB_PREFIX_ . 'product_comment_criterion_product` (`id_product_comment_criterion`, `id_product`) - VALUES(' . (int) $this->id . ',' . (int) $id_product . ') + VALUES(' . (int) $this->id . ',' . $id_product . ') '); } @@ -122,7 +122,7 @@ public function addCategory($id_category) return Db::getInstance()->execute(' INSERT INTO `' . _DB_PREFIX_ . 'product_comment_criterion_category` (`id_product_comment_criterion`, `id_category`) - VALUES(' . (int) $this->id . ',' . (int) $id_category . ') + VALUES(' . (int) $this->id . ',' . $id_category . ') '); } @@ -145,7 +145,7 @@ public function addGrade($id_product_comment, $grade) return Db::getInstance()->execute(' INSERT INTO `' . _DB_PREFIX_ . 'product_comment_grade` (`id_product_comment`, `id_product_comment_criterion`, `grade`) VALUES( - ' . (int) ($id_product_comment) . ', + ' . $id_product_comment . ', ' . (int) $this->id . ', ' . (int) ($grade) . ')'); } @@ -169,7 +169,7 @@ public static function getByProduct($id_product, $id_lang) $alias = 'ps'; } - $cache_id = 'ProductCommentCriterion::getByProduct_' . (int) $id_product . '-' . (int) $id_lang; + $cache_id = 'ProductCommentCriterion::getByProduct_' . $id_product . '-' . $id_lang; if (!Cache::isStored($cache_id)) { $result = Db::getInstance()->executeS(' SELECT pcc.`id_product_comment_criterion`, pccl.`name` @@ -177,12 +177,12 @@ public static function getByProduct($id_product, $id_lang) LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_lang` pccl ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion) LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_product` pccp - ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = ' . (int) $id_product . ') + ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = ' . $id_product . ') LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_category` pccc ON (pcc.`id_product_comment_criterion` = pccc.`id_product_comment_criterion`) LEFT JOIN `' . _DB_PREFIX_ . 'product' . $table . '` ' . $alias . ' - ON (' . $alias . '.id_category_default = pccc.id_category AND ' . $alias . '.id_product = ' . (int) $id_product . ') - WHERE pccl.`id_lang` = ' . (int) ($id_lang) . ' + ON (' . $alias . '.id_category_default = pccc.id_category AND ' . $alias . '.id_product = ' . $id_product . ') + WHERE pccl.`id_lang` = ' . $id_lang . ' AND ( pccp.id_product IS NOT NULL OR ps.id_product IS NOT NULL @@ -212,7 +212,7 @@ public static function getCriterions($id_lang, $type = false, $active = false) SELECT pcc.`id_product_comment_criterion`, pcc.id_product_comment_criterion_type, pccl.`name`, pcc.active FROM `' . _DB_PREFIX_ . 'product_comment_criterion` pcc JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_lang` pccl ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion) - WHERE pccl.`id_lang` = ' . (int) $id_lang . ($active ? ' AND active = 1' : '') . ($type ? ' AND id_product_comment_criterion_type = ' . (int) $type : '') . ' + WHERE pccl.`id_lang` = ' . $id_lang . ($active ? ' AND active = 1' : '') . ($type ? ' AND id_product_comment_criterion_type = ' . (int) $type : '') . ' ORDER BY pccl.`name` ASC'; $criterions = Db::getInstance()->executeS($sql); From 4cde940f675a1116c603fd3a761c3b55ae370d25 Mon Sep 17 00:00:00 2001 From: leemyongpakvn Date: Wed, 6 Jul 2022 12:49:34 +0700 Subject: [PATCH 5/5] cs-fixer --- ProductComment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProductComment.php b/ProductComment.php index e2db3a7d..70de252f 100644 --- a/ProductComment.php +++ b/ProductComment.php @@ -95,7 +95,7 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer $validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE'); $p = (int) $p; $n = (int) $n; - $id_customer = (int) $id_customer; + $id_customer = (int) $id_customer; if ($p <= 1) { $p = 1; }