Skip to content

code refactor, php8.x adjustmentts, param types, etc #39868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,44 @@
interface AttributeOptionManagementInterface
{
/**
* Add option to attribute
* Add an option to attribute
*
* @param int $entityType
* @param string $entityType
* @param string $attributeCode
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\InputException
*
* @return string
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
public function add($entityType, $attributeCode, $option);
public function add(string $entityType, string $attributeCode, Data\AttributeOptionInterface $option): string;

/**
* Delete option from attribute
*
* @param int $entityType
* @param string $entityType
* @param string $attributeCode
* @param string $optionId
*
* @return bool
*
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return bool
*/
public function delete($entityType, $attributeCode, $optionId);
public function delete(string $entityType, string $attributeCode, string|int $optionId): bool;

/**
* Retrieve list of attribute options
* Retrieve a list of attribute options
*
* @param int $entityType
* @param string $entityType
* @param string $attributeCode
*
* @return \Magento\Eav\Api\Data\AttributeOptionInterface[]
*
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\InputException
* @return \Magento\Eav\Api\Data\AttributeOptionInterface[]
*/
public function getItems($entityType, $attributeCode);
public function getItems(string $entityType, string $attributeCode): array;
}
25 changes: 18 additions & 7 deletions app/code/Magento/Eav/Api/AttributeRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
namespace Magento\Eav\Api;

use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Framework\Api\SearchCriteriaInterface;

/**
* Interface AttributeRepositoryInterface
* @api
Expand All @@ -20,43 +23,51 @@ interface AttributeRepositoryInterface
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Eav\Api\Data\AttributeSearchResultsInterface
*/
public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
public function getList(
string $entityTypeCode,
SearchCriteriaInterface $searchCriteria
): Data\AttributeSearchResultsInterface;

/**
* Retrieve specific attribute
*
* @param string $entityTypeCode
* @param string $attributeCode
*
* @return \Magento\Eav\Api\Data\AttributeInterface
*
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($entityTypeCode, $attributeCode);
public function get(string $entityTypeCode, string $attributeCode): Data\AttributeInterface;

/**
* Create attribute data
*
* @param \Magento\Eav\Api\Data\AttributeInterface $attribute
* @return string
*
* @return \Magento\Eav\Api\Data\AttributeInterface
*
* @throws \Magento\Framework\Exception\StateException
*/
public function save(\Magento\Eav\Api\Data\AttributeInterface $attribute);
public function save(AttributeInterface $attribute): AttributeInterface;

/**
* Delete Attribute
*
* @param Data\AttributeInterface $attribute
* @return bool True if the entity was deleted
*
* @throws \Magento\Framework\Exception\StateException
*/
public function delete(Data\AttributeInterface $attribute);
public function delete(Data\AttributeInterface $attribute): bool;

