Skip to content

Commit 95f8d36

Browse files
authored
Merge pull request #34062 from karyna-tsymbal-atwix/php8-compatibility/fixes-for-launch-magento-in-php8
Added fixes to launch Magento on PHP 8
2 parents a958eac + 1a8f234 commit 95f8d36

File tree

23 files changed

+179
-82
lines changed

23 files changed

+179
-82
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/api-functional/framework/bootstrap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ function setCustomErrorHandler()
123123
{
124124
set_error_handler(
125125
function ($errNo, $errStr, $errFile, $errLine) {
126-
if (error_reporting()) {
126+
$errLevel = error_reporting();
127+
if (($errLevel & $errNo) !== 0) {
127128
$errorNames = [
128129
E_ERROR => 'Error',
129130
E_WARNING => 'Warning',

dev/tests/error_handler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ function setCustomErrorHandler()
1111
{
1212
set_error_handler(
1313
function ($errNo, $errStr, $errFile, $errLine) {
14-
if (error_reporting()) {
14+
$errLevel = error_reporting();
15+
if (($errLevel & $errNo) !== 0) {
1516
$errorNames = [
1617
E_ERROR => 'Error',
1718
E_WARNING => 'Warning',

dev/tests/integration/framework/bootstrap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ function setCustomErrorHandler()
131131
{
132132
set_error_handler(
133133
function ($errNo, $errStr, $errFile, $errLine) {
134-
if (error_reporting()) {
134+
$errLevel = error_reporting();
135+
if (($errLevel & $errNo) !== 0) {
135136
$errorNames = [
136137
E_ERROR => 'Error',
137138
E_WARNING => 'Warning',

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/static/framework/bootstrap.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
define('TESTS_TEMP_DIR', dirname(__DIR__) . '/tmp');
2121
}
2222

23+
// PHP 8 compatibility. Define constants that are not present in PHP < 8.0
24+
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80000) {
25+
if (!defined('T_NAME_QUALIFIED')) {
26+
define('T_NAME_QUALIFIED', 24001);
27+
}
28+
if (!defined('T_NAME_FULLY_QUALIFIED')) {
29+
define('T_NAME_FULLY_QUALIFIED', 24002);
30+
}
31+
}
32+
2333
setCustomErrorHandler();
2434

2535
$componentRegistrar = new ComponentRegistrar();
@@ -38,7 +48,8 @@ function setCustomErrorHandler()
3848
{
3949
set_error_handler(
4050
function ($errNo, $errStr, $errFile, $errLine) {
41-
if (error_reporting()) {
51+
$errLevel = error_reporting();
52+
if (($errLevel & $errNo) !== 0) {
4253
$errorNames = [
4354
E_ERROR => 'Error',
4455
E_WARNING => 'Warning',

dev/tests/unit/framework/bootstrap.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
define('TESTS_TEMP_DIR', dirname(__DIR__) . '/tmp');
1111
}
1212

13+
// PHP 8 compatibility. Define constants that are not present in PHP < 8.0
14+
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80000) {
15+
if (!defined('T_NAME_QUALIFIED')) {
16+
define('T_NAME_QUALIFIED', 24001);
17+
}
18+
if (!defined('T_NAME_FULLY_QUALIFIED')) {
19+
define('T_NAME_FULLY_QUALIFIED', 24002);
20+
}
21+
}
22+
1323
require_once __DIR__ . '/autoload.php';
1424

1525
setCustomErrorHandler();
@@ -30,7 +40,8 @@ function setCustomErrorHandler()
3040
{
3141
set_error_handler(
3242
function ($errNo, $errStr, $errFile, $errLine) {
33-
if (error_reporting()) {
43+
$errLevel = error_reporting();
44+
if (($errLevel & $errNo) !== 0) {
3445
$errorNames = [
3546
E_ERROR => 'Error',
3647
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], $whitelist[$token[0]])) {
109116
return true;
110117
}
111118
return false;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\DB;
78

89
use Magento\Framework\Api\CriteriaInterface;
@@ -125,7 +126,8 @@ public function map(CriteriaInterface $criteria)
125126
if (!is_array($value)) {
126127
throw new \InvalidArgumentException('Wrong type of argument, expecting array for '. $mapperMethod);
127128
}
128-
call_user_func_array([$this, $mapperMethod], $value);
129+
// The `array_values` is a workaround to ensure the same behavior in PHP 7 and 8.
130+
call_user_func_array([$this, $mapperMethod], array_values($value));
129131
}
130132
}
131133
return $this->select;
@@ -392,9 +394,10 @@ protected function getConditionFieldName($fieldName)
392394

393395
/**
394396
* Hook for operations before rendering filters
397+
*
395398
* @return void
396399
*/
397-
protected function renderFiltersBefore()
400+
protected function renderFiltersBefore() //phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
398401
{
399402
}
400403

0 commit comments

Comments
 (0)