Skip to content

Commit 541ce18

Browse files
Oleksandr Gorkunal.kravchuk
authored andcommitted
MAGETWO-89899: Proxy generator does not understand PHP 7.1 syntax
1 parent f674bad commit 541ce18

File tree

7 files changed

+91
-8
lines changed

7 files changed

+91
-8
lines changed

dev/tests/integration/testsuite/Magento/Framework/Code/GeneratorTest/SourceClassWithNamespace.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,16 @@ public static function publicChildStatic()
110110
final public function publicChildFinal()
111111
{
112112
}
113+
114+
public function public71(
115+
$arg1,
116+
string $arg2,
117+
?int $arg3,
118+
?int $arg4 = null
119+
): void {
120+
}
121+
122+
public function public71Another(?\DateTime $arg1, $arg2 = null): ?string
123+
{
124+
}
113125
}

dev/tests/integration/testsuite/Magento/Framework/Code/_expected/SourceClassWithNamespaceInterceptor.php.sample

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace;
33

44
/**
5+
* Interceptor class for @see \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
6+
*
57
* Copyright © Magento, Inc. All rights reserved.
68
* See COPYING.txt for license details.
79
*/
@@ -54,6 +56,32 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
5456
}
5557
}
5658

59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function public71($arg1, string $arg2, ?int $arg3, ?int $arg4 = null) : void
63+
{
64+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'public71');
65+
if (!$pluginInfo) {
66+
parent::public71($arg1, $arg2, $arg3, $arg4);
67+
} else {
68+
$this->___callPlugins('public71', func_get_args(), $pluginInfo);
69+
}
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function public71Another(?\DateTime $arg1, $arg2 = null) : ?string
76+
{
77+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'public71Another');
78+
if (!$pluginInfo) {
79+
return parent::public71Another($arg1, $arg2);
80+
} else {
81+
return $this->___callPlugins('public71Another', func_get_args(), $pluginInfo);
82+
}
83+
}
84+
5785
/**
5886
* {@inheritdoc}
5987
*/

dev/tests/integration/testsuite/Magento/Framework/Code/_expected/SourceClassWithNamespaceProxy.php.sample

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ class Proxy extends \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespa
114114
return $this->_getSubject()->publicChildWithoutParameters();
115115
}
116116

117+
/**
118+
* {@inheritdoc}
119+
*/
120+
public function public71($arg1, string $arg2, ?int $arg3, ?int $arg4 = null) : void
121+
{
122+
$this->_getSubject()->public71($arg1, $arg2, $arg3, $arg4);
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function public71Another(?\DateTime $arg1, $arg2 = null) : ?string
129+
{
130+
return $this->_getSubject()->public71Another($arg1, $arg2);
131+
}
132+
117133
/**
118134
* {@inheritdoc}
119135
*/

lib/internal/Magento/Framework/Code/Generator/ClassGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ClassGenerator extends \Zend\Code\Generator\ClassGenerator implements
4747
'abstract' => 'setAbstract',
4848
'visibility' => 'setVisibility',
4949
'body' => 'setBody',
50+
'returntype' => 'setReturnType'
5051
];
5152

5253
/**

lib/internal/Magento/Framework/Code/Generator/EntityAbstract.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
313313
$parameterInfo = [
314314
'name' => $parameter->getName(),
315315
'passedByReference' => $parameter->isPassedByReference(),
316-
'type' => $parameter->getType(),
316+
'type' => $parameter->hasType()
317+
? $parameter->getType()->getName() : null,
317318
'variadic' => $parameter->isVariadic()
318319
];
319320

@@ -336,6 +337,10 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
336337
}
337338
}
338339

340+
if ($parameter->allowsNull() && $parameterInfo['type']) {
341+
$parameterInfo['type'] = '?' .$parameterInfo['type'];
342+
}
343+
339344
return $parameterInfo;
340345
}
341346

lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,24 @@ protected function _getMethodInfo(\ReflectionMethod $method)
102102
$parameters[] = $this->_getMethodParameterInfo($parameter);
103103
}
104104

105+
$returnType = $method->getReturnType();
106+
$returnTypeValue = $returnType
107+
? ($returnType->allowsNull() ? '?' : '') .$returnType->getName()
108+
: null;
105109
$methodInfo = [
106110
'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(),
107111
'parameters' => $parameters,
108112
'body' => "\$pluginInfo = \$this->pluginList->getNext(\$this->subjectType, '{$method->getName()}');\n" .
109113
"if (!\$pluginInfo) {\n" .
110-
" return parent::{$method->getName()}({$this->_getParameterList(
114+
" " .($returnTypeValue === 'void' ? '' : 'return')
115+
." parent::{$method->getName()}({$this->_getParameterList(
111116
$parameters
112117
)});\n" .
113118
"} else {\n" .
114-
" return \$this->___callPlugins('{$method->getName()}', func_get_args(), \$pluginInfo);\n" .
119+
" " .($returnTypeValue === 'void' ? '' : 'return')
120+
." \$this->___callPlugins('{$method->getName()}', func_get_args(), \$pluginInfo);\n" .
115121
"}",
116-
'returnType' => $method->getReturnType(),
122+
'returnType' => $returnTypeValue,
117123
'docblock' => ['shortDescription' => '{@inheritdoc}'],
118124
];
119125

lib/internal/Magento/Framework/ObjectManager/Code/Generator/Proxy.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,20 @@ protected function _getMethodInfo(\ReflectionMethod $method)
160160
$parameters[] = $this->_getMethodParameterInfo($parameter);
161161
}
162162

163+
$returnType = $method->getReturnType();
164+
$returnTypeValue = $returnType
165+
? ($returnType->allowsNull() ? '?' : '') .$returnType->getName()
166+
: null;
163167
$methodInfo = [
164168
'name' => $method->getName(),
165169
'parameters' => $parameters,
166-
'body' => $this->_getMethodBody($method->getName(), $parameterNames),
170+
'body' => $this->_getMethodBody(
171+
$method->getName(),
172+
$parameterNames,
173+
$returnTypeValue === 'void'
174+
),
167175
'docblock' => ['shortDescription' => '{@inheritdoc}'],
176+
'returntype' => $returnTypeValue,
168177
];
169178

170179
return $methodInfo;
@@ -213,16 +222,22 @@ protected function _getDefaultConstructorDefinition()
213222
*
214223
* @param string $name
215224
* @param array $parameters
225+
* @param bool $withoutReturn
216226
* @return string
217227
*/
218-
protected function _getMethodBody($name, array $parameters = [])
219-
{
228+
protected function _getMethodBody(
229+
$name,
230+
array $parameters = [],
231+
bool $withoutReturn = false
232+
) {
220233
if (count($parameters) == 0) {
221234
$methodCall = sprintf('%s()', $name);
222235
} else {
223236
$methodCall = sprintf('%s(%s)', $name, implode(', ', $parameters));
224237
}
225-
return 'return $this->_getSubject()->' . $methodCall . ';';
238+
239+
return ($withoutReturn ? '' : 'return ')
240+
.'$this->_getSubject()->' . $methodCall . ';';
226241
}
227242

228243
/**

0 commit comments

Comments
 (0)