diff --git a/app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php b/app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php index 5359230c08c2a..9f7dc16968064 100644 --- a/app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php +++ b/app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php @@ -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; } diff --git a/app/code/Magento/Eav/Api/AttributeRepositoryInterface.php b/app/code/Magento/Eav/Api/AttributeRepositoryInterface.php index ad323cdd7bf36..dfade958e5eab 100644 --- a/app/code/Magento/Eav/Api/AttributeRepositoryInterface.php +++ b/app/code/Magento/Eav/Api/AttributeRepositoryInterface.php @@ -6,6 +6,9 @@ */ namespace Magento\Eav\Api; +use Magento\Eav\Api\Data\AttributeInterface; +use Magento\Framework\Api\SearchCriteriaInterface; + /** * Interface AttributeRepositoryInterface * @api @@ -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; } diff --git a/app/code/Magento/Eav/Model/AttributeRepository.php b/app/code/Magento/Eav/Model/AttributeRepository.php index 5bd489ca6e35a..0d6ef4820f99e 100644 --- a/app/code/Magento/Eav/Model/AttributeRepository.php +++ b/app/code/Magento/Eav/Model/AttributeRepository.php @@ -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; @@ -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'); @@ -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); @@ -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); @@ -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(); @@ -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; } } diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index 2c9b6d68b0bbf..29dc5b7b8fe10 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -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); @@ -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 @@ -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); @@ -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.')); @@ -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( @@ -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.')); @@ -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())) {