diff --git a/Helper/Data.php b/Helper/Data.php index 6e4cbf0..e1063fd 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -53,6 +53,8 @@ private function initAttributes(ProductAttributeCollectionFactory $attributeColl $attributeCollection = $attributeCollectionFactory->create(); $attributeCollection->addFieldToFilter(ProductAttributeInterface::FRONTEND_INPUT, 'smile_custom_entity'); - $this->customEntityProductAttributes = $attributeCollection->getItems(); + /** @var ProductAttributeInterface[] $attributeCollectionItems */ + $attributeCollectionItems = $attributeCollection->getItems(); + $this->customEntityProductAttributes = $attributeCollectionItems; } } diff --git a/Helper/Product.php b/Helper/Product.php index 5663cac..f030041 100644 --- a/Helper/Product.php +++ b/Helper/Product.php @@ -46,6 +46,7 @@ public function __construct( public function getCustomEntities(ProductInterface $product, string $attributeCode): array { $result = []; + // @phpstan-ignore-next-line $customEntities = $product->getExtensionAttributes()->getCustomEntities(); if ($customEntities) { foreach ($customEntities as $customEntity) { diff --git a/Model/CustomEntityProductLinkManagement.php b/Model/CustomEntityProductLinkManagement.php index 6f949c9..0eef753 100644 --- a/Model/CustomEntityProductLinkManagement.php +++ b/Model/CustomEntityProductLinkManagement.php @@ -5,8 +5,11 @@ namespace Smile\CustomEntityProductLink\Model; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Model\AbstractModel\Stub; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\Api\SearchCriteriaBuilderFactory; +use Magento\Framework\DataObject; +use Magento\Framework\Model\AbstractModel; use Smile\CustomEntity\Api\CustomEntityRepositoryInterface; use Smile\CustomEntity\Api\Data\CustomEntityInterface; use Smile\CustomEntityProductLink\Api\CustomEntityProductLinkManagementInterface; @@ -53,10 +56,12 @@ public function __construct( */ public function getCustomEntities(ProductInterface $product): ?array { + /** @var CustomEntityInterface[] $entities */ $entities = []; foreach ($this->resourceModel->loadCustomEntityData($product->getId()) as $linkData) { // @todo use collection + /** @var Stub $product */ $customEntity = $this->customEntityRepository->get($linkData['custom_entity_id'], $product->getStoreId()); $entities[$linkData['attribute_code']][] = $customEntity; } @@ -105,12 +110,14 @@ public function getCustomEntitiesByProductIds(array $productIds, array $attribut public function saveCustomEntities(ProductInterface $product): ?ProductInterface { foreach ($this->helper->getCustomEntityProductAttributes() as $attribute) { + /** @var DataObject $product */ $entityIds = $product->getData($attribute->getAttributeCode()); if (!$entityIds) { $entityIds = []; } + /** @var AbstractModel $attribute */ $this->resourceModel->saveLinks($product->getId(), $attribute->getId(), $entityIds); } diff --git a/Model/Layer/CustomEntity/CollectionFilter.php b/Model/Layer/CustomEntity/CollectionFilter.php index de1eab6..1f96fc2 100644 --- a/Model/Layer/CustomEntity/CollectionFilter.php +++ b/Model/Layer/CustomEntity/CollectionFilter.php @@ -12,6 +12,7 @@ use Magento\Framework\Registry; use Smile\CustomEntity\Api\Data\CustomEntityInterface; use Smile\CustomEntityProductLink\Helper\Product as ProductHelper; +use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection; use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; use Smile\ElasticsuiteCore\Search\Request\QueryInterface; @@ -61,6 +62,7 @@ public function filter($collection, Category $category): void ['field' => $this->getAttributeCode(), 'value' => $currentCustomEntity->getId()] ); + /** @var Collection $collection */ $collection->addQueryFilter($query); } } diff --git a/Model/Product/Indexer/Fulltext/Datasource/CustomEntity.php b/Model/Product/Indexer/Fulltext/Datasource/CustomEntity.php index 25b75e0..8432173 100644 --- a/Model/Product/Indexer/Fulltext/Datasource/CustomEntity.php +++ b/Model/Product/Indexer/Fulltext/Datasource/CustomEntity.php @@ -5,6 +5,8 @@ namespace Smile\CustomEntityProductLink\Model\Product\Indexer\Fulltext\Datasource; use Magento\Eav\Api\Data\AttributeInterface; +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; +use Magento\Eav\Model\Entity\Attribute\AttributeInterface as EntityAttributeInterface; use Smile\CustomEntityProductLink\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\CustomEntity as ResourceModel; use Smile\ElasticsuiteCatalog\Helper\AbstractAttribute as AttributeHelper; use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface; @@ -135,6 +137,9 @@ private function loadCustomEntityRowData($storeId, array $productIds, array $att private function addField(AttributeInterface $attribute): self { $fieldName = $attribute->getAttributeCode(); + + /** @var AttributeHelper $fieldConfig */ + /** @var EntityAttributeInterface $attribute */ $fieldConfig = $this->attributeHelper->getMappingFieldOptions($attribute); $optionFieldName = $this->attributeHelper->getOptionTextFieldName($fieldName); @@ -146,7 +151,8 @@ private function addField(AttributeInterface $attribute): self $fieldConfig['is_used_in_spellcheck'] = false; $fieldConfig['is_searchable'] = false; - $fieldType = $this->attributeHelper->getFieldType($attribute); + /** @var AbstractAttribute $attribute */ + $fieldType = $this->attributeHelper->getFieldType($attribute->getAttributeId()); $fieldOptions = ['name' => $fieldName, 'type' => $fieldType, 'fieldConfig' => $fieldConfig]; $this->fields[$fieldName] = $this->fieldFactory->create($fieldOptions); diff --git a/Model/ResourceModel/Search/CustomCollection.php b/Model/ResourceModel/Search/CustomCollection.php index f868453..7a12f35 100644 --- a/Model/ResourceModel/Search/CustomCollection.php +++ b/Model/ResourceModel/Search/CustomCollection.php @@ -15,37 +15,44 @@ class CustomCollection extends Collection { /** - * This method has been rewritten with the purpose of removing all smile custom entities from the attribute - * collection. This enables the product grid search to create the correct sql request in the method - * _getSearchEntityIdsSql + * @inheritdoc */ - protected function _getAttributesCollection(): AbstractDb + protected function _getAttributesCollection() { + /** @var AbstractDb $attributesCollection */ + $attributesCollection = $this->_attributesCollection; + if (!$this->_attributesCollection) { - $this->_attributesCollection = $this->_attributeCollectionFactory + /** @var array $attributesCollection */ + $attributesCollection = $this->_attributeCollectionFactory ->create() ->addSearchableAttributeFilter() ->addFieldToFilter('frontend_input', ['neq' => 'smile_custom_entity']) ->load(); + $this->_attributesCollection = $attributesCollection; + foreach ($this->_attributesCollection as $attribute) { $attribute->setEntity($this->getEntity()); } } - return $this->_attributesCollection; + + return $attributesCollection; } /** * @inheritdoc + * @SuppressWarnings(PHPMD) */ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = true) { + /** @var Select $sql */ $sql = parent::_getSearchEntityIdsSql($query, $searchOnlyInCurrentStore); $selects = $this->_getSmileCustomSql($query); $sql = $sql->union($selects, Select::SQL_UNION_ALL); - return $sql; + return (string) $sql; } /** @@ -54,6 +61,7 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr * @param mixed $query Query * @return array|null * @throws LocalizedException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function _getSmileCustomSql($query): ?array { diff --git a/Plugin/Block/Product/ListProductPlugin.php b/Plugin/Block/Product/ListProductPlugin.php index 0edffbb..9b29fd1 100644 --- a/Plugin/Block/Product/ListProductPlugin.php +++ b/Plugin/Block/Product/ListProductPlugin.php @@ -23,6 +23,7 @@ public function afterGetIdentities(ListProduct $source, array $identities): ?arr { /** @var ProductInterface $product */ foreach ($source->getLoadedProductCollection() as $product) { + // @phpstan-ignore-next-line $customEntities = $product->getExtensionAttributes()->getCustomEntities(); $identities = []; if ($customEntities) { diff --git a/Plugin/Block/Product/ViewPlugin.php b/Plugin/Block/Product/ViewPlugin.php index f8a5c57..15caa5d 100644 --- a/Plugin/Block/Product/ViewPlugin.php +++ b/Plugin/Block/Product/ViewPlugin.php @@ -22,6 +22,7 @@ class ViewPlugin public function afterGetIdentities(View $source, array $identities): ?array { // @todo Optimization: only custom entities if is visible on front + // @phpstan-ignore-next-line $customEntities = $source->getProduct()->getExtensionAttributes()->getCustomEntities(); if ($customEntities) { /** @var CustomEntityInterface $customEntity */ diff --git a/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/EavPlugin.php b/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/EavPlugin.php index 7c21cd7..4c5dcac 100644 --- a/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/EavPlugin.php +++ b/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/EavPlugin.php @@ -6,6 +6,7 @@ use Magento\Catalog\Api\Data\ProductAttributeInterface; use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav as EavModifier; +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; use Magento\Framework\Stdlib\ArrayManager; use Smile\CustomEntity\Model\ResourceModel\CustomEntity\CollectionFactory as EntityCollectionFactory; use Smile\ScopedEav\Api\Data\EntityInterface; @@ -81,6 +82,7 @@ public function aroundSetupAttributeMeta( */ private function getOptions(ProductAttributeInterface $attribute): array { + /** @var AbstractAttribute $attribute */ $attributeSetId = $attribute->getCustomEntityAttributeSetId(); /** diff --git a/Plugin/Helper/AbstractAttributePlugin.php b/Plugin/Helper/AbstractAttributePlugin.php index 8e62d8d..40fdb57 100644 --- a/Plugin/Helper/AbstractAttributePlugin.php +++ b/Plugin/Helper/AbstractAttributePlugin.php @@ -4,6 +4,7 @@ namespace Smile\CustomEntityProductLink\Plugin\Helper; +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; use Magento\Eav\Model\Entity\Attribute\AttributeInterface; use Smile\CustomEntity\Model\CustomEntity; use Smile\ElasticsuiteCatalog\Helper\ProductAttribute; @@ -22,6 +23,7 @@ class AbstractAttributePlugin */ public function afterGetFilterField(ProductAttribute $source, string $field, AttributeInterface $attribute): ?string { + /** @var AbstractAttribute $attribute */ if ($attribute->getFrontendInput() == CustomEntity::CACHE_TAG) { $field = $source->getOptionTextFieldName($field); }