Skip to content

Commit 62cedd7

Browse files
fballianokiatng
andcommitted
Some PHPStan fixes (#3031)
* PHPStan fix in app/Mage.php * Solved PHPStan problems with lib/Varien/Object/Cache.php * PHPStan fixes for lib/Varien/Object/Mapper.php * PHPStan fixes for lib/Varien/Io/File.php * Fixed PHPStan error for lib/Varien/Image/Adapter/Gd2.php * Fixed signature for getSingleton * removed what this PR fixed * Update lib/Varien/Io/File.php * Update lib/Varien/Object/Cache.php * seems not necessary anymore * seems not necessary anymore * updates * updates * updates * revert * trying to fix the baseline * trying to fix the baseline * failed comparisons * types * test type hint * typehint * nullable * fixes * fixes * small revert * small revert * better error message * Update app/code/core/Mage/Adminhtml/Block/System/Config/Form.php Co-authored-by: Ng Kiat Siong <[email protected]> * Update app/code/core/Mage/Paypal/Controller/Express/Abstract.php Co-authored-by: Ng Kiat Siong <[email protected]> * suggestion * Update app/code/core/Mage/Adminhtml/Block/System/Config/Form.php Co-authored-by: Ng Kiat Siong <[email protected]> * Exception added * small rewrite * phpcs --------- Co-authored-by: Ng Kiat Siong <[email protected]>
1 parent 34bec45 commit 62cedd7

File tree

13 files changed

+55
-144
lines changed

13 files changed

+55
-144
lines changed

app/Mage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public static function getModel($modelClass = '', $arguments = [])
526526
*
527527
* @param string $modelClass
528528
* @param array $arguments
529-
* @return Mage_Core_Model_Abstract
529+
* @return Mage_Core_Model_Abstract|false
530530
*/
531531
public static function getSingleton($modelClass = '', array $arguments = [])
532532
{

app/code/core/Mage/Adminhtml/Block/System/Config/Form.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ protected function _getDependence()
229229
* @param Varien_Simplexml_Element $section
230230
* @param string $fieldPrefix
231231
* @param string $labelPrefix
232+
* @throw Mage_Core_Exception
232233
* @return $this
233234
*/
234235
public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labelPrefix = '')
@@ -425,21 +426,30 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
425426
}
426427

