Skip to content

Commit 37642ed

Browse files
authored
PHPStan: fixes for Mage_Catalog_Model_Product_Type_Abstract (OpenMage#2605)
1 parent c4c2102 commit 37642ed

File tree

41 files changed

+258
-512
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+258
-512
lines changed

.github/phpstan-baseline.neon

Lines changed: 9 additions & 344 deletions
Large diffs are not rendered by default.

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Composite/Fieldset/Grouped.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public function getProduct()
7272
public function getAssociatedProducts()
7373
{
7474
$product = $this->getProduct();
75-
$result = $product->getTypeInstance(true)->getAssociatedProducts($product);
75+
/** @var Mage_Catalog_Model_Product_Type_Grouped $productType */
76+
$productType = $product->getTypeInstance(true);
77+
$result = $productType->getAssociatedProducts($product);
7678

7779
$storeId = $product->getStoreId();
7880
foreach ($result as $item) {

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Created.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ public function getAttributesJson()
9393
public function getAttributes()
9494
{
9595
if ($this->getConfigurableProduct()->getId()) {
96-
return $this->getConfigurableProduct()->getTypeInstance(true)->getUsedProductAttributes($this->getConfigurableProduct());
96+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
97+
$productType = $this->getConfigurableProduct()->getTypeInstance(true);
98+
return $productType->getUsedProductAttributes($this->getConfigurableProduct());
9799
}
98100

99101
$attributes = [];

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,18 @@ public function getAttributeSetName()
234234

235235
public function getIsConfigured()
236236
{
237-
if ($this->getProduct()->isConfigurable()
238-
&& !($superAttributes = $this->getProduct()
239-
->getTypeInstance(true)
240-
->getUsedProductAttributeIds($this->getProduct()))
241-
) {
242-
$superAttributes = false;
237+
$superAttributes = true;
238+
$product = $this->getProduct();
239+
if ($product->isConfigurable()) {
240+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
241+
$productType = $product->getTypeInstance(true);
242+
$superAttributes = $productType->getUsedProductAttributeIds($product);
243+
if (!$superAttributes) {
244+
$superAttributes = false;
245+
}
243246
}
244247

245-
return !$this->getProduct()->isConfigurable() || $superAttributes !== false;
248+
return !$product->isConfigurable() || $superAttributes !== false;
246249
}
247250

248251
public function getSelectedTabId()

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ protected function _getProduct()
140140
*/
141141
public function getAttributesJson()
142142
{
143-
$attributes = $this->_getProduct()->getTypeInstance(true)
144-
->getConfigurableAttributesAsArray($this->_getProduct());
143+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
144+
$productType = $this->_getProduct()->getTypeInstance(true);
145+
$attributes = $productType->getConfigurableAttributesAsArray($this->_getProduct());
145146
if(!$attributes) {
146147
return '[]';
147148
} else {
@@ -172,9 +173,10 @@ public function getAttributesJson()
172173
*/
173174
public function getLinksJson()
174175
{
175-
$products = $this->_getProduct()->getTypeInstance(true)
176-
->getUsedProducts(null, $this->_getProduct());
177-
if(!$products) {
176+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
177+
$productType = $this->_getProduct()->getTypeInstance(true);
178+
$products = $productType->getUsedProducts(null, $this->_getProduct());
179+
if (!$products) {
178180
return '{}';
179181
}
180182
$data = [];
@@ -192,8 +194,9 @@ public function getLinksJson()
192194
*/
193195
public function getConfigurableSettings($product) {
194196
$data = [];
195-
$attributes = $this->_getProduct()->getTypeInstance(true)
196-
->getUsedProductAttributes($this->_getProduct());
197+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
198+
$productType = $this->_getProduct()->getTypeInstance(true);
199+
$attributes = $productType->getUsedProductAttributes($this->_getProduct());
197200
foreach ($attributes as $attribute) {
198201
$data[] = [
199202
'attribute_id' => $attribute->getId(),
@@ -285,8 +288,9 @@ public function getQuickCreationUrl()
285288
protected function _getRequiredAttributesIds()
286289
{
287290
$attributesIds = [];
288-
$configurableAttributes = $this->_getProduct()
289-
->getTypeInstance(true)->getConfigurableAttributes($this->_getProduct());
291+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
292+
$productType = $this->_getProduct()->getTypeInstance(true);
293+
$configurableAttributes = $productType->getConfigurableAttributes($this->_getProduct());
290294
foreach ($configurableAttributes as $attribute) {
291295
$attributesIds[] = $attribute->getProductAttribute()->getId();
292296
}

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Grid.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ protected function _prepareCollection()
138138
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
139139
Mage::getModel('cataloginventory/stock_item')->addCatalogInventoryToProductCollection($collection);
140140
}
141+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
142+
$productType = $product->getTypeInstance(true);
141143

142-
foreach ($product->getTypeInstance(true)->getUsedProductAttributes($product) as $attribute) {
144+
foreach ($productType->getUsedProductAttributes($product) as $attribute) {
143145
$collection->addAttributeToSelect($attribute->getAttributeCode());
144146
$collection->addAttributeToFilter($attribute->getAttributeCode(), ['notnull'=>1]);
145147
}
@@ -158,7 +160,9 @@ protected function _getSelectedProducts()
158160
{
159161
$products = $this->getRequest()->getPost('products', null);
160162
if (!is_array($products)) {
161-
$products = $this->_getProduct()->getTypeInstance(true)->getUsedProductIds($this->_getProduct());
163+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
164+
$productType = $this->_getProduct()->getTypeInstance(true);
165+
$products = $productType->getUsedProductIds($this->_getProduct());
162166
}
163167
return $products;
164168
}
@@ -183,7 +187,9 @@ public function isReadonly()
183187
protected function _prepareColumns()
184188
{
185189
$product = $this->_getProduct();
186-
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
190+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
191+
$productType = $product->getTypeInstance(true);
192+
$attributes = $productType->getConfigurableAttributes($product);
187193

188194
if (!$this->isReadonly()) {
189195
$this->addColumn('in_products', [
@@ -297,11 +303,9 @@ public function getEditParamsForAssociated()
297303
protected function _getRequiredAttributesIds()
298304
{
299305
$attributesIds = [];
300-
foreach (
301-
$this->_getProduct()
302-
->getTypeInstance(true)
303-
->getConfigurableAttributes($this->_getProduct()) as $attribute
304-
) {
306+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
307+
$productType = $this->_getProduct()->getTypeInstance(true);
308+
foreach ($productType->getConfigurableAttributes($this->_getProduct()) as $attribute) {
305309
$attributesIds[] = $attribute->getProductAttribute()->getId();
306310
}
307311

@@ -333,7 +337,9 @@ protected function _getConfigAttributeCodes()
333337
{
334338
if (is_null($this->_configAttributeCodes)) {
335339
$product = $this->_getProduct();
336-
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
340+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
341+
$productType = $product->getTypeInstance(true);
342+
$attributes = $productType->getConfigurableAttributes($product);
337343
$attributeCodes = [];
338344
foreach ($attributes as $attribute) {
339345
$productAttribute = $attribute->getProductAttribute();

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Simple.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,47 @@ protected function _prepareForm()
5858
->setAttributeSetId($this->_getProduct()->getAttributeSetId())
5959
->getAttributes();
6060

61-
/* Standart attributes */
61+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
62+
$productType = $this->_getProduct()->getTypeInstance(true);
63+
$usedAttributes = $productType->getUsedProductAttributes($this->_getProduct());
64+
65+
/* Standard attributes */
6266
foreach ($attributes as $attribute) {
67+
$attributeCode = $attribute->getAttributeCode();
6368
if (($attribute->getIsRequired()
64-
&& $attribute->getApplyTo()
65-
// If not applied to configurable
66-
&& !in_array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, $attribute->getApplyTo())
67-
// If not used in configurable
68-
&& !in_array($attribute->getId(),
69-
$this->_getProduct()->getTypeInstance(true)->getUsedProductAttributeIds($this->_getProduct()))
70-
)
69+
&& $attribute->getApplyTo()
70+
// If not applied to configurable
71+
&& !in_array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, $attribute->getApplyTo())
72+
// If not used in configurable
73+
&& !in_array($attribute->getId(), $usedAttributes))
7174
// Or in additional
72-
|| in_array($attribute->getAttributeCode(), $attributesConfig['additional'])
75+
|| in_array($attributeCode, $attributesConfig['additional'])
7376
) {
7477
$inputType = $attribute->getFrontend()->getInputType();
7578
if (!in_array($inputType, $availableTypes)) {
7679
continue;
7780
}
78-
$attributeCode = $attribute->getAttributeCode();
7981
$attribute->setAttributeCode('simple_product_' . $attributeCode);
8082
$element = $fieldset->addField(
8183
'simple_product_' . $attributeCode,
82-
$inputType,
83-
[
84+
$inputType,
85+
[
8486
'label' => $attribute->getFrontend()->getLabel(),
8587
'name' => $attributeCode,
8688
'required' => $attribute->getIsRequired(),
87-
]
89+
]
8890
)->setEntityAttribute($attribute);
8991

9092
if (in_array($attributeCode, $attributesConfig['autogenerate'])) {
9193
$element->setDisabled('true');
9294
$element->setValue($this->_getProduct()->getData($attributeCode));
9395
$element->setAfterElementHtml(
94-
'<input type="checkbox" id="simple_product_' . $attributeCode . '_autogenerate" '
95-
. 'name="simple_product[' . $attributeCode . '_autogenerate]" value="1" '
96-
. 'onclick="toggleValueElements(this, this.parentNode)" checked="checked" /> '
97-
. '<label for="simple_product_' . $attributeCode . '_autogenerate" >'
98-
. Mage::helper('catalog')->__('Autogenerate')
99-
. '</label>'
96+
'<input type="checkbox" id="simple_product_' . $attributeCode . '_autogenerate" '
97+
. 'name="simple_product[' . $attributeCode . '_autogenerate]" value="1" '
98+
. 'onclick="toggleValueElements(this, this.parentNode)" checked="checked" /> '
99+
. '<label for="simple_product_' . $attributeCode . '_autogenerate" >'
100+
. Mage::helper('catalog')->__('Autogenerate')
101+
. '</label>'
100102
);
101103
}
102104

@@ -108,7 +110,6 @@ protected function _prepareForm()
108110
}
109111

110112
/* Configurable attributes */
111-
$usedAttributes = $this->_getProduct()->getTypeInstance(true)->getUsedProductAttributes($this->_getProduct());
112113
foreach ($usedAttributes as $attribute) {
113114
$attributeCode = $attribute->getAttributeCode();
114115
$fieldset->addField( 'simple_product_' . $attributeCode, 'select', [

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Settings.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ protected function _prepareForm()
8181

8282
$fieldset->addField('req_text', 'note', [
8383
'text' => '<ul class="messages"><li class="notice-msg"><ul><li>'
84-
. $this->__('Only attributes with scope "Global", input type "Dropdown" and Use To Create Configurable Product "Yes" are available.')
85-
. '</li></ul></li></ul>'
84+
. $this->__('Only attributes with scope "Global", input type "Dropdown" and Use To Create Configurable Product "Yes" are available.')
85+
. '</li></ul></li></ul>'
8686
]);
8787

8888
$hasAttributes = false;
8989

90+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
91+
$productType = $product->getTypeInstance(true);
92+
9093
foreach ($attributes as $attribute) {
91-
if ($product->getTypeInstance(true)->canUseAttribute($attribute, $product)) {
94+
if ($productType->canUseAttribute($attribute, $product)) {
9295
$hasAttributes = true;
9396
$fieldset->addField('attribute_'.$attribute->getAttributeId(), 'checkbox', [
9497
'label' => $attribute->getFrontend()->getLabel(),
@@ -102,9 +105,9 @@ protected function _prepareForm()
102105

103106
if ($hasAttributes) {
104107
$fieldset->addField('attributes', 'hidden', [
105-
'name' => 'attribute_validate',
106-
'value' => '',
107-
'class' => 'validate-super-product-attributes'
108+
'name' => 'attribute_validate',
109+
'value' => '',
110+
'class' => 'validate-super-product-attributes'
108111
]);
109112

110113
$fieldset->addField('continue_button', 'note', [

app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ protected function _initProduct()
9898
}
9999

100100
$attributes = $this->getRequest()->getParam('attributes');
101-
if ($attributes && $product->isConfigurable() &&
102-
(!$productId || !$product->getTypeInstance()->getUsedProductAttributeIds())) {
103-
$product->getTypeInstance()->setUsedProductAttributeIds(
104-
explode(",", base64_decode(urldecode($attributes)))
105-
);
101+
if ($attributes && $product->isConfigurable()) {
102+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
103+
$productType = $product->getTypeInstance();
104+
if (!$productId || !$productType->getUsedProductAttributeIds()) {
105+
$productType->setUsedProductAttributeIds(
106+
explode(",", base64_decode(urldecode($attributes)))
107+
);
108+
}
106109
}
107110

108111
// Required attributes of simple product for configurable creation
@@ -207,10 +210,12 @@ public function newAction()
207210
$this->loadLayout('popup');
208211
} else {
209212
$_additionalLayoutPart = '';
210-
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
211-
&& !($product->getTypeInstance()->getUsedProductAttributeIds()))
212-
{
213-
$_additionalLayoutPart = '_new';
213+
if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
214+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
215+
$productType = $product->getTypeInstance();
216+
if (!$productType->getUsedProductAttributeIds()) {
217+
$_additionalLayoutPart = '_new';
218+
}
214219
}
215220
$this->loadLayout([
216221
'default',
@@ -249,10 +254,12 @@ public function editAction()
249254
Mage::dispatchEvent('catalog_product_edit_action', ['product' => $product]);
250255

251256
$_additionalLayoutPart = '';
252-
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
253-
&& !($product->getTypeInstance()->getUsedProductAttributeIds()))
254-
{
255-
$_additionalLayoutPart = '_new';
257+
if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
258+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
259+
$productType = $product->getTypeInstance();
260+
if (!$productType->getUsedProductAttributeIds()) {
261+
$_additionalLayoutPart = '_new';
262+
}
256263
}
257264

258265
$this->loadLayout([
@@ -1052,7 +1059,9 @@ public function quickCreateAction()
10521059
$autogenerateOptions = [];
10531060
$result['attributes'] = [];
10541061

1055-
foreach ($configurableProduct->getTypeInstance()->getConfigurableAttributes() as $attribute) {
1062+
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
1063+
$productType = $product->getTypeInstance();
1064+
foreach ($productType->getConfigurableAttributes() as $attribute) {
10561065
$value = $product->getAttributeText($attribute->getProductAttribute()->getAttributeCode());
10571066
$autogenerateOptions[] = $value;
10581067
$result['attributes'][] = [

app/code/core/Mage/Bundle/Block/Adminhtml/Catalog/Product/Edit/Tab/Bundle/Option.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,22 @@ public function getAddSelectionButtonHtml()
216216
public function getOptions()
217217
{
218218
if (!$this->_options) {
219-
$this->getProduct()->getTypeInstance(true)->setStoreFilter(
220-
$this->getProduct()->getStoreId(),
221-
$this->getProduct()
219+
$product = $this->getProduct();
220+
/** @var Mage_Bundle_Model_Product_Type $productType */
221+
$productType = $product->getTypeInstance(true);
222+
223+
$productType->setStoreFilter(
224+
$product->getStoreId(),
225+
$product
222226
);
223227

224228
/** @var Mage_Bundle_Model_Resource_Option_Collection $optionCollection */
225-
$optionCollection = $this->getProduct()->getTypeInstance(true)->getOptionsCollection($this->getProduct());
229+
$optionCollection = $productType->getOptionsCollection($product);
226230

227231
/** @var Mage_Bundle_Model_Resource_Selection_Collection $selectionCollection */
228-
$selectionCollection = $this->getProduct()->getTypeInstance(true)->getSelectionsCollection(
229-
$this->getProduct()->getTypeInstance(true)->getOptionsIds($this->getProduct()),
230-
$this->getProduct()
232+
$selectionCollection = $productType->getSelectionsCollection(
233+
$productType->getOptionsIds($product),
234+
$product
231235
);
232236

233237
$this->_options = $optionCollection->appendSelections($selectionCollection);

0 commit comments

Comments
 (0)