Skip to content

Commit

Permalink
Analyse code style with phpstan command
Browse files Browse the repository at this point in the history
  • Loading branch information
cemag44 authored and cemag44300 committed Oct 19, 2022
1 parent debdf0c commit f6722de
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions Model/CustomEntityProductLinkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions Model/Layer/CustomEntity/CollectionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,6 +62,7 @@ public function filter($collection, Category $category): void
['field' => $this->getAttributeCode(), 'value' => $currentCustomEntity->getId()]
);

/** @var Collection $collection */
$collection->addQueryFilter($query);
}
}
Expand Down
8 changes: 7 additions & 1 deletion Model/Product/Indexer/Fulltext/Datasource/CustomEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
22 changes: 15 additions & 7 deletions Model/ResourceModel/Search/CustomCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions Plugin/Block/Product/ListProductPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Plugin/Block/Product/ViewPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,6 +82,7 @@ public function aroundSetupAttributeMeta(
*/
private function getOptions(ProductAttributeInterface $attribute): array
{
/** @var AbstractAttribute $attribute */
$attributeSetId = $attribute->getCustomEntityAttributeSetId();

/**
Expand Down
2 changes: 2 additions & 0 deletions Plugin/Helper/AbstractAttributePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit f6722de

Please sign in to comment.