Skip to content

Commit 3805ac2

Browse files
committed
Merge remote-tracking branch 'origin/2.3-develop' into 2.3-develop-pr14
2 parents aaa6a0a + c46a17c commit 3805ac2

File tree

7 files changed

+178
-33
lines changed

7 files changed

+178
-33
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,34 @@ public static function publicChildStatic()
110110
final public function publicChildFinal()
111111
{
112112
}
113+
114+
/**
115+
* @param mixed $arg1
116+
* @param string $arg2
117+
* @param int|null $arg3
118+
* @param int|null $arg4
119+
*
120+
* @return void
121+
*
122+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
123+
*/
124+
public function public71(
125+
$arg1,
126+
string $arg2,
127+
?int $arg3,
128+
?int $arg4 = null
129+
): void {
130+
}
131+
132+
/**
133+
* @param \DateTime|null $arg1
134+
* @param mixed $arg2
135+
*
136+
* @return null|string
137+
*
138+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
139+
*/
140+
public function public71Another(?\DateTime $arg1, $arg2 = false): ?string
141+
{
142+
}
113143
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
5454
}
5555
}
5656

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

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 = false) : ?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: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Framework\Code\Generator;
77

8+
use Zend\Code\Generator\ValueGenerator;
9+
810
abstract class EntityAbstract
911
{
1012
/**
@@ -293,11 +295,64 @@ protected function _fixCodeStyle($sourceCode)
293295
/**
294296
* Get value generator for null default value
295297
*
296-
* @return \Zend\Code\Generator\ValueGenerator
298+
* @return ValueGenerator
297299
*/
298300
protected function _getNullDefaultValue()
299301
{
300-
$value = new \Zend\Code\Generator\ValueGenerator(null, \Zend\Code\Generator\ValueGenerator::TYPE_NULL);
302+
$value = new ValueGenerator(null, ValueGenerator::TYPE_NULL);
303+
304+
return $value;
305+
}
306+
307+
/**
308+
* @param \ReflectionParameter $parameter
309+
*
310+
* @return null|string
311+
*/
312+
private function extractParameterType(
313+
\ReflectionParameter $parameter
314+
): ?string {
315+
/** @var string|null $typeName */
316+
$typeName = null;
317+
if ($parameter->hasType()) {
318+
if ($parameter->isArray()) {
319+
$typeName = 'array';
320+
} elseif ($parameter->getClass()) {
321+
$typeName = $this->_getFullyQualifiedClassName(
322+
$parameter->getClass()->getName()
323+
);
324+
} elseif ($parameter->isCallable()) {
325+
$typeName = 'callable';
326+
} else {
327+
$typeName = $parameter->getType()->getName();
328+
}
329+
330+
if ($parameter->allowsNull()) {
331+
$typeName = '?' .$typeName;
332+
}
333+
}
334+
335+
return $typeName;
336+
}
337+
338+
/**
339+
* @param \ReflectionParameter $parameter
340+
*
341+
* @return null|ValueGenerator
342+
*/
343+
private function extractParameterDefaultValue(
344+
\ReflectionParameter $parameter
345+
): ?ValueGenerator {
346+
/** @var ValueGenerator|null $value */
347+
$value = null;
348+
if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) {
349+
$valueType = ValueGenerator::TYPE_AUTO;
350+
$defaultValue = $parameter->getDefaultValue();
351+
if ($defaultValue === null) {
352+
$valueType = ValueGenerator::TYPE_NULL;
353+
}
354+
$value = new ValueGenerator($defaultValue, $valueType);
355+
}
301356

302357
return $value;
303358
}
@@ -313,27 +368,13 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
313368
$parameterInfo = [
314369
'name' => $parameter->getName(),
315370
'passedByReference' => $parameter->isPassedByReference(),
316-
'type' => $parameter->getType(),
317371
'variadic' => $parameter->isVariadic()
318372
];
319-
320-
if ($parameter->isArray()) {
321-
$parameterInfo['type'] = 'array';
322-
} elseif ($parameter->getClass()) {
323-
$parameterInfo['type'] = $this->_getFullyQualifiedClassName($parameter->getClass()->getName());
324-
} elseif ($parameter->isCallable()) {
325-
$parameterInfo['type'] = 'callable';
373+
if ($type = $this->extractParameterType($parameter)) {
374+
$parameterInfo['type'] = $type;
326375
}
327-
328-
if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) {
329-
$defaultValue = $parameter->getDefaultValue();
330-
if (is_string($defaultValue)) {
331-
$parameterInfo['defaultValue'] = $parameter->getDefaultValue();
332-
} elseif ($defaultValue === null) {
333-
$parameterInfo['defaultValue'] = $this->_getNullDefaultValue();
334-
} else {
335-
$parameterInfo['defaultValue'] = $defaultValue;
336-
}
376+
if ($default = $this->extractParameterDefaultValue($parameter)) {
377+
$parameterInfo['defaultValue'] = $default;
337378
}
338379

339380
return $parameterInfo;

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,34 @@ 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,
108-
'body' => "\$pluginInfo = \$this->pluginList->getNext(\$this->subjectType, '{$method->getName()}');\n" .
109-
"if (!\$pluginInfo) {\n" .
110-
" return parent::{$method->getName()}({$this->_getParameterList(
111-
$parameters
112-
)});\n" .
113-
"} else {\n" .
114-
" return \$this->___callPlugins('{$method->getName()}', func_get_args(), \$pluginInfo);\n" .
115-
"}",
116-
'returnType' => $method->getReturnType(),
112+
'body' => str_replace(
113+
[
114+
'%methodName%',
115+
'%return%',
116+
'%parameters%'
117+
],
118+
[
119+
$method->getName(),
120+
$returnTypeValue === 'void' ? '' : ' return',
121+
$this->_getParameterList($parameters)
122+
],
123+
<<<'METHOD_BODY'
124+
$pluginInfo = $this->pluginList->getNext($this->subjectType, '%methodName%');
125+
if (!$pluginInfo) {
126+
%return% parent::%methodName%(%parameters%);
127+
} else {
128+
%return% $this->___callPlugins('%methodName%', func_get_args(), $pluginInfo);
129+
}
130+
METHOD_BODY
131+
),
132+
'returnType' => $returnTypeValue,
117133
'docblock' => ['shortDescription' => '{@inheritdoc}'],
118134
];
119135

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

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

