Skip to content

[WIP] Fixes for the issues in app/code found by Unit Tests for PHP 8.1 compatibility #34453

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

Closed
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
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @api
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
class Image extends AbstractHelper implements ArgumentInterface
Expand Down Expand Up @@ -787,7 +788,7 @@ protected function getImageFile()
*/
protected function parseSize($string)
{
$size = explode('x', strtolower($string));
$size = explode('x', strtolower((string) $string));
if (count($size) == 2) {
return ['width' => $size[0] > 0 ? $size[0] : null, 'height' => $size[1] > 0 ? $size[1] : null];
}
Expand Down
59 changes: 30 additions & 29 deletions app/code/Magento/Catalog/Helper/Product/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,40 @@ public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\It
$optionIds = $item->getOptionByCode('option_ids');
if ($optionIds) {
$options = [];
foreach (explode(',', $optionIds->getValue()) as $optionId) {
foreach (explode(',', (string) $optionIds->getValue()) as $optionId) {
$option = $product->getOptionById($optionId);
if ($option) {
$itemOption = $item->getOptionByCode('option_' . $option->getId());
/** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
$group = $option->groupFactory($option->getType())
->setOption($option)
->setConfigurationItem($item)
->setConfigurationItemOption($itemOption);

if ('file' == $option->getType()) {
$downloadParams = $item->getFileDownloadParams();
if ($downloadParams) {
$url = $downloadParams->getUrl();
if ($url) {
$group->setCustomOptionDownloadUrl($url);
}
$urlParams = $downloadParams->getUrlParams();
if ($urlParams) {
$group->setCustomOptionUrlParams($urlParams);
}
if (!$option) {
continue;
}
$itemOption = $item->getOptionByCode('option_' . $option->getId());
/** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
$group = $option->groupFactory($option->getType())
->setOption($option)
->setConfigurationItem($item)
->setConfigurationItemOption($itemOption);

if ('file' == $option->getType()) {
$downloadParams = $item->getFileDownloadParams();
if ($downloadParams) {
$url = $downloadParams->getUrl();
if ($url) {
$group->setCustomOptionDownloadUrl($url);
}
$urlParams = $downloadParams->getUrlParams();
if ($urlParams) {
$group->setCustomOptionUrlParams($urlParams);
}
}

$options[] = [
'label' => $option->getTitle(),
'value' => $group->getFormattedOptionValue($itemOption->getValue()),
'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
'option_id' => $option->getId(),
'option_type' => $option->getType(),
'custom_view' => $group->isCustomizedView(),
];
}

$options[] = [
'label' => $option->getTitle(),
'value' => $group->getFormattedOptionValue($itemOption->getValue()),
'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
'option_id' => $option->getId(),
'option_type' => $option->getType(),
'custom_view' => $group->isCustomizedView(),
];
}
}

Expand Down
8 changes: 5 additions & 3 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements

const CACHE_TAG = 'cat_c';

/**#@-*/
/**
* @var string
*/
protected $_eventPrefix = 'catalog_category';

/**
Expand Down Expand Up @@ -852,7 +854,7 @@ public function getPathIds()
{
$ids = $this->getData('path_ids');
if ($ids === null) {
$ids = explode('/', $this->getPath());
$ids = explode('/', (string) $this->getPath());
$this->setData('path_ids', $ids);
}
return $ids;
Expand All @@ -866,7 +868,7 @@ public function getPathIds()
public function getLevel()
{
if (!$this->hasLevel()) {
return count(explode('/', $this->getPath())) - 1;
return count(explode('/', (string) $this->getPath())) - 1;
}
return $this->getData(self::KEY_LEVEL);
}
Expand Down
7 changes: 6 additions & 1 deletion app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterface
{
Expand Down Expand Up @@ -347,7 +348,7 @@ protected function getCacheKey($data)
}
}
$serializeData = $this->serializer->serialize($serializeData);
return sha1($serializeData);
return sha1((string) $serializeData);
}

/**
Expand Down Expand Up @@ -520,8 +521,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function save(ProductInterface $product, $saveOptions = false)
{
Expand Down Expand Up @@ -779,6 +782,7 @@ public function cleanCache()
private function getMediaGalleryProcessor()
{
if (null === $this->mediaProcessor) {
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
$this->mediaProcessor = \Magento\Framework\App\ObjectManager::getInstance()
->get(MediaGalleryProcessor::class);
}
Expand All @@ -795,6 +799,7 @@ private function getMediaGalleryProcessor()
private function getCollectionProcessor()
{
if (!$this->collectionProcessor) {
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
// phpstan:ignore "Class Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor not found."
\Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ class SaveTest extends AttributeTest
protected function setUp(): void
{
parent::setUp();
$this->filterManagerMock = $this->createMock(FilterManager::class);
$this->filterManagerMock = $this->getMockBuilder(FilterManager::class)
->setMethods(['stripTags'])
->disableOriginalConstructor()
->getMock();
$this->productHelperMock = $this->createMock(ProductHelper::class);
$this->attributeSetMock = $this->createMock(AttributeSetInterface::class);
$this->builderMock = $this->createMock(Build::class);
Expand Down Expand Up @@ -173,6 +176,9 @@ protected function setUp(): void
$this->attributeFactoryMock
->method('create')
->willReturn($this->productAttributeMock);
$this->filterManagerMock
->method('stripTags')
->willReturn('');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ function ($arg) use ($baseDirectory, $pubDirectory) {
$this->pubDirectory->method('getAbsolutePath')
->willReturn('/a/b/c/pub/');

$this->store->method('getBaseUrl')
->willReturn('');

$this->model = new FileInfo(
$this->filesystem,
$this->mime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3119,7 +3119,7 @@ private function parseMultipleValues($labelRow)
*/
private function isSkuExist($sku)
{
$sku = strtolower($sku);
$sku = strtolower((string) $sku);
return isset($this->_oldSku[$sku]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
abstract class AbstractType
{
/**
* Common attributes cache
*
* @var array
*/
public static $commonAttributesCache = [];
Expand Down Expand Up @@ -149,8 +147,6 @@ abstract class AbstractType
protected $metadataPool;

/**
* Product entity link field
*
* @var string
*/
private $productEntityLinkField;
Expand Down Expand Up @@ -343,7 +339,7 @@ protected function attachAttributesById($attributeSetName, $attributeIds)
'apply_to' => $attribute->getApplyTo(),
'type' => \Magento\ImportExport\Model\Import::getAttributeType($attribute),
'default_value' => strlen(
$attribute->getDefaultValue()
(string) $attribute->getDefaultValue()
) ? $attribute->getDefaultValue() : null,
'options' => $this->_entityModel->getAttributeOptions(
$attribute,
Expand Down Expand Up @@ -597,6 +593,7 @@ public function saveData()
protected function getMetadataPool()
{
if (!$this->metadataPool) {
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\MetadataPool::class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;

/**
* Class Quantity
* Quantity Validator
*/
class Quantity extends AbstractImportValidator implements RowValidatorInterface
{
/**
* {@inheritdoc}
* @inheritdoc
*/
public function isValid($value)
{
Expand All @@ -24,7 +24,7 @@ public function isValid($value)
$this->_addMessages(
[
sprintf(
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
(string) $this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
'qty',
'decimal'
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Weight extends AbstractImportValidator implements RowValidatorInterface
{
/**
* {@inheritdoc}
* @inheritdoc
*/
public function isValid($value)
{
Expand All @@ -21,7 +21,7 @@ public function isValid($value)
$this->_addMessages(
[
sprintf(
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
(string) $this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
'weight',
'decimal'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\CatalogImportExport\Model\Import\Product\Validator;
use Magento\CatalogImportExport\Model\Import\Product\Validator\Media;
use Magento\CatalogImportExport\Model\Import\Product\Validator\Website;
use Magento\Framework\Stdlib\StringUtils;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\ImportExport\Model\Import;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -64,7 +65,10 @@ protected function setUp(): void
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->validator = $this->objectManagerHelper->getObject(
Validator::class,
['validators' => $this->validators]
[
'validators' => $this->validators,
'string' => new StringUtils()
]
);
$this->validator->init($this->context);
}
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Checkout/Model/DefaultConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DefaultConfigProvider implements ConfigProviderInterface
private $configurationPool;

/**
* @param QuoteIdMaskFactory
* @var QuoteIdMaskFactory
*/
protected $quoteIdMaskFactory;

Expand Down Expand Up @@ -344,7 +344,7 @@ public function getConfig()
ScopeInterface::SCOPE_STORE
),
'shippingPolicyContent' => nl2br(
$this->scopeConfig->getValue(
(string) $this->scopeConfig->getValue(
'shipping/shipping_policy/shipping_policy_content',
ScopeInterface::SCOPE_STORE
)
Expand Down
8 changes: 6 additions & 2 deletions app/code/Magento/Cms/Model/Wysiwyg/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Config extends \Magento\Framework\DataObject implements ConfigInterface
const WYSIWYG_STATUS_CONFIG_PATH = 'cms/wysiwyg/enabled';

/**
*
* Skin image identifier
*/
const WYSIWYG_SKIN_IMAGE_PLACEHOLDER_ID = 'Magento_Cms::images/wysiwyg_skin_image.png';

Expand Down Expand Up @@ -197,7 +197,11 @@ public function getConfig($data = [])
]
);

$config->setData('directives_url_quoted', preg_quote($config->getData('directives_url')));
$directivesUrl = $config->getData('directives_url');
$config->setData(
'directives_url_quoted',
$directivesUrl ? preg_quote($directivesUrl) : ''
);

if (is_array($data)) {
$config->addData($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function getHtml()
*/
protected function _getSpecificCountryElementId()
{
return substr($this->getId(), 0, strrpos($this->getId(), 'allowspecific')) . 'specificcountry';
$id = (string) $this->getId();
return substr($id, 0, strrpos($id, 'allowspecific')) . 'specificcountry';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Sender extends \Magento\Framework\App\Config\Value
public function beforeSave()
{
$value = $this->getValue();
if (!preg_match("/^[\S ]+$/", $value)) {
if (empty($value) || !preg_match("/^[\S ]+$/", $value)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The sender name "%1" is not valid. Please use only visible characters and spaces.', $value)
);
Expand Down
Loading