Skip to content

Commit ef4ca85

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-89540: Static CompilerTest doesn't understand nullable type hint
1 parent 9fcc4da commit ef4ca85

File tree

3 files changed

+37
-46
lines changed

3 files changed

+37
-46
lines changed

dev/tests/integration/testsuite/Magento/Framework/Code/Reader/SourceArgumentsReaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function getConstructorArgumentTypesDataProvider()
4545
'\Imported\Name\Space\ClassName',
4646
'\Some\Testing\Name\Space\Test',
4747
'\Exception',
48+
'',
4849
'\Imported\Name\Space\ClassName',
4950
'array',
5051
''

dev/tests/integration/testsuite/Magento/Framework/Code/Reader/_files/SourceArgumentsReaderTest.php.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class AnotherSimpleClass
2020
ClassName $itemSix,
2121
Test $itemSeven,
2222
?\Exception $optionalException,
23+
$defaultString = '$default),value;',
2324
?ClassName $optionalWithDefaultValue = null,
2425
array $itemEight = [],
2526
$itemNine = 'test'

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

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,61 +37,48 @@ public function __construct(NamespaceResolver $namespaceResolver = null)
3737
* @SuppressWarnings(PHPMD.NPathComplexity)
3838
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
3939
*/
40-
public function getConstructorArgumentTypes(\ReflectionClass $class, $inherited = false)
41-
{
40+
public function getConstructorArgumentTypes(
41+
\ReflectionClass $class,
42+
$inherited = false
43+
) {
4244
$output = [null];
4345
if (!$class->getFileName() || false == $class->hasMethod(
4446
'__construct'
4547
) || !$inherited && $class->getConstructor()->class !== $class->getName()
4648
) {
4749
return $output;
4850
}
49-
$reflectionConstructor = $class->getConstructor();
50-
$fileContent = file($class->getFileName());
51-
$availableNamespaces = $this->namespaceResolver->getImportedNamespaces($fileContent);
52-
$availableNamespaces[0] = $class->getNamespaceName();
53-
$constructorStartLine = $reflectionConstructor->getStartLine() - 1;
54-
$constructorEndLine = $reflectionConstructor->getEndLine();
55-
$fileContent = array_slice($fileContent, $constructorStartLine, $constructorEndLine - $constructorStartLine);
56-
$source = '<?php ' . trim(implode('', $fileContent));
57-
58-
// Remove parameter default value.
59-
$source = preg_replace("/(\s*)=([^\,\)]*)/", '', $source);
6051

61-
$methodTokenized = token_get_all($source);
62-
$argumentsStart = array_search('(', $methodTokenized) + 1;
63-
$argumentsEnd = array_search(')', $methodTokenized);
64-
$arguments = array_slice($methodTokenized, $argumentsStart, $argumentsEnd - $argumentsStart);
65-
foreach ($arguments as &$argument) {
66-
is_array($argument) ?: $argument = [1 => $argument];
67-
}
68-
unset($argument);
69-
$arguments = array_filter($arguments, function ($token) {
70-
$blacklist = [T_VARIABLE, T_WHITESPACE];
71-
if (isset($token[0]) && in_array($token[0], $blacklist)) {
72-
return false;
52+
//Reading parameters' types.
53+
$params = $class->getConstructor()->getParameters();
54+
/** @var string[] $types */
55+
$types = [];
56+
foreach ($params as $param) {
57+
//For the sake of backward compatibility.
58+
$typeName = '';
59+
if ($param->isArray()) {
60+
//For the sake of backward compatibility.
61+
$typeName = 'array';
62+
} else {
63+
try {
64+
$paramClass = $param->getClass();
65+
if ($paramClass) {
66+
$typeName = '\\' .$paramClass->getName();
67+
}
68+
} catch (\ReflectionException $exception) {
69+
//If there's a problem loading a class then ignore it and
70+
//just return it's name.
71+
$typeName = '\\' .$param->getType()->getName();
72+
}
7373
}
74-
return true;
75-
});
76-
$arguments = array_map(function ($element) {
77-
return $element[1];
78-
}, $arguments);
79-
$arguments = array_values($arguments);
80-
$arguments = implode('', $arguments);
81-
if (empty($arguments)) {
82-
return $output;
74+
$types[] = $typeName;
8375
}
84-
$arguments = explode(',', $arguments);
85-
foreach ($arguments as $key => &$argument) {
86-
$argument = $this->removeToken($argument, '=');
87-
$argument = $this->removeToken($argument, '&');
88-
if (mb_strpos($argument, '?') === 0) {
89-
$argument = mb_substr($argument, 1);
90-
}
91-
$argument = $this->namespaceResolver->resolveNamespace($argument, $availableNamespaces);
76+
if (!$types) {
77+
//For the sake of backward compatibility.
78+
$types = [null];
9279
}
93-
unset($argument);
94-
return $arguments;
80+
81+
return $types;
9582
}
9683

9784
/**
@@ -101,7 +88,7 @@ public function getConstructorArgumentTypes(\ReflectionClass $class, $inherited
10188
* @param array $availableNamespaces
10289
* @return string
10390
* @deprecated 100.2.0
104-
* @see \Magento\Framework\Code\Reader\NamespaceResolver::resolveNamespace
91+
* @see getConstructorArgumentTypes
10592
*/
10693
protected function resolveNamespaces($argument, $availableNamespaces)
10794
{
@@ -114,6 +101,8 @@ protected function resolveNamespaces($argument, $availableNamespaces)
114101
* @param string $argument
115102
* @param string $token
116103
* @return string
104+
*
105+
* @deprecated Not used anymore.
117106
*/
118107
protected function removeToken($argument, $token)
119108
{
@@ -130,7 +119,7 @@ protected function removeToken($argument, $token)
130119
* @param array $file
131120
* @return array
132121
* @deprecated 100.2.0
133-
* @see \Magento\Framework\Code\Reader\NamespaceResolver::getImportedNamespaces
122+
* @see getConstructorArgumentTypes
134123
*/
135124
protected function getImportedNamespaces(array $file)
136125
{

0 commit comments

Comments
 (0)