Skip to content
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

Fix static tests to be php8 compatible #34192

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,7 +13,7 @@
class CurlClient extends \OAuth\Common\Http\Client\CurlClient
{
/**
* {@inheritdoc}
* @inheritdoc
*/
public function retrieveResponse(
UriInterface $endpoint,
Expand Down
Expand Up @@ -82,6 +82,13 @@ protected function getClassByStaticCall($staticCall)
{
$step = 1;
$staticClassParts = [];

$token = $this->tokens->getTokenCodeByKey($staticCall - $step);
if ($token === T_NAME_FULLY_QUALIFIED || $token === T_NAME_QUALIFIED) {
return $this->tokens->getTokenValueByKey($staticCall - $step);
}

// PHP 7 compatibility
while ($this->tokens->getTokenCodeByKey(
$staticCall - $step
) == T_STRING || $this->tokens->getTokenCodeByKey(
Expand All @@ -90,6 +97,7 @@ protected function getClassByStaticCall($staticCall)
$staticClassParts[] = $this->tokens->getTokenValueByKey($staticCall - $step);
$step++;
}

return implode(array_reverse($staticClassParts));
}

Expand Down
Expand Up @@ -57,14 +57,19 @@ public function getDependencies(Uses $uses)
$class = '';
if ($this->tokens->getTokenCodeByKey($throw + 2) == T_NEW) {
$step = 4;
while ($this->tokens->getTokenCodeByKey(
$throw + $step
) == T_STRING || $this->tokens->getTokenCodeByKey(
$throw + $step
) == T_NS_SEPARATOR) {
$class .= trim($this->tokens->getTokenValueByKey($throw + $step));
$step++;

$token = $this->tokens->getTokenCodeByKey($throw + $step);
if ($token === T_NAME_FULLY_QUALIFIED || $token === T_NAME_QUALIFIED) {
$class = $this->tokens->getTokenValueByKey($throw + $step);
} else {
// PHP 7 compatibility
while ($this->tokens->getTokenCodeByKey($throw + $step) === T_STRING
|| $this->tokens->getTokenCodeByKey($throw + $step) === T_NS_SEPARATOR) {
$class .= trim($this->tokens->getTokenValueByKey($throw + $step));
$step++;
}
}

if ($uses->hasUses()) {
$class = $uses->getClassNameWithNamespace($class);
}
Expand Down
Expand Up @@ -127,11 +127,9 @@ private function getPluginBlacklist(): array
);
$blacklistItems = [];
foreach (glob($blacklistFiles) as $fileName) {
$blacklistItems = array_merge(
$blacklistItems,
file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
);
$blacklistItems[] = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
$blacklistItems = array_merge([], ...$blacklistItems);
$this->pluginBlacklist = $blacklistItems;
}
return $this->pluginBlacklist;
Expand Down Expand Up @@ -243,10 +241,10 @@ protected function _phpClassesDataProvider()
$allowedFiles = array_keys($classes);
foreach ($classes as $class) {
if (!in_array($class, $output)) {
$output = array_merge($output, $this->_buildInheritanceHierarchyTree($class, $allowedFiles));
$output = array_unique($output);
$output[] = $this->_buildInheritanceHierarchyTree($class, $allowedFiles);
}
}
$output = array_unique(array_merge([], ...$output));

/** Convert data into data provider format */
$outputClasses = [];
Expand Down Expand Up @@ -409,7 +407,7 @@ protected function pluginDataProvider()
$plugin = $node->attributes->getNamedItem('type')->nodeValue;
if (!in_array($plugin, $this->getPluginBlacklist())) {
$plugin = \Magento\Framework\App\Utility\Classes::resolveVirtualType($plugin);
$plugins[] = ['plugin' => $plugin, 'intercepted type' => $type];
$plugins[] = [$plugin, $type];
}
}
}
Expand Down
Expand Up @@ -135,9 +135,10 @@ private function checkSetExtensionAttributes(
} else {
// Get the parameter name via a regular expression capture because the class may
// not exist which causes a fatal error
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $methodParameters[0]->__toString(), $matches);
preg_match('/\[\s\<\w+?>\s([?]?[\w\\\]+)/s', $methodParameters[0]->__toString(), $matches);
$isCorrectParameter = false;
if (isset($matches[1]) && '\\' . $matches[1] != $extensionInterfaceName) {
if (isset($matches[1])
&& ('\\' . ltrim($matches[1], '?')) === $extensionInterfaceName) {
$isCorrectParameter = true;
}

Expand Down
Expand Up @@ -11,6 +11,7 @@
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionParameter;

/**
Expand Down Expand Up @@ -111,9 +112,9 @@ public function testAllPHPClassesReferencedFromPublicClassesArePublic($class)
{
$nonPublishedClasses = [];
$reflection = new \ReflectionClass($class);
$filter = \ReflectionMethod::IS_PUBLIC;
$filter = ReflectionMethod::IS_PUBLIC;
if ($reflection->isAbstract()) {
$filter = $filter | \ReflectionMethod::IS_PROTECTED;
$filter = $filter | ReflectionMethod::IS_PROTECTED;
}
$methods = $reflection->getMethods($filter);
foreach ($methods as $method) {
Expand All @@ -125,8 +126,11 @@ public function testAllPHPClassesReferencedFromPublicClassesArePublic($class)
is written on early php 7 when return types are not actively used */
$returnTypes = [];
if ($method->hasReturnType()) {
if (!$method->getReturnType()->isBuiltin()) {
$returnTypes = [trim($method->getReturnType()->getName(), '?[]')];
$methodReturnType = $method->getReturnType();
// For PHP 8.0 - ReflectionUnionType doesn't have isBuiltin method.
if (method_exists($methodReturnType, 'isBuiltin')
&& !$methodReturnType->isBuiltin()) {
$returnTypes = [trim($methodReturnType->getName(), '?[]')];
}
} else {
$returnTypes = $this->getReturnTypesFromDocComment($method->getDocComment());
Expand Down Expand Up @@ -260,16 +264,20 @@ private function checkReturnValues($class, array $returnTypes, array $nonPublish

/**
* Check if all method parameters are public
*
* @param string $class
* @param \ReflectionMethod $method
* @param ReflectionMethod $method
* @param array $nonPublishedClasses
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function checkParameters($class, \ReflectionMethod $method, array $nonPublishedClasses)
private function checkParameters($class, ReflectionMethod $method, array $nonPublishedClasses)
{
/* Ignoring docblocks for argument types */
foreach ($method->getParameters() as $parameter) {
if ($parameter->hasType()
&& method_exists($parameter->getType(), 'isBuiltin')
&& !$parameter->getType()->isBuiltin()
&& !$this->isGenerated($parameter->getType()->getName())
) {
Expand Down
Expand Up @@ -57,7 +57,7 @@ private function getDirectories()
$directories = [];
foreach ($this->scanList as $dir) {
if (!$this->isInBlacklist($dir)) {
$directories[][$dir] = $dir;
$directories[][] = $dir;
}
}

Expand Down