Skip to content

Commit 2a40085

Browse files
karyna-tandrewbess
authored andcommitted
Update Magento to be compatible with PHP 8.0
1 parent ea3fcf0 commit 2a40085

File tree

15 files changed

+82
-35
lines changed

15 files changed

+82
-35
lines changed

app/bootstrap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
exit(1);
3131
}
3232

33+
// PHP 8 compatibility. Define constants that are not present in PHP < 8.0
34+
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80000) {
35+
if (!defined('T_NAME_QUALIFIED')) {
36+
define('T_NAME_QUALIFIED', 24001);
37+
}
38+
if (!defined('T_NAME_FULLY_QUALIFIED')) {
39+
define('T_NAME_FULLY_QUALIFIED', 24002);
40+
}
41+
}
42+
3343
require_once __DIR__ . '/autoload.php';
3444
// Sets default autoload mappings, may be overridden in Bootstrap::create
3545
\Magento\Framework\App\Bootstrap::populateAutoloader(BP, []);

dev/tests/integration/testsuite/Magento/Customer/Model/ResourceModel/Grid/CollectionReindexOnAccountLockTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\Exception\NoSuchEntityException;
1313
use Magento\TestFramework\Helper\Bootstrap;
1414
use Magento\TestFramework\Indexer\TestCase;
15-
use Magento\Tests\NamingConvention\true\mixed;
1615

1716
/**
1817
* Test if customer account lock on too many failed authentication attempts triggers customer grid reindex
@@ -39,7 +38,7 @@ private function lockCustomerAccountWithInvalidAuthentications()
3938
}
4039

4140
/**
42-
* @return mixed
41+
* @return string|null
4342
* @throws NoSuchEntityException
4443
*/
4544
private function getCustomerLockExpire(): ?string
@@ -53,7 +52,7 @@ private function getCustomerLockExpire(): ?string
5352
}
5453

5554
/**
56-
* @return mixed
55+
* @return string|null
5756
*/
5857
private function getCustomerGridLockExpire(): ?string
5958
{

dev/tests/unit/framework/bootstrap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function setCustomErrorHandler()
3030
{
3131
set_error_handler(
3232
function ($errNo, $errStr, $errFile, $errLine) {
33-
if (error_reporting()) {
33+
$errLevel = error_reporting();
34+
if (($errLevel & $errNo) !== 0) {
3435
$errorNames = [
3536
E_ERROR => 'Error',
3637
E_WARNING => 'Warning',

lib/internal/Magento/Framework/Code/Reader/ClassReader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public function getConstructor($className)
6666
private function getParameterClass(ReflectionParameter $reflectionParameter): ?ReflectionClass
6767
{
6868
$parameterType = $reflectionParameter->getType();
69+
// In PHP8, $parameterType could be an instance of ReflectionUnionType, which doesn't have isBuiltin method.
70+
if ($parameterType !== null && method_exists($parameterType, 'isBuiltin') === false) {
71+
return null;
72+
}
6973

7074
return $parameterType && !$parameterType->isBuiltin()
7175
? new ReflectionClass($parameterType->getName())

lib/internal/Magento/Framework/Code/Reader/NamespaceResolver.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function getImportedNamespaces(array $fileContent)
8686
$classStart = array_search('{', $fileContent);
8787
$fileContent = array_slice($fileContent, 0, $classStart);
8888
$output = [];
89+
8990
foreach ($fileContent as $position => $token) {
9091
if (is_array($token) && $token[0] === T_USE) {
9192
$import = array_slice($fileContent, $position);
@@ -104,8 +105,14 @@ public function getImportedNamespaces(array $fileContent)
104105
$import = array_filter(
105106
$import,
106107
function ($token) {
107-
$whitelist = [T_NS_SEPARATOR, T_STRING, T_AS];
108-
if (isset($token[0]) && in_array($token[0], $whitelist)) {
108+
$whitelist = [
109+
T_NS_SEPARATOR => T_NS_SEPARATOR,
110+
T_STRING => T_STRING,
111+
T_AS => T_AS,
112+
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
113+
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED
114+
];
115+
if (isset($token[0]) && \array_key_exists($token[0], $whitelist)) {
109116
return true;
110117
}
111118
return false;

lib/internal/Magento/Framework/DB/AbstractMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function map(CriteriaInterface $criteria)
125125
if (!is_array($value)) {
126126
throw new \InvalidArgumentException('Wrong type of argument, expecting array for '. $mapperMethod);
127127
}
128-
call_user_func_array([$this, $mapperMethod], $value);
128+
call_user_func_array([$this, $mapperMethod], array_values($value));
129129
}
130130
}
131131
return $this->select;

lib/internal/Magento/Framework/Escaper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ public function encodeUrlParam($string)
303303
*/
304304
public function escapeJs($string)
305305
{
306+
if (!is_string($string) && !is_array($string)) {
307+
// In PHP > 8, preg_replace_callback throws an error if the 3rd param type is incorrect.
308+
// This check emulates an old behavior.
309+
return $string;
310+
}
306311
if ($string === '' || ctype_digit($string)) {
307312
return $string;
308313
}

lib/internal/Magento/Framework/MessageQueue/Rpc/Publisher.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,12 @@ class Publisher implements PublisherInterface
5555
*
5656
* @param ExchangeRepository $exchangeRepository
5757
* @param EnvelopeFactory $envelopeFactory
58-
* @param null $messageQueueConfig @deprecated obsolete dependency
59-
* @param null $amqpConfig @deprecated obsolete dependency
6058
* @param MessageEncoder $messageEncoder
6159
* @param MessageValidator $messageValidator
62-
*
63-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6460
*/
6561
public function __construct(
6662
ExchangeRepository $exchangeRepository,
6763
EnvelopeFactory $envelopeFactory,
68-
$messageQueueConfig = null,
69-
$amqpConfig = null,
7064
MessageEncoder $messageEncoder,
7165
MessageValidator $messageValidator
7266
) {

lib/internal/Magento/Framework/Model/AbstractModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function __construct(
186186
$this->_logger = $context->getLogger();
187187
$this->_actionValidator = $context->getActionValidator();
188188

189-
if (method_exists($this->_resource, 'getIdFieldName')
189+
if ((($this->_resource !== null) && (method_exists($this->_resource, 'getIdFieldName')))
190190
|| $this->_resource instanceof \Magento\Framework\DataObject
191191
) {
192192
$this->_idFieldName = $this->_getResource()->getIdFieldName();

lib/internal/Magento/Framework/Translate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ protected function _addData($data)
332332
continue;
333333
}
334334

335+
$key = is_array($key) ? $key : (string) $key;
336+
$value = is_array($value) ? $value : (string) $value;
335337
$key = str_replace('""', '"', $key);
336338
$value = str_replace('""', '"', $value);
337339

0 commit comments

Comments
 (0)