Skip to content

Commit

Permalink
PHPStan: fixes for Mage_Catalog_Model_Product_Type_Abstract (#2605)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Sep 20, 2022
1 parent c4c2102 commit 37642ed
Show file tree
Hide file tree
Showing 41 changed files with 258 additions and 512 deletions.
353 changes: 9 additions & 344 deletions .github/phpstan-baseline.neon

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public function getProduct()
public function getAssociatedProducts()
{
$product = $this->getProduct();
$result = $product->getTypeInstance(true)->getAssociatedProducts($product);
/** @var Mage_Catalog_Model_Product_Type_Grouped $productType */
$productType = $product->getTypeInstance(true);
$result = $productType->getAssociatedProducts($product);

$storeId = $product->getStoreId();
foreach ($result as $item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public function getAttributesJson()
public function getAttributes()
{
if ($this->getConfigurableProduct()->getId()) {
return $this->getConfigurableProduct()->getTypeInstance(true)->getUsedProductAttributes($this->getConfigurableProduct());
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->getConfigurableProduct()->getTypeInstance(true);
return $productType->getUsedProductAttributes($this->getConfigurableProduct());
}

$attributes = [];
Expand Down
17 changes: 10 additions & 7 deletions app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,18 @@ public function getAttributeSetName()

public function getIsConfigured()
{
if ($this->getProduct()->isConfigurable()
&& !($superAttributes = $this->getProduct()
->getTypeInstance(true)
->getUsedProductAttributeIds($this->getProduct()))
) {
$superAttributes = false;
$superAttributes = true;
$product = $this->getProduct();
if ($product->isConfigurable()) {
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance(true);
$superAttributes = $productType->getUsedProductAttributeIds($product);
if (!$superAttributes) {
$superAttributes = false;
}
}

return !$this->getProduct()->isConfigurable() || $superAttributes !== false;
return !$product->isConfigurable() || $superAttributes !== false;
}

public function getSelectedTabId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ protected function _getProduct()
*/
public function getAttributesJson()
{
$attributes = $this->_getProduct()->getTypeInstance(true)
->getConfigurableAttributesAsArray($this->_getProduct());
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->_getProduct()->getTypeInstance(true);
$attributes = $productType->getConfigurableAttributesAsArray($this->_getProduct());
if(!$attributes) {
return '[]';
} else {
Expand Down Expand Up @@ -172,9 +173,10 @@ public function getAttributesJson()
*/
public function getLinksJson()
{
$products = $this->_getProduct()->getTypeInstance(true)
->getUsedProducts(null, $this->_getProduct());
if(!$products) {
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->_getProduct()->getTypeInstance(true);
$products = $productType->getUsedProducts(null, $this->_getProduct());
if (!$products) {
return '{}';
}
$data = [];
Expand All @@ -192,8 +194,9 @@ public function getLinksJson()
*/
public function getConfigurableSettings($product) {
$data = [];
$attributes = $this->_getProduct()->getTypeInstance(true)
->getUsedProductAttributes($this->_getProduct());
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->_getProduct()->getTypeInstance(true);
$attributes = $productType->getUsedProductAttributes($this->_getProduct());
foreach ($attributes as $attribute) {
$data[] = [
'attribute_id' => $attribute->getId(),
Expand Down Expand Up @@ -285,8 +288,9 @@ public function getQuickCreationUrl()
protected function _getRequiredAttributesIds()
{
$attributesIds = [];
$configurableAttributes = $this->_getProduct()
->getTypeInstance(true)->getConfigurableAttributes($this->_getProduct());
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->_getProduct()->getTypeInstance(true);
$configurableAttributes = $productType->getConfigurableAttributes($this->_getProduct());
foreach ($configurableAttributes as $attribute) {
$attributesIds[] = $attribute->getProductAttribute()->getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ protected function _prepareCollection()
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
Mage::getModel('cataloginventory/stock_item')->addCatalogInventoryToProductCollection($collection);
}
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance(true);

foreach ($product->getTypeInstance(true)->getUsedProductAttributes($product) as $attribute) {
foreach ($productType->getUsedProductAttributes($product) as $attribute) {
$collection->addAttributeToSelect($attribute->getAttributeCode());
$collection->addAttributeToFilter($attribute->getAttributeCode(), ['notnull'=>1]);
}
Expand All @@ -158,7 +160,9 @@ protected function _getSelectedProducts()
{
$products = $this->getRequest()->getPost('products', null);
if (!is_array($products)) {
$products = $this->_getProduct()->getTypeInstance(true)->getUsedProductIds($this->_getProduct());
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->_getProduct()->getTypeInstance(true);
$products = $productType->getUsedProductIds($this->_getProduct());
}
return $products;
}
Expand All @@ -183,7 +187,9 @@ public function isReadonly()
protected function _prepareColumns()
{
$product = $this->_getProduct();
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance(true);
$attributes = $productType->getConfigurableAttributes($product);

if (!$this->isReadonly()) {
$this->addColumn('in_products', [
Expand Down Expand Up @@ -297,11 +303,9 @@ public function getEditParamsForAssociated()
protected function _getRequiredAttributesIds()
{
$attributesIds = [];
foreach (
$this->_getProduct()
->getTypeInstance(true)
->getConfigurableAttributes($this->_getProduct()) as $attribute
) {
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->_getProduct()->getTypeInstance(true);
foreach ($productType->getConfigurableAttributes($this->_getProduct()) as $attribute) {
$attributesIds[] = $attribute->getProductAttribute()->getId();
}

Expand Down Expand Up @@ -333,7 +337,9 @@ protected function _getConfigAttributeCodes()
{
if (is_null($this->_configAttributeCodes)) {
$product = $this->_getProduct();
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance(true);
$attributes = $productType->getConfigurableAttributes($product);
$attributeCodes = [];
foreach ($attributes as $attribute) {
$productAttribute = $attribute->getProductAttribute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,47 @@ protected function _prepareForm()
->setAttributeSetId($this->_getProduct()->getAttributeSetId())
->getAttributes();

/* Standart attributes */
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $this->_getProduct()->getTypeInstance(true);
$usedAttributes = $productType->getUsedProductAttributes($this->_getProduct());

/* Standard attributes */
foreach ($attributes as $attribute) {
$attributeCode = $attribute->getAttributeCode();
if (($attribute->getIsRequired()
&& $attribute->getApplyTo()
// If not applied to configurable
&& !in_array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, $attribute->getApplyTo())
// If not used in configurable
&& !in_array($attribute->getId(),
$this->_getProduct()->getTypeInstance(true)->getUsedProductAttributeIds($this->_getProduct()))
)
&& $attribute->getApplyTo()
// If not applied to configurable
&& !in_array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, $attribute->getApplyTo())
// If not used in configurable
&& !in_array($attribute->getId(), $usedAttributes))
// Or in additional
|| in_array($attribute->getAttributeCode(), $attributesConfig['additional'])
|| in_array($attributeCode, $attributesConfig['additional'])
) {
$inputType = $attribute->getFrontend()->getInputType();
if (!in_array($inputType, $availableTypes)) {
continue;
}
$attributeCode = $attribute->getAttributeCode();
$attribute->setAttributeCode('simple_product_' . $attributeCode);
$element = $fieldset->addField(
'simple_product_' . $attributeCode,
$inputType,
[
$inputType,
[
'label' => $attribute->getFrontend()->getLabel(),
'name' => $attributeCode,
'required' => $attribute->getIsRequired(),
]
]
)->setEntityAttribute($attribute);

if (in_array($attributeCode, $attributesConfig['autogenerate'])) {
$element->setDisabled('true');
$element->setValue($this->_getProduct()->getData($attributeCode));
$element->setAfterElementHtml(
'<input type="checkbox" id="simple_product_' . $attributeCode . '_autogenerate" '
. 'name="simple_product[' . $attributeCode . '_autogenerate]" value="1" '
. 'onclick="toggleValueElements(this, this.parentNode)" checked="checked" /> '
. '<label for="simple_product_' . $attributeCode . '_autogenerate" >'
. Mage::helper('catalog')->__('Autogenerate')
. '</label>'
'<input type="checkbox" id="simple_product_' . $attributeCode . '_autogenerate" '
. 'name="simple_product[' . $attributeCode . '_autogenerate]" value="1" '
. 'onclick="toggleValueElements(this, this.parentNode)" checked="checked" /> '
. '<label for="simple_product_' . $attributeCode . '_autogenerate" >'
. Mage::helper('catalog')->__('Autogenerate')
. '</label>'
);
}

Expand All @@ -108,7 +110,6 @@ protected function _prepareForm()
}

/* Configurable attributes */
$usedAttributes = $this->_getProduct()->getTypeInstance(true)->getUsedProductAttributes($this->_getProduct());
foreach ($usedAttributes as $attribute) {
$attributeCode = $attribute->getAttributeCode();
$fieldset->addField( 'simple_product_' . $attributeCode, 'select', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ protected function _prepareForm()

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

$hasAttributes = false;

/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance(true);

foreach ($attributes as $attribute) {
if ($product->getTypeInstance(true)->canUseAttribute($attribute, $product)) {
if ($productType->canUseAttribute($attribute, $product)) {
$hasAttributes = true;
$fieldset->addField('attribute_'.$attribute->getAttributeId(), 'checkbox', [
'label' => $attribute->getFrontend()->getLabel(),
Expand All @@ -102,9 +105,9 @@ protected function _prepareForm()

if ($hasAttributes) {
$fieldset->addField('attributes', 'hidden', [
'name' => 'attribute_validate',
'value' => '',
'class' => 'validate-super-product-attributes'
'name' => 'attribute_validate',
'value' => '',
'class' => 'validate-super-product-attributes'
]);

$fieldset->addField('continue_button', 'note', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ protected function _initProduct()
}

$attributes = $this->getRequest()->getParam('attributes');
if ($attributes && $product->isConfigurable() &&
(!$productId || !$product->getTypeInstance()->getUsedProductAttributeIds())) {
$product->getTypeInstance()->setUsedProductAttributeIds(
explode(",", base64_decode(urldecode($attributes)))
);
if ($attributes && $product->isConfigurable()) {
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance();
if (!$productId || !$productType->getUsedProductAttributeIds()) {
$productType->setUsedProductAttributeIds(
explode(",", base64_decode(urldecode($attributes)))
);
}
}

// Required attributes of simple product for configurable creation
Expand Down Expand Up @@ -207,10 +210,12 @@ public function newAction()
$this->loadLayout('popup');
} else {
$_additionalLayoutPart = '';
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
&& !($product->getTypeInstance()->getUsedProductAttributeIds()))
{
$_additionalLayoutPart = '_new';
if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance();
if (!$productType->getUsedProductAttributeIds()) {
$_additionalLayoutPart = '_new';
}
}
$this->loadLayout([
'default',
Expand Down Expand Up @@ -249,10 +254,12 @@ public function editAction()
Mage::dispatchEvent('catalog_product_edit_action', ['product' => $product]);

$_additionalLayoutPart = '';
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
&& !($product->getTypeInstance()->getUsedProductAttributeIds()))
{
$_additionalLayoutPart = '_new';
if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance();
if (!$productType->getUsedProductAttributeIds()) {
$_additionalLayoutPart = '_new';
}
}

$this->loadLayout([
Expand Down Expand Up @@ -1052,7 +1059,9 @@ public function quickCreateAction()
$autogenerateOptions = [];
$result['attributes'] = [];

foreach ($configurableProduct->getTypeInstance()->getConfigurableAttributes() as $attribute) {
/** @var Mage_Catalog_Model_Product_Type_Configurable $productType */
$productType = $product->getTypeInstance();

This comment has been minimized.

Copy link
@shaughnlgh

shaughnlgh Sep 29, 2022

You have introduced a bug here, $productType = $product->getTypeInstance(); should be $productType = $configurableProduct->getTypeInstance();. The current product object is the simple product.

foreach ($productType->getConfigurableAttributes() as $attribute) {
$value = $product->getAttributeText($attribute->getProductAttribute()->getAttributeCode());
$autogenerateOptions[] = $value;
$result['attributes'][] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,22 @@ public function getAddSelectionButtonHtml()
public function getOptions()
{
if (!$this->_options) {
$this->getProduct()->getTypeInstance(true)->setStoreFilter(
$this->getProduct()->getStoreId(),
$this->getProduct()
$product = $this->getProduct();
/** @var Mage_Bundle_Model_Product_Type $productType */
$productType = $product->getTypeInstance(true);

$productType->setStoreFilter(
$product->getStoreId(),
$product
);

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

/** @var Mage_Bundle_Model_Resource_Selection_Collection $selectionCollection */
$selectionCollection = $this->getProduct()->getTypeInstance(true)->getSelectionsCollection(
$this->getProduct()->getTypeInstance(true)->getOptionsIds($this->getProduct()),
$this->getProduct()
$selectionCollection = $productType->getSelectionsCollection(
$productType->getOptionsIds($product),
$product
);

$this->_options = $optionCollection->appendSelections($selectionCollection);
Expand Down
11 changes: 7 additions & 4 deletions app/code/core/Mage/Bundle/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,13 @@ public function duplicateProduct($observer)
/** @var Mage_Catalog_Model_Product $newProduct */
$newProduct = $observer->getEvent()->getNewProduct();

$product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
$optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
$selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
/** @var Mage_Bundle_Model_Product_Type $productType */
$productType = $product->getTypeInstance(true);

$productType->setStoreFilter($product->getStoreId(), $product);
$optionCollection = $productType->getOptionsCollection($product);
$selectionCollection = $productType->getSelectionsCollection(
$productType->getOptionsIds($product),
$product
);
$optionCollection->appendSelections($selectionCollection);
Expand Down
Loading

0 comments on commit 37642ed

Please sign in to comment.