427428
$sourceModel = Mage::getSingleton($factoryName);
429+
if (!$sourceModel) {
430+
Mage::throwException("Source model '{$factoryName}' is not found");
431+
}
428432
if ($sourceModel instanceof Varien_Object) {
429433
$sourceModel->setPath($path);
430434
}
435+
436+
$optionArray = [];
431437
if ($method) {
432438
if ($fieldType == 'multiselect') {
433439
$optionArray = $sourceModel->$method();
434440
} else {
435-
$optionArray = [];
436441
foreach ($sourceModel->$method() as $value => $label) {
437442
$optionArray[] = ['label' => $label, 'value' => $value];
438443
}
439444
}
440445
} else {
441-
$optionArray = $sourceModel->toOptionArray($fieldType == 'multiselect');
446+
if (method_exists($sourceModel, 'toOptionArray')) {
447+
$optionArray = $sourceModel->toOptionArray($fieldType == 'multiselect');
448+
} else {
449+
Mage::throwException("Missing method 'toOptionArray()' in source model '{$factoryName}'");
450+
}
442451
}
452+
443453
$field->setValues($optionArray);
444454
}
445455
}
@@ -637,9 +647,9 @@ public function getScope()
637647
*/
638648
public function getScopeLabel($element)
639649
{
640-
if ($element->show_in_store == 1) {
650+
if ((int)$element->show_in_store === 1) {
641651
return $this->_scopeLabels[self::SCOPE_STORES];
642-
} elseif ($element->show_in_website == 1) {
652+
} elseif ((int)$element->show_in_website === 1) {
643653
return $this->_scopeLabels[self::SCOPE_WEBSITES];
644654
}
645655
return $this->_scopeLabels[self::SCOPE_DEFAULT];

app/code/core/Mage/Catalog/Helper/Product/Flat.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,24 @@ class Mage_Catalog_Helper_Product_Flat extends Mage_Catalog_Helper_Flat_Abstract
8989
/**
9090
* Old Catalog Product Flat forced status
9191
*
92-
* @var bool
92+
* @var bool|null
9393
*/
9494
protected $_forceFlatStatusOld;
9595

9696
/**
9797
* Retrieve Catalog Product Flat Flag object
9898
*
9999
* @return Mage_Catalog_Model_Product_Flat_Flag
100+
* @throws Mage_Core_Exception
100101
*/
101102
public function getFlag()
102103
{
103104
if (is_null($this->_flagObject)) {
104105
$className = (string)Mage::getConfig()->getNode(self::XML_PATH_FLAT_FLAG);
105-
$this->_flagObject = Mage::getSingleton($className)
106-
->loadSelf();
106+
/** @var Mage_Catalog_Model_Product_Flat_Flag $classInstance */
107+
$classInstance = Mage::getSingleton($className);
108+
$this->_flagObject = $classInstance;
109+
$this->_flagObject->loadSelf();
107110
}
108111
return $this->_flagObject;
109112
}

app/code/core/Mage/CatalogIndex/Model/Retreiver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ protected function _construct()
8787
* Returns data retriever model by specified product type
8888
*
8989
* @param string $type
90-
* @return Mage_CatalogIndex_Model_Data_Abstract
90+
* @return Mage_CatalogIndex_Model_Data_Abstract|false
91+
* @throws Mage_Core_Exception
9192
*/
9293
public function getRetreiver($type)
9394
{

app/code/core/Mage/Oauth/controllers/Customer/TokenController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Mage_Oauth_Customer_TokenController extends Mage_Core_Controller_Front_Act
4040
/**
4141
* Customer session model
4242
*
43-
* @var Mage_Customer_Model_Session
43+
* @var string
4444
*/
4545
protected $_sessionName = 'customer/session';
4646

@@ -52,7 +52,9 @@ class Mage_Oauth_Customer_TokenController extends Mage_Core_Controller_Front_Act
5252
public function preDispatch()
5353
{
5454
parent::preDispatch();
55-
$this->_session = Mage::getSingleton($this->_sessionName);
55+
/** @var Mage_Customer_Model_Session $classInstance */
56+
$classInstance = Mage::getSingleton($this->_sessionName);
57+
$this->_session = $classInstance;
5658
if (!$this->_session->authenticate($this)) {
5759
$this->setFlag('', self::FLAG_NO_DISPATCH, true);
5860
}

app/code/core/Mage/Paypal/Controller/Express/Abstract.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class Mage_Paypal_Controller_Express_Abstract extends Mage_Core_Control
3939
protected $_config = null;
4040

4141
/**
42-
* @var Mage_Sales_Model_Quote
42+
* @var Mage_Sales_Model_Quote|false
4343
*/
4444
protected $_quote = false;
4545

@@ -64,7 +64,9 @@ abstract class Mage_Paypal_Controller_Express_Abstract extends Mage_Core_Control
6464
protected function _construct()
6565
{
6666
parent::_construct();
67-
$this->_config = Mage::getModel($this->_configType, [$this->_configMethod]);
67+
/** @var Mage_Paypal_Model_Config $classInstance */
68+
$classInstance = Mage::getModel($this->_configType, [$this->_configMethod]);
69+
$this->_config = $classInstance;
6870
}
6971

7072
/**
@@ -457,11 +459,13 @@ protected function _initCheckout()
457459
$this->getResponse()->setHeader('HTTP/1.1', '403 Forbidden');
458460
Mage::throwException(Mage::helper('paypal')->__('Unable to initialize Express Checkout.'));
459461
}
460-
$this->_checkout = Mage::getSingleton($this->_checkoutType, [
462+
463+
/** @var Mage_Paypal_Model_Express_Checkout $classInstance */
464+
$classInstance = Mage::getSingleton($this->_checkoutType, [
461465
'config' => $this->_config,
462466
'quote' => $quote,
463467
]);
464-
468+
$this->_checkout = $classInstance;
465469
return $this->_checkout;
466470
}
467471

@@ -470,7 +474,7 @@ protected function _initCheckout()
470474
* Combined getter/setter
471475
*
472476
* @param string $setToken
473-
* @return Mage_Paypal_ExpressController|string
477+
* @return $this|string
474478
*/
475479
protected function _initToken($setToken = null)
476480
{

app/code/core/Mage/Paypal/Model/Api/Abstract.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,12 @@ protected function _importFromResponse(array $privateResponseMap, array $respons
382382
*
383383
* @param array &$request
384384
* @param int $i
385-
* @return true|bool
385+
* @return bool
386386
*/
387387
protected function _exportLineItems(array &$request, $i = 0)
388388
{
389389
if (!$this->_cart) {
390-
return;
390+
return false;
391391
}
392392

393393
// always add cart totals, even if line items are not requested
@@ -403,9 +403,9 @@ protected function _exportLineItems(array &$request, $i = 0)
403403
// add cart line items
404404
$items = $this->_cart->getItems();
405405
if (empty($items) || !$this->getIsLineItemsEnabled()) {
406-
return;
406+
return false;
407407
}
408-
$result = null;
408+
$result = false;
409409
foreach ($items as $item) {
410410
foreach ($this->_lineItemExportItemsFormat as $publicKey => $privateFormat) {
411411
$result = true;

app/code/core/Mage/Paypal/Model/Api/Standard.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ public function debugRequest($request)
187187
*
188188
* @param array $request
189189
* @param int $i
190-
* @return true|null
190+
* @return bool
191191
*/
192192
protected function _exportLineItems(array &$request, $i = 1)
193193
{
194194
if (!$this->_cart) {
195-
return;
195+
return false;
196196
}
197197
if ($this->getIsLineItemsEnabled()) {
198198
$this->_cart->isShippingAsItem(true);

lib/Varien/Image/Adapter/Gd2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function _convertToByte($memoryValue)
109109
}
110110
if (preg_match('~^([1-9][0-9]*)[\s]*(k|m|g)b?$~i', $memoryValue, $matches)) {
111111
$option = strtolower($matches[2]);
112-
$memoryValue = $matches[1];
112+
$memoryValue = (int)$matches[1];
113113
switch ($option) {
114114
case 'g':
115115
$memoryValue *= 1024;

lib/Varien/Io/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Varien_Io_File extends Varien_Io_Abstract
7070
/**
7171
* Stream open file pointer
7272
*
73-
* @var resource
73+
* @var resource|null
7474
*/
7575
protected $_streamHandler;
7676

0 commit comments

Comments
 (0)