From 62d17a806b4add84596b19d4bb5b5812f571aa40 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Tue, 28 Mar 2023 14:26:24 +0100 Subject: [PATCH] PHPStan: some fixes to lib/ files (#3099) --- lib/Varien/Data/Tree/Dbp.php | 6 ++- lib/Varien/Db/Adapter/Interface.php | 3 +- lib/Varien/Db/Adapter/Mysqli.php | 28 ++++++----- lib/Varien/Db/Adapter/Pdo/Mysql.php | 2 +- lib/Varien/Debug.php | 2 +- lib/Varien/Image/Adapter/Abstract.php | 2 +- lib/Varien/Image/Adapter/Gd2.php | 2 +- phpstan.dist.baseline.neon | 70 --------------------------- 8 files changed, 26 insertions(+), 89 deletions(-) diff --git a/lib/Varien/Data/Tree/Dbp.php b/lib/Varien/Data/Tree/Dbp.php index b36a5b14e7e..8b66c0e5b59 100644 --- a/lib/Varien/Data/Tree/Dbp.php +++ b/lib/Varien/Data/Tree/Dbp.php @@ -107,7 +107,9 @@ public function __construct($connection, $table, $fields) $this->_orderField = $fields[self::ORDER_FIELD]; $this->_levelField = $fields[self::LEVEL_FIELD]; - $this->_select = $this->_conn->select(); + /** @var Varien_Db_Select $select */ + $select = $this->_conn->select(); + $this->_select = $select; $this->_select->from($this->_table); } @@ -158,7 +160,7 @@ public function load($parentNode = null, $recursionLevel = 0) $parentNode = null; } elseif (is_string($parentNode)) { $parentPath = $parentNode; - $startLevel = count(explode($parentPath)) - 1; + $startLevel = count(explode('/', $parentPath)) - 1; $parentNode = null; } diff --git a/lib/Varien/Db/Adapter/Interface.php b/lib/Varien/Db/Adapter/Interface.php index c289755b8d6..3f8dad9b447 100644 --- a/lib/Varien/Db/Adapter/Interface.php +++ b/lib/Varien/Db/Adapter/Interface.php @@ -792,8 +792,7 @@ public function getCaseSql($valueName, $casesResults, $defaultValue = null); * @param string $value OPTIONAL. Applies when $expression is NULL * @return Zend_Db_Expr */ - - public function getIfNullSql($expression, $value = 0); + public function getIfNullSql($expression, $value = '0'); /** * Generate fragment of SQL, that combine together (concatenate) the results from data array diff --git a/lib/Varien/Db/Adapter/Mysqli.php b/lib/Varien/Db/Adapter/Mysqli.php index 95fc0f81269..9a954fa2bf6 100644 --- a/lib/Varien/Db/Adapter/Mysqli.php +++ b/lib/Varien/Db/Adapter/Mysqli.php @@ -43,7 +43,7 @@ protected function _connect() // Suppress connection warnings here. // Throw an exception instead. @$conn = new mysqli(); - if (false === $conn || mysqli_connect_errno()) { + if (mysqli_connect_errno()) { throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_errno()); } @@ -61,7 +61,7 @@ protected function _connect() list($this->_config['host'], $port) = explode(':', $this->_config['host']); } - @$conn->real_connect( + $connectionSuccessful = @$conn->real_connect( $this->_config['host'], $this->_config['username'], $this->_config['password'], @@ -69,7 +69,7 @@ protected function _connect() $port, $socket ); - if (mysqli_connect_errno()) { + if (!$connectionSuccessful) { throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error()); } @@ -83,7 +83,7 @@ protected function _connect() * Run RAW Query * * @param string $sql - * @return Zend_Db_Statement_Interface + * @return mysqli_result */ // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps public function raw_query($sql) @@ -94,7 +94,9 @@ public function raw_query($sql) $retry = false; try { $this->clear_result(); - $result = $this->getConnection()->query($sql); + /** @var mysqli $connection */ + $connection = $this->getConnection(); + $result = $connection->query($sql); $this->clear_result(); } catch (Exception $e) { if ($tries < 10 && $e->getMessage() == $timeoutMessage) { @@ -147,11 +149,13 @@ public function multi_query($sql) $this->beginTransaction(); try { $this->clear_result(); - if ($this->getConnection()->multi_query($sql)) { + /** @var mysqli $connection */ + $connection = $this->getConnection(); + if ($connection->multi_query($sql)) { $this->clear_result(); $this->commit(); } else { - throw new Zend_Db_Adapter_Mysqli_Exception('multi_query: ' . $this->getConnection()->error); + throw new Zend_Db_Adapter_Mysqli_Exception('multi_query: ' . $connection->error); } } catch (Exception $e) { $this->rollBack(); @@ -164,11 +168,13 @@ public function multi_query($sql) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps public function clear_result() { - while ($this->getConnection()->next_result()) { - if ($result = $this->getConnection()->store_result()) { + /** @var mysqli $connection */ + $connection = $this->getConnection(); + while ($connection->next_result()) { + if ($result = $connection->store_result()) { $result->free_result(); - } elseif ($this->getConnection()->error) { - throw new Zend_Db_Adapter_Mysqli_Exception('clear_result: ' . $this->getConnection()->error); + } elseif ($connection->error) { + throw new Zend_Db_Adapter_Mysqli_Exception('clear_result: ' . $connection->error); } } } diff --git a/lib/Varien/Db/Adapter/Pdo/Mysql.php b/lib/Varien/Db/Adapter/Pdo/Mysql.php index 938da576304..534b0dcfa69 100644 --- a/lib/Varien/Db/Adapter/Pdo/Mysql.php +++ b/lib/Varien/Db/Adapter/Pdo/Mysql.php @@ -3209,7 +3209,7 @@ public function getCheckSql($expression, $true, $false) * @param string|int $value OPTIONAL. Applies when $expression is NULL * @return Zend_Db_Expr */ - public function getIfNullSql($expression, $value = 0) + public function getIfNullSql($expression, $value = '0') { if ($expression instanceof Zend_Db_Expr || $expression instanceof Zend_Db_Select) { $expression = sprintf("IFNULL((%s), %s)", $expression, $value); diff --git a/lib/Varien/Debug.php b/lib/Varien/Debug.php index 93242d16d25..d89b7b60d6a 100644 --- a/lib/Varien/Debug.php +++ b/lib/Varien/Debug.php @@ -182,7 +182,7 @@ protected static function _formatCalledArgument($arg) } } elseif (is_null($arg)) { $out .= 'NULL'; - } elseif (is_numeric($arg) || is_float($arg)) { + } elseif (is_numeric($arg)) { $out .= $arg; } elseif (is_string($arg)) { if (strlen($arg) > self::$argLength) { diff --git a/lib/Varien/Image/Adapter/Abstract.php b/lib/Varien/Image/Adapter/Abstract.php index 5eeb09e56aa..bcb9b454e75 100644 --- a/lib/Varien/Image/Adapter/Abstract.php +++ b/lib/Varien/Image/Adapter/Abstract.php @@ -306,7 +306,7 @@ public function quality($value = null) * Get/set keepBackgroundColor * * @param array $value - * @return array + * @return array|void */ public function backgroundColor($value = null) { diff --git a/lib/Varien/Image/Adapter/Gd2.php b/lib/Varien/Image/Adapter/Gd2.php index 18423d22dca..1abf6dd6f48 100644 --- a/lib/Varien/Image/Adapter/Gd2.php +++ b/lib/Varien/Image/Adapter/Gd2.php @@ -50,7 +50,7 @@ public function __construct() */ public function destruct() { - if (is_resource($this->_imageHandler) || $this->_imageHandler instanceof \GdImage) { + if (is_resource($this->_imageHandler) || (class_exists('GdImage') && $this->_imageHandler instanceof \GdImage)) { @imagedestroy($this->_imageHandler); } } diff --git a/phpstan.dist.baseline.neon b/phpstan.dist.baseline.neon index f8ecf218a2d..a15c7e8d017 100644 --- a/phpstan.dist.baseline.neon +++ b/phpstan.dist.baseline.neon @@ -4925,66 +4925,11 @@ parameters: count: 1 path: lib/Varien/Data/Form/Element/Multiline.php - - - message: "#^Function explode invoked with 1 parameter, 2\\-3 required\\.$#" - count: 1 - path: lib/Varien/Data/Tree/Dbp.php - - - - message: "#^Property Varien_Data_Tree_Dbp\\:\\:\\$_select \\(Varien_Db_Select\\) does not accept Zend_Db_Select\\.$#" - count: 1 - path: lib/Varien/Data/Tree/Dbp.php - - message: "#^Method Varien_Data_Tree\\:\\:load\\(\\) invoked with 2 parameters, 0\\-1 required\\.$#" count: 1 path: lib/Varien/Data/Tree/Node.php - - - message: "#^Default value of the parameter \\#2 \\$value \\(int\\) of method Varien_Db_Adapter_Interface\\:\\:getIfNullSql\\(\\) is incompatible with type string\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Interface.php - - - - message: "#^Call to an undefined method Zend_Db_Statement_Interface\\:\\:fetch_assoc\\(\\)\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Mysqli.php - - - - message: "#^Cannot access property \\$error on object\\|resource\\.$#" - count: 3 - path: lib/Varien/Db/Adapter/Mysqli.php - - - - message: "#^Cannot call method multi_query\\(\\) on object\\|resource\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Mysqli.php - - - - message: "#^Cannot call method next_result\\(\\) on object\\|resource\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Mysqli.php - - - - message: "#^Cannot call method query\\(\\) on object\\|resource\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Mysqli.php - - - - message: "#^Cannot call method store_result\\(\\) on object\\|resource\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Mysqli.php - - - - message: "#^If condition is always false\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Mysqli.php - - - - message: "#^Strict comparison using \\=\\=\\= between false and mysqli will always evaluate to false\\.$#" - count: 1 - path: lib/Varien/Db/Adapter/Mysqli.php - - message: "#^Cannot access offset 'Engine' on bool\\.$#" count: 1 @@ -5060,11 +5005,6 @@ parameters: count: 1 path: lib/Varien/Db/Tree/NodeSet.php - - - message: "#^Call to function is_float\\(\\) with mixed will always evaluate to false\\.$#" - count: 1 - path: lib/Varien/Debug.php - - message: "#^Call to an undefined method Varien_File_Uploader_Image\\:\\:newUploader\\(\\)\\.$#" count: 1 @@ -5095,21 +5035,11 @@ parameters: count: 1 path: lib/Varien/Http/Adapter/Curl.php - - - message: "#^Method Varien_Image_Adapter_Abstract\\:\\:backgroundColor\\(\\) should return array but empty return statement found\\.$#" - count: 2 - path: lib/Varien/Image/Adapter/Abstract.php - - message: "#^Property Varien_Image_Adapter_Abstract\\:\\:\\$_imageHandler has unknown class GdImage as its type\\.$#" count: 1 path: lib/Varien/Image/Adapter/Abstract.php - - - message: "#^Class GdImage not found\\.$#" - count: 1 - path: lib/Varien/Image/Adapter/Gd2.php - - message: "#^Call to an undefined method SimpleXMLElement\\:\\:setNode\\(\\)\\.$#" count: 1