Skip to content

Fixes for the issues found by Unit Tests for PHP 8.1 compatibility #34454

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ private function reInitModel(): void
->getMockForAbstractClass();

$dateTime = '2017-10-25 18:57:08';
$timestamp = '1508983028';
$timestamp = 1508983028;
$dateTimeMock = $this->getMockBuilder(\DateTime::class)
->disableOriginalConstructor()
->onlyMethods(['format', 'getTimestamp', 'setTimestamp'])
Expand Down Expand Up @@ -2363,7 +2363,7 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
private function prepareDateTimeFactory(): string
{
$dateTime = '2017-10-25 18:57:08';
$timestamp = '1508983028';
$timestamp = 1508983028;
$dateTimeMock = $this->createMock(\DateTime::class);
$dateTimeMock->expects($this->any())
->method('format')
Expand Down
14 changes: 8 additions & 6 deletions lib/internal/Magento/Framework/App/Console/Request.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/**
* Console request
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
Expand All @@ -19,7 +17,7 @@ class Request implements \Magento\Framework\App\RequestInterface
*/
public function __construct(array $parameters = [])
{
$data = getopt(null, $parameters);
$data = getopt('', $parameters);
// It can happen that request comes from http (e.g. pub/cron.php), but it runs the console
if ($data) {
$this->setParams($data);
Expand All @@ -35,17 +33,19 @@ public function __construct(array $parameters = [])
*/
public function getModuleName()
{
// phpcs:ignore Squiz.PHP.NonExecutableCode.ReturnNotRequired
return;
}

/**
* Set Module name
*
* @param string $name
*
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setModuleName($name)
public function setModuleName($name) // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
{
}

Expand All @@ -56,6 +56,7 @@ public function setModuleName($name)
*/
public function getActionName()
{
// phpcs:ignore Squiz.PHP.NonExecutableCode.ReturnNotRequired
return;
}

Expand All @@ -66,7 +67,7 @@ public function getActionName()
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setActionName($name)
public function setActionName($name) // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
{
}

Expand Down Expand Up @@ -112,10 +113,11 @@ public function setParams(array $data)
*
* @param null|string $name
* @param null|string $default
*
* @return null|string|void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getCookie($name, $default)
public function getCookie($name, $default) // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public function testSaveConfig()
->method('getFiles')
->willReturn('test.php');
$this->dirRead->expects($this->any())
->method('getAbsolutePath');
->method('getAbsolutePath')
->willReturn('');
$this->filesystem->expects($this->any())
->method('getDirectoryWrite')
->with(DirectoryList::CONFIG)
Expand Down Expand Up @@ -231,7 +232,8 @@ public function testSaveConfigOverride()
->method('getFiles')
->willReturn('test.php');
$this->dirRead->expects($this->any())
->method('getAbsolutePath');
->method('getAbsolutePath')
->willReturn('');
$this->filesystem->expects($this->any())
->method('getDirectoryWrite')
->with(DirectoryList::CONFIG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function testGetBasePathWithPath()
public function testGetBasePathWithoutPath()
{
$this->model = $this->getModel();
$this->model->setBasePath(null);
$this->model->setBasePath('');
$this->assertEquals('/', $this->model->getBasePath());
}

Expand Down
4 changes: 3 additions & 1 deletion lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ protected function _splitMultiQuery($sql)
$parts = preg_split(
'#(;|\'|"|\\\\|//|--|\n|/\*|\*/)#',
$sql,
null,
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);

Expand Down Expand Up @@ -3625,6 +3625,7 @@ public function selectsByRange($rangeField, \Magento\Framework\DB\Select $select
private function getQueryGenerator()
{
if ($this->queryGenerator === null) {
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
$this->queryGenerator = \Magento\Framework\App\ObjectManager::getInstance()->create(QueryGenerator::class);
}
return $this->queryGenerator;
Expand Down Expand Up @@ -4097,6 +4098,7 @@ public function getAutoIncrementField($tableName, $schemaName = null)
public function getSchemaListener()
{
if ($this->schemaListener === null) {
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
$this->schemaListener = \Magento\Framework\App\ObjectManager::getInstance()->create(SchemaListener::class);
}
return $this->schemaListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testExecuteWhenThrowPDOException()
$this->pdoStatementMock->expects($this->once())
->method('execute')
->willThrowException(new \PDOException('test message'));

$this->setQueryStringForPdoStmtMock($query);
$this->assertEquals($errorReporting, error_reporting(), 'Error report level was\'t restored');

(new Mysql($this->adapterMock, $query))->_execute();
Expand Down Expand Up @@ -152,4 +152,23 @@ public function testExecuteWhenParamsAsParameterObject()

(new Mysql($this->adapterMock, $query))->_execute($params);
}

/**
* Initialize queryString property.
*
* @param string $query
*
* @return void
*/
private function setQueryStringForPdoStmtMock(string $query): void
{
/*
* In PHP 8.1 $queryString is a Typed property, thus it should be initialized before the 1st call.
* But it's not automatically initialized in case of Mocking, so we do it here.
* Note: In PHP < 8.1 such assignment prohibited.
*/
if (PHP_VERSION_ID >= 80100) {
$this->pdoStatementMock->queryString = $query;
}
}
}
8 changes: 4 additions & 4 deletions lib/internal/Magento/Framework/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ public function toString($format = '')
/**
* Set/Get attribute wrapper
*
* @param string $method
* @param array $args
* @return mixed
* @param string $method
* @param array $args
* @return mixed
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function __call($method, $args)
Expand Down Expand Up @@ -464,7 +464,7 @@ public function serialize($keys = [], $valueSeparator = '=', $fieldSeparator = '
* Present object data as string in debug mode
*
* @param mixed $data
* @param array &$objects
* @param array $objects
* @return array
*/
public function debug($data = null, &$objects = [])
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Event/Observer/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Cron extends \Magento\Framework\Event\Observer
*/
public function isValidFor(\Magento\Framework\Event $event)
{
$e = preg_split('#\s+#', $this->getCronExpr(), null, PREG_SPLIT_NO_EMPTY);
$e = preg_split('#\s+#', $this->getCronExpr(), -1, PREG_SPLIT_NO_EMPTY);
if (count($e) !== 5) {
return false;
}
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/Magento/Framework/Filter/SplitWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
*/
namespace Magento\Framework\Filter;

/**
* Split words
*/
class SplitWords implements \Zend_Filter_Interface
{
/**
Expand Down Expand Up @@ -46,7 +43,7 @@ public function __construct($uniqueOnly = true, $wordsQty = 0, $wordSeparatorReg
public function filter($str)
{
$result = [];
$split = preg_split('#' . $this->wordSeparatorRegexp . '#siu', $str, null, PREG_SPLIT_NO_EMPTY);
$split = preg_split('#' . $this->wordSeparatorRegexp . '#siu', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($split as $word) {
if ($this->uniqueOnly) {
$result[$word] = $word;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

namespace Magento\Framework\Model\ResourceModel\Db\Collection;

use \Magento\Framework\App\ResourceConnection\SourceProviderInterface;
use \Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection\SourceProviderInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Event\ManagerInterface;

/**
* Abstract Resource Collection
Expand Down Expand Up @@ -106,23 +108,23 @@ abstract class AbstractCollection extends AbstractDb implements SourceProviderIn
/**
* Event manager proxy
*
* @var \Magento\Framework\Event\ManagerInterface
* @var ManagerInterface
*/
protected $_eventManager = null;

/**
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param ManagerInterface $eventManager
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
ManagerInterface $eventManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
Expand Down Expand Up @@ -241,12 +243,7 @@ protected function _initSelectFields()
$column = $field;
}

if ($alias !== null &&
in_array($alias, $columnsToSelect) ||
// If field already joined from another table
$alias === null &&
isset($alias, $columnsToSelect)
) {
if ($alias !== null && in_array($alias, $columnsToSelect)) {
continue;
}

Expand Down Expand Up @@ -468,9 +465,8 @@ public function getResourceModelName()
public function getResource()
{
if (empty($this->_resource)) {
$this->_resource = \Magento\Framework\App\ObjectManager::getInstance()->create(
$this->getResourceModelName()
);
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
$this->_resource = ObjectManager::getInstance()->create($this->getResourceModelName());
}
return $this->_resource;
}
Expand Down Expand Up @@ -513,14 +509,13 @@ public function getAllIds()
*/
public function join($table, $cond, $cols = '*')
{
$alias = $table;
if (is_array($table)) {
foreach ($table as $k => $v) {
$alias = $k;
$table = $v;
break;
}
} else {
$alias = $table;
}

if (!isset($this->_joinedTables[$alias])) {
Expand Down Expand Up @@ -624,7 +619,7 @@ public function __sleep()
public function __wakeup()
{
parent::__wakeup();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_eventManager = $objectManager->get(\Magento\Framework\Event\ManagerInterface::class);
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
$this->_eventManager = ObjectManager::getInstance()->get(ManagerInterface::class);
}
}
15 changes: 7 additions & 8 deletions lib/internal/Magento/Framework/ObjectManager/TMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Magento\Framework\ObjectManagerInterface;

/**
* Class TMap
* @internal
*/
class TMap implements \IteratorAggregate, \Countable, \ArrayAccess
Expand Down Expand Up @@ -62,7 +61,7 @@ public function __construct(
array $array = [],
\Closure $objectCreationStrategy = null
) {
if (!class_exists($this->type) && !interface_exists($type)) {
if ((empty($this->type) || !class_exists($this->type)) && !interface_exists($type)) {
throw new \InvalidArgumentException(sprintf('Unknown type %s', $type));
}

Expand Down Expand Up @@ -142,7 +141,7 @@ private function initObject($index)
}

/**
* {inheritdoc}
* @inheritdoc
*/
public function getIterator()
{
Expand All @@ -156,23 +155,23 @@ public function getIterator()
}

/**
* {inheritdoc}
* @inheritdoc
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->array);
}

/**
* {inheritdoc}
* @inheritdoc
*/
public function offsetGet($offset)
{
return isset($this->array[$offset]) ? $this->initObject($offset) : null;
}

/**
* {inheritdoc}
* @inheritdoc
*/
public function offsetSet($offset, $value)
{
Expand All @@ -187,7 +186,7 @@ public function offsetSet($offset, $value)
}

/**
* {inheritdoc}
* @inheritdoc
*/
public function offsetUnset($offset)
{
Expand All @@ -202,7 +201,7 @@ public function offsetUnset($offset)
}

/**
* {inheritdoc}
* @inheritdoc
*/
public function count()
{
Expand Down
Loading