/**
* Delete Attribute By Id
*
* @param int $attributeId
* @param int|string $attributeId
* @return bool True if the entity was deleted
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function deleteById($attributeId);
public function deleteById(int|string $attributeId): bool;
}
21 changes: 15 additions & 6 deletions app/code/Magento/Eav/Model/AttributeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

namespace Magento\Eav\Model;

use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Api\Data\AttributeSearchResultsInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
Expand Down Expand Up @@ -83,20 +86,24 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function save(\Magento\Eav\Api\Data\AttributeInterface $attribute)
public function save(AttributeInterface $attribute): AttributeInterface
{
try {
$this->eavResource->save($attribute);
} catch (\Exception $e) {
throw new StateException(__("The attribute can't be saved."), $e);
}

return $attribute;
}

/**
* {@inheritdoc}
*/
public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
public function getList(
string $entityTypeCode,
SearchCriteriaInterface $searchCriteria
): AttributeSearchResultsInterface
{
if (!$entityTypeCode) {
throw InputException::requiredField('entity_type_code');
Expand Down Expand Up @@ -158,7 +165,7 @@ public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaIn
/**
* {@inheritdoc}
*/
public function get($entityTypeCode, $attributeCode)
public function get(string $entityTypeCode, string $attributeCode): AttributeInterface
{
/** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
$attribute = $this->eavConfig->getAttribute($entityTypeCode, $attributeCode);
Expand All @@ -170,13 +177,14 @@ public function get($entityTypeCode, $attributeCode)
)
);
}

return $attribute;
}

/**
* {@inheritdoc}
*/
public function delete(\Magento\Eav\Api\Data\AttributeInterface $attribute)
public function delete(AttributeInterface $attribute): bool
{
try {
$this->eavResource->delete($attribute);
Expand All @@ -189,7 +197,7 @@ public function delete(\Magento\Eav\Api\Data\AttributeInterface $attribute)
/**
* {@inheritdoc}
*/
public function deleteById($attributeId)
public function deleteById(int|string $attributeId): bool
{
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
$attribute = $this->attributeFactory->create();
Expand All @@ -211,13 +219,14 @@ public function deleteById($attributeId)
* @deprecated 101.0.0
* @return CollectionProcessorInterface
*/
private function getCollectionProcessor()
private function getCollectionProcessor(): CollectionProcessorInterface
{
if (!$this->collectionProcessor) {
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
CollectionProcessor::class
);
}

return $this->collectionProcessor;
}
}
23 changes: 13 additions & 10 deletions app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ public function __construct(
}

/**
* Add option to attribute.
* Add an option to attribute.
*
* @param int $entityType
* @param string $entityType
* @param string $attributeCode
* @param AttributeOptionInterface $option
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
*
* @return string
*
* @throws InputException
* @throws NoSuchEntityException
* @throws StateException
*/
public function add($entityType, $attributeCode, $option)
public function add(string $entityType, string $attributeCode, AttributeOptionInterface $option): string
{
$attribute = $this->loadAttribute($entityType, (string)$attributeCode);

Expand Down Expand Up @@ -160,7 +162,7 @@ private function saveOption(
}

/**
* Get option id to create new option
* Get option id to create a new option
*
* @param AttributeOptionInterface $option
* @return string
Expand All @@ -178,7 +180,7 @@ private function getNewOptionId(AttributeOptionInterface $option): string
/**
* @inheritdoc
*/
public function delete($entityType, $attributeCode, $optionId)
public function delete(string $entityType, string $attributeCode, string|int $optionId): bool
{
$attribute = $this->loadAttribute($entityType, $attributeCode);
$this->validateOption($attribute, $optionId);
Expand All @@ -202,7 +204,7 @@ public function delete($entityType, $attributeCode, $optionId)
/**
* @inheritdoc
*/
public function getItems($entityType, $attributeCode)
public function getItems(string $entityType, string $attributeCode): array
{
if (empty($attributeCode)) {
throw new InputException(__('The attribute code is empty. Enter the code and try again.'));
Expand All @@ -226,7 +228,7 @@ public function getItems($entityType, $attributeCode)
* @return void
* @throws NoSuchEntityException
*/
protected function validateOption($attribute, $optionId)
protected function validateOption(EavAttributeInterface $attribute, int $optionId): void
{
if ($attribute->getSource()->getOptionText($optionId) === false) {
throw new NoSuchEntityException(
Expand All @@ -242,14 +244,14 @@ protected function validateOption($attribute, $optionId)
/**
* Load attribute
*
* @param string|int $entityType
* @param string $entityType
* @param string $attributeCode
* @return EavAttributeInterface
* @throws InputException
* @throws NoSuchEntityException
* @throws StateException
*/
private function loadAttribute($entityType, string $attributeCode): EavAttributeInterface
private function loadAttribute(string $entityType, string $attributeCode): EavAttributeInterface
{
if (empty($attributeCode)) {
throw new InputException(__('The attribute code is empty. Enter the code and try again.'));
Expand Down Expand Up @@ -278,6 +280,7 @@ private function retrieveOptionId(
) : string {
$label = $option->getLabel() !== null ? trim($option->getLabel()) : '';
$optionId = $attribute->getSource()->getOptionId($label);

if ($optionId) {
$option->setValue($optionId);
} elseif (is_array($option->getStoreLabels())) {
Expand Down