Skip to content

Commit 0bf7b7f

Browse files
committed
Fix static tests for php8 compatibility
1 parent f5f6b61 commit 0bf7b7f

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

dev/tests/static/framework/Magento/TestFramework/Integrity/Library/PhpParser/StaticCalls.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ protected function getClassByStaticCall($staticCall)
8282
{
8383
$step = 1;
8484
$staticClassParts = [];
85+
86+
$token = $this->tokens->getTokenCodeByKey($staticCall - $step);
87+
if ($token === T_NAME_FULLY_QUALIFIED || $token === T_NAME_QUALIFIED) {
88+
return $this->tokens->getTokenValueByKey($staticCall - $step);
89+
}
90+
91+
// PHP 7 compatibility
8592
while ($this->tokens->getTokenCodeByKey(
8693
$staticCall - $step
8794
) == T_STRING || $this->tokens->getTokenCodeByKey(
@@ -90,6 +97,7 @@ protected function getClassByStaticCall($staticCall)
9097
$staticClassParts[] = $this->tokens->getTokenValueByKey($staticCall - $step);
9198
$step++;
9299
}
100+
93101
return implode(array_reverse($staticClassParts));
94102
}
95103

dev/tests/static/framework/Magento/TestFramework/Integrity/Library/PhpParser/Throws.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,19 @@ public function getDependencies(Uses $uses)
5757
$class = '';
5858
if ($this->tokens->getTokenCodeByKey($throw + 2) == T_NEW) {
5959
$step = 4;
60-
while ($this->tokens->getTokenCodeByKey(
61-
$throw + $step
62-
) == T_STRING || $this->tokens->getTokenCodeByKey(
63-
$throw + $step
64-
) == T_NS_SEPARATOR) {
65-
$class .= trim($this->tokens->getTokenValueByKey($throw + $step));
66-
$step++;
60+
61+
$token = $this->tokens->getTokenCodeByKey($throw + $step);
62+
if ($token === T_NAME_FULLY_QUALIFIED || $token === T_NAME_QUALIFIED) {
63+
$class = $this->tokens->getTokenValueByKey($throw + $step);
64+
} else {
65+
// PHP 7 compatibility
66+
while ($this->tokens->getTokenCodeByKey($throw + $step) === T_STRING
67+
|| $this->tokens->getTokenCodeByKey($throw + $step) === T_NS_SEPARATOR) {
68+
$class .= trim($this->tokens->getTokenValueByKey($throw + $step));
69+
$step++;
70+
}
6771
}
72+
6873
if ($uses->hasUses()) {
6974
$class = $uses->getClassNameWithNamespace($class);
7075
}

dev/tests/static/testsuite/Magento/Test/Integrity/Di/CompilerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ private function getPluginBlacklist(): array
127127
);
128128
$blacklistItems = [];
129129
foreach (glob($blacklistFiles) as $fileName) {
130+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
130131
$blacklistItems = array_merge(
131132
$blacklistItems,
132133
file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
@@ -243,6 +244,7 @@ protected function _phpClassesDataProvider()
243244
$allowedFiles = array_keys($classes);
244245
foreach ($classes as $class) {
245246
if (!in_array($class, $output)) {
247+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
246248
$output = array_merge($output, $this->_buildInheritanceHierarchyTree($class, $allowedFiles));
247249
$output = array_unique($output);
248250
}
@@ -409,7 +411,7 @@ protected function pluginDataProvider()
409411
$plugin = $node->attributes->getNamedItem('type')->nodeValue;
410412
if (!in_array($plugin, $this->getPluginBlacklist())) {
411413
$plugin = \Magento\Framework\App\Utility\Classes::resolveVirtualType($plugin);
412-
$plugins[] = ['plugin' => $plugin, 'intercepted type' => $type];
414+
$plugins[] = [$plugin, $type];
413415
}
414416
}
415417
}

dev/tests/static/testsuite/Magento/Test/Integrity/Magento/Framework/Api/ExtensibleInterfacesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private function checkSetExtensionAttributes(
135135
} else {
136136
// Get the parameter name via a regular expression capture because the class may
137137
// not exist which causes a fatal error
138-
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $methodParameters[0]->__toString(), $matches);
138+
preg_match('/\[\s\<\w+?>\s([?]?[\w]+)/s', $methodParameters[0]->__toString(), $matches);
139139
$isCorrectParameter = false;
140140
if (isset($matches[1]) && '\\' . $matches[1] != $extensionInterfaceName) {
141141
$isCorrectParameter = true;

dev/tests/static/testsuite/Magento/Test/Integrity/PublicCodeTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPUnit\Framework\TestCase;
1212
use ReflectionClass;
1313
use ReflectionException;
14+
use ReflectionMethod;
1415
use ReflectionParameter;
1516

1617
/**
@@ -111,9 +112,9 @@ public function testAllPHPClassesReferencedFromPublicClassesArePublic($class)
111112
{
112113
$nonPublishedClasses = [];
113114
$reflection = new \ReflectionClass($class);
114-
$filter = \ReflectionMethod::IS_PUBLIC;
115+
$filter = ReflectionMethod::IS_PUBLIC;
115116
if ($reflection->isAbstract()) {
116-
$filter = $filter | \ReflectionMethod::IS_PROTECTED;
117+
$filter = $filter | ReflectionMethod::IS_PROTECTED;
117118
}
118119
$methods = $reflection->getMethods($filter);
119120
foreach ($methods as $method) {
@@ -125,8 +126,11 @@ public function testAllPHPClassesReferencedFromPublicClassesArePublic($class)
125126
is written on early php 7 when return types are not actively used */
126127
$returnTypes = [];
127128
if ($method->hasReturnType()) {
128-
if (!$method->getReturnType()->isBuiltin()) {
129-
$returnTypes = [trim($method->getReturnType()->getName(), '?[]')];
129+
$methodReturnType = $method->getReturnType();
130+
// For PHP 8.0 - ReflectionUnionType doesn't have isBuiltin method.
131+
if (method_exists($methodReturnType, 'isBuiltin')
132+
&& !$methodReturnType->isBuiltin()) {
133+
$returnTypes = [trim($methodReturnType->getName(), '?[]')];
130134
}
131135
} else {
132136
$returnTypes = $this->getReturnTypesFromDocComment($method->getDocComment());
@@ -260,16 +264,20 @@ private function checkReturnValues($class, array $returnTypes, array $nonPublish
260264

261265
/**
262266
* Check if all method parameters are public
267+
*
263268
* @param string $class
264-
* @param \ReflectionMethod $method
269+
* @param ReflectionMethod $method
265270
* @param array $nonPublishedClasses
271+
*
266272
* @return array
273+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
267274
*/
268-
private function checkParameters($class, \ReflectionMethod $method, array $nonPublishedClasses)
275+
private function checkParameters($class, ReflectionMethod $method, array $nonPublishedClasses)
269276
{
270277
/* Ignoring docblocks for argument types */
271278
foreach ($method->getParameters() as $parameter) {
272279
if ($parameter->hasType()
280+
&& method_exists($parameter->getType(), 'isBuiltin')
273281
&& !$parameter->getType()->isBuiltin()
274282
&& !$this->isGenerated($parameter->getType()->getName())
275283
) {

dev/tests/static/testsuite/Magento/Test/Integrity/Readme/ReadmeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function getDirectories()
5757
$directories = [];
5858
foreach ($this->scanList as $dir) {
5959
if (!$this->isInBlacklist($dir)) {
60-
$directories[][$dir] = $dir;
60+
$directories[][] = $dir;
6161
}
6262
}
6363

0 commit comments

Comments
 (0)