Skip to content

Commit 61814ad

Browse files
Merge pull request #2523 from magento-qwerty/MAGETWO-89540
Fixed issues: - MAGETWO-89540: Static CompilerTest doesn't understand nullable type hint
2 parents 4cf1a32 + 03c4b12 commit 61814ad

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function getConstructorArgumentTypesDataProvider()
4444
'\Imported\Name\Space\ClassName\Under\Test',
4545
'\Imported\Name\Space\ClassName',
4646
'\Some\Testing\Name\Space\Test',
47+
'\Exception',
48+
'',
49+
'\Imported\Name\Space\ClassName',
4750
'array',
4851
''
4952
],

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class AnotherSimpleClass
1919
ClassName\Under\Test $itemFive,
2020
ClassName $itemSix,
2121
Test $itemSeven,
22+
?\Exception $optionalException,
23+
$defaultString = '$default),value;',
24+
?ClassName $optionalWithDefaultValue = null,
2225
array $itemEight = [],
2326
$itemNine = 'test'
2427
) {

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

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,58 +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("/ = (.*)/", ',)', $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-
$argument = $this->namespaceResolver->resolveNamespace($argument, $availableNamespaces);
76+
if (!$types) {
77+
//For the sake of backward compatibility.
78+
$types = [null];
8979
}
90-
unset($argument);
91-
return $arguments;
80+
81+
return $types;
9282
}
9383

9484
/**
@@ -98,7 +88,7 @@ public function getConstructorArgumentTypes(\ReflectionClass $class, $inherited
9888
* @param array $availableNamespaces
9989
* @return string
10090
* @deprecated 100.2.0
101-
* @see \Magento\Framework\Code\Reader\NamespaceResolver::resolveNamespace
91+
* @see getConstructorArgumentTypes
10292
*/
10393
protected function resolveNamespaces($argument, $availableNamespaces)
10494
{
@@ -111,6 +101,8 @@ protected function resolveNamespaces($argument, $availableNamespaces)
111101
* @param string $argument
112102
* @param string $token
113103
* @return string
104+
*
105+
* @deprecated Not used anymore.
114106
*/
115107
protected function removeToken($argument, $token)
116108
{
@@ -127,7 +119,7 @@ protected function removeToken($argument, $token)
127119
* @param array $file
128120
* @return array
129121
* @deprecated 100.2.0
130-
* @see \Magento\Framework\Code\Reader\NamespaceResolver::getImportedNamespaces
122+
* @see getConstructorArgumentTypes
131123
*/
132124
protected function getImportedNamespaces(array $file)
133125
{

0 commit comments

Comments
 (0)