Skip to content

Commit 9144b32

Browse files
committed
Fixes for the issues in app/code found by Unit Tests for PHP 8.1 compatibility
1 parent a7e0f6a commit 9144b32

File tree

35 files changed

+137
-111
lines changed

35 files changed

+137
-111
lines changed

app/code/Magento/Catalog/Helper/Image.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
* @api
1818
* @SuppressWarnings(PHPMD.TooManyFields)
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1920
* @since 100.0.2
2021
*/
2122
class Image extends AbstractHelper implements ArgumentInterface
@@ -787,7 +788,7 @@ protected function getImageFile()
787788
*/
788789
protected function parseSize($string)
789790
{
790-
$size = explode('x', strtolower($string));
791+
$size = explode('x', strtolower((string) $string));
791792
if (count($size) == 2) {
792793
return ['width' => $size[0] > 0 ? $size[0] : null, 'height' => $size[1] > 0 ? $size[1] : null];
793794
}

app/code/Magento/Catalog/Helper/Product/Configuration.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,40 @@ public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\It
8686
$optionIds = $item->getOptionByCode('option_ids');
8787
if ($optionIds) {
8888
$options = [];
89-
foreach (explode(',', $optionIds->getValue()) as $optionId) {
89+
foreach (explode(',', (string) $optionIds->getValue()) as $optionId) {
9090
$option = $product->getOptionById($optionId);
91-
if ($option) {
92-
$itemOption = $item->getOptionByCode('option_' . $option->getId());
93-
/** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
94-
$group = $option->groupFactory($option->getType())
95-
->setOption($option)
96-
->setConfigurationItem($item)
97-
->setConfigurationItemOption($itemOption);
98-
99-
if ('file' == $option->getType()) {
100-
$downloadParams = $item->getFileDownloadParams();
101-
if ($downloadParams) {
102-
$url = $downloadParams->getUrl();
103-
if ($url) {
104-
$group->setCustomOptionDownloadUrl($url);
105-
}
106-
$urlParams = $downloadParams->getUrlParams();
107-
if ($urlParams) {
108-
$group->setCustomOptionUrlParams($urlParams);
109-
}
91+
if (!$option) {
92+
continue;
93+
}
94+
$itemOption = $item->getOptionByCode('option_' . $option->getId());
95+
/** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
96+
$group = $option->groupFactory($option->getType())
97+
->setOption($option)
98+
->setConfigurationItem($item)
99+
->setConfigurationItemOption($itemOption);
100+
101+
if ('file' == $option->getType()) {
102+
$downloadParams = $item->getFileDownloadParams();
103+
if ($downloadParams) {
104+
$url = $downloadParams->getUrl();
105+
if ($url) {
106+
$group->setCustomOptionDownloadUrl($url);
107+
}
108+
$urlParams = $downloadParams->getUrlParams();
109+
if ($urlParams) {
110+
$group->setCustomOptionUrlParams($urlParams);
110111
}
111112
}
112-
113-
$options[] = [
114-
'label' => $option->getTitle(),
115-
'value' => $group->getFormattedOptionValue($itemOption->getValue()),
116-
'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
117-
'option_id' => $option->getId(),
118-
'option_type' => $option->getType(),
119-
'custom_view' => $group->isCustomizedView(),
120-
];
121113
}
114+
115+
$options[] = [
116+
'label' => $option->getTitle(),
117+
'value' => $group->getFormattedOptionValue($itemOption->getValue()),
118+
'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
119+
'option_id' => $option->getId(),
120+
'option_type' => $option->getType(),
121+
'custom_view' => $group->isCustomizedView(),
122+
];
122123
}
123124
}
124125

app/code/Magento/Catalog/Model/Category.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
7272

7373
const CACHE_TAG = 'cat_c';
7474

75-
/**#@-*/
75+
/**
76+
* @var string
77+
*/
7678
protected $_eventPrefix = 'catalog_category';
7779

7880
/**
@@ -852,7 +854,7 @@ public function getPathIds()
852854
{
853855
$ids = $this->getData('path_ids');
854856
if ($ids === null) {
855-
$ids = explode('/', $this->getPath());
857+
$ids = explode('/', (string) $this->getPath());
856858
$this->setData('path_ids', $ids);
857859
}
858860
return $ids;
@@ -866,7 +868,7 @@ public function getPathIds()
866868
public function getLevel()
867869
{
868870
if (!$this->hasLevel()) {
869-
return count(explode('/', $this->getPath())) - 1;
871+
return count(explode('/', (string) $this->getPath())) - 1;
870872
}
871873
return $this->getData(self::KEY_LEVEL);
872874
}

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*
3939
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4040
* @SuppressWarnings(PHPMD.TooManyFields)
41+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
4142
*/
4243
class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterface
4344
{
@@ -347,7 +348,7 @@ protected function getCacheKey($data)
347348
}
348349
}
349350
$serializeData = $this->serializer->serialize($serializeData);
350-
return sha1($serializeData);
351+
return sha1((string) $serializeData);
351352
}
352353

353354
/**
@@ -520,8 +521,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
520521

521522
/**
522523
* @inheritdoc
524+
*
523525
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
524526
* @SuppressWarnings(PHPMD.NPathComplexity)
527+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
525528
*/
526529
public function save(ProductInterface $product, $saveOptions = false)
527530
{
@@ -779,6 +782,7 @@ public function cleanCache()
779782
private function getMediaGalleryProcessor()
780783
{
781784
if (null === $this->mediaProcessor) {
785+
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
782786
$this->mediaProcessor = \Magento\Framework\App\ObjectManager::getInstance()
783787
->get(MediaGalleryProcessor::class);
784788
}
@@ -795,6 +799,7 @@ private function getMediaGalleryProcessor()
795799
private function getCollectionProcessor()
796800
{
797801
if (!$this->collectionProcessor) {
802+
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
798803
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
799804
// phpstan:ignore "Class Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor not found."
800805
\Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor::class

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Attribute/SaveTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ class SaveTest extends AttributeTest
121121
protected function setUp(): void
122122
{
123123
parent::setUp();
124-
$this->filterManagerMock = $this->createMock(FilterManager::class);
124+
$this->filterManagerMock = $this->getMockBuilder(FilterManager::class)
125+
->setMethods(['stripTags'])
126+
->disableOriginalConstructor()
127+
->getMock();
125128
$this->productHelperMock = $this->createMock(ProductHelper::class);
126129
$this->attributeSetMock = $this->createMock(AttributeSetInterface::class);
127130
$this->builderMock = $this->createMock(Build::class);
@@ -173,6 +176,9 @@ protected function setUp(): void
173176
$this->attributeFactoryMock
174177
->method('create')
175178
->willReturn($this->productAttributeMock);
179+
$this->filterManagerMock
180+
->method('stripTags')
181+
->willReturn('');
176182
}
177183

178184
/**

app/code/Magento/Catalog/Test/Unit/Model/Category/FileInfoTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ function ($arg) use ($baseDirectory, $pubDirectory) {
116116
$this->pubDirectory->method('getAbsolutePath')
117117
->willReturn('/a/b/c/pub/');
118118

119+
$this->store->method('getBaseUrl')
120+
->willReturn('');
121+
119122
$this->model = new FileInfo(
120123
$this->filesystem,
121124
$this->mime,

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ private function parseMultipleValues($labelRow)
31303130
*/
31313131
private function isSkuExist($sku)
31323132
{
3133-
$sku = strtolower($sku);
3133+
$sku = strtolower((string) $sku);
31343134
return isset($this->_oldSku[$sku]);
31353135
}
31363136

app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
abstract class AbstractType
2424
{
2525
/**
26-
* Common attributes cache
27-
*
2826
* @var array
2927
*/
3028
public static $commonAttributesCache = [];
@@ -149,8 +147,6 @@ abstract class AbstractType
149147
protected $metadataPool;
150148

151149
/**
152-
* Product entity link field
153-
*
154150
* @var string
155151
*/
156152
private $productEntityLinkField;
@@ -343,7 +339,7 @@ protected function attachAttributesById($attributeSetName, $attributeIds)
343339
'apply_to' => $attribute->getApplyTo(),
344340
'type' => \Magento\ImportExport\Model\Import::getAttributeType($attribute),
345341
'default_value' => strlen(
346-
$attribute->getDefaultValue()
342+
(string) $attribute->getDefaultValue()
347343
) ? $attribute->getDefaultValue() : null,
348344
'options' => $this->_entityModel->getAttributeOptions(
349345
$attribute,
@@ -597,6 +593,7 @@ public function saveData()
597593
protected function getMetadataPool()
598594
{
599595
if (!$this->metadataPool) {
596+
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
600597
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
601598
->get(\Magento\Framework\EntityManager\MetadataPool::class);
602599
}

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Quantity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
99

1010
/**
11-
* Class Quantity
11+
* Quantity Validator
1212
*/
1313
class Quantity extends AbstractImportValidator implements RowValidatorInterface
1414
{
1515
/**
16-
* {@inheritdoc}
16+
* @inheritdoc
1717
*/
1818
public function isValid($value)
1919
{
@@ -24,7 +24,7 @@ public function isValid($value)
2424
$this->_addMessages(
2525
[
2626
sprintf(
27-
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
27+
(string) $this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
2828
'qty',
2929
'decimal'
3030
),

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Weight.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Weight extends AbstractImportValidator implements RowValidatorInterface
1111
{
1212
/**
13-
* {@inheritdoc}
13+
* @inheritdoc
1414
*/
1515
public function isValid($value)
1616
{
@@ -21,7 +21,7 @@ public function isValid($value)
2121
$this->_addMessages(
2222
[
2323
sprintf(
24-
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
24+
(string) $this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
2525
'weight',
2626
'decimal'
2727
)

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ValidatorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\CatalogImportExport\Model\Import\Product\Validator;
1313
use Magento\CatalogImportExport\Model\Import\Product\Validator\Media;
1414
use Magento\CatalogImportExport\Model\Import\Product\Validator\Website;
15+
use Magento\Framework\Stdlib\StringUtils;
1516
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1617
use Magento\ImportExport\Model\Import;
1718
use PHPUnit\Framework\MockObject\MockObject;
@@ -64,7 +65,10 @@ protected function setUp(): void
6465
$this->objectManagerHelper = new ObjectManagerHelper($this);
6566
$this->validator = $this->objectManagerHelper->getObject(
6667
Validator::class,
67-
['validators' => $this->validators]
68+
[
69+
'validators' => $this->validators,
70+
'string' => new StringUtils()
71+
]
6872
);
6973
$this->validator->init($this->context);
7074
}

app/code/Magento/Checkout/Model/DefaultConfigProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class DefaultConfigProvider implements ConfigProviderInterface
9797
private $configurationPool;
9898

9999
/**
100-
* @param QuoteIdMaskFactory
100+
* @var QuoteIdMaskFactory
101101
*/
102102
protected $quoteIdMaskFactory;
103103

@@ -344,7 +344,7 @@ public function getConfig()
344344
ScopeInterface::SCOPE_STORE
345345
),
346346
'shippingPolicyContent' => nl2br(
347-
$this->scopeConfig->getValue(
347+
(string) $this->scopeConfig->getValue(
348348
'shipping/shipping_policy/shipping_policy_content',
349349
ScopeInterface::SCOPE_STORE
350350
)

app/code/Magento/Cms/Model/Wysiwyg/Config.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Config extends \Magento\Framework\DataObject implements ConfigInterface
3030
const WYSIWYG_STATUS_CONFIG_PATH = 'cms/wysiwyg/enabled';
3131

3232
/**
33-
*
33+
* Skin image identifier
3434
*/
3535
const WYSIWYG_SKIN_IMAGE_PLACEHOLDER_ID = 'Magento_Cms::images/wysiwyg_skin_image.png';
3636

@@ -197,7 +197,11 @@ public function getConfig($data = [])
197197
]
198198
);
199199

200-
$config->setData('directives_url_quoted', preg_quote($config->getData('directives_url')));
200+
$directivesUrl = $config->getData('directives_url');
201+
$config->setData(
202+
'directives_url_quoted',
203+
$directivesUrl ? preg_quote($directivesUrl) : ''
204+
);
201205

202206
if (is_array($data)) {
203207
$config->addData($data);

app/code/Magento/Config/Block/System/Config/Form/Field/Select/Allowspecific.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function getHtml()
100100
*/
101101
protected function _getSpecificCountryElementId()
102102
{
103-
return substr($this->getId(), 0, strrpos($this->getId(), 'allowspecific')) . 'specificcountry';
103+
$id = (string) $this->getId();
104+
return substr($id, 0, strrpos($id, 'allowspecific')) . 'specificcountry';
104105
}
105106
}

app/code/Magento/Config/Model/Config/Backend/Email/Sender.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Sender extends \Magento\Framework\App\Config\Value
2424
public function beforeSave()
2525
{
2626
$value = $this->getValue();
27-
if (!preg_match("/^[\S ]+$/", $value)) {
27+
if (empty($value) || !preg_match("/^[\S ]+$/", $value)) {
2828
throw new \Magento\Framework\Exception\LocalizedException(
2929
__('The sender name "%1" is not valid. Please use only visible characters and spaces.', $value)
3030
);

0 commit comments

Comments
 (0)