162+
$returnType = $method->getReturnType();
163+
$returnTypeValue = $returnType
164+
? ($returnType->allowsNull() ? '?' : '') .$returnType->getName()
165+
: null;
162166
$methodInfo = [
163167
'name' => $method->getName(),
164168
'parameters' => $parameters,
165-
'body' => $this->_getMethodBody($method->getName(), $parameterNames),
169+
'body' => $this->_getMethodBody(
170+
$method->getName(),
171+
$parameterNames,
172+
$returnTypeValue === 'void'
173+
),
166174
'docblock' => ['shortDescription' => '{@inheritdoc}'],
175+
'returntype' => $returnTypeValue,
167176
];
168177

169178
return $methodInfo;
@@ -212,16 +221,22 @@ protected function _getDefaultConstructorDefinition()
212221
*
213222
* @param string $name
214223
* @param array $parameters
224+
* @param bool $withoutReturn
215225
* @return string
216226
*/
217-
protected function _getMethodBody($name, array $parameters = [])
218-
{
227+
protected function _getMethodBody(
228+
$name,
229+
array $parameters = [],
230+
bool $withoutReturn = false
231+
) {
219232
if (count($parameters) == 0) {
220233
$methodCall = sprintf('%s()', $name);
221234
} else {
222235
$methodCall = sprintf('%s(%s)', $name, implode(', ', $parameters));
223236
}
224-
return 'return $this->_getSubject()->' . $methodCall . ';';
237+
238+
return ($withoutReturn ? '' : 'return ')
239+
.'$this->_getSubject()->' . $methodCall . ';';
225240
}
226241

227242
/**

0 commit comments

Comments
 (0)