Skip to content

Commit d33db64

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

File tree

4 files changed

+82
-28
lines changed

4 files changed

+82
-28
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ final public function publicChildFinal()
111111
{
112112
}
113113

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+
*/
114124
public function public71(
115125
$arg1,
116126
string $arg2,
@@ -119,7 +129,15 @@ public function public71(
119129
): void {
120130
}
121131

122-
public function public71Another(?\DateTime $arg1, $arg2 = null): ?string
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
123141
{
124142
}
125143
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
7272
/**
7373
* {@inheritdoc}
7474
*/
75-
public function public71Another(?\DateTime $arg1, $arg2 = null) : ?string
75+
public function public71Another(?\DateTime $arg1, $arg2 = false) : ?string
7676
{
7777
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'public71Another');
7878
if (!$pluginInfo) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Proxy extends \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespa
125125
/**
126126
* {@inheritdoc}
127127
*/
128-
public function public71Another(?\DateTime $arg1, $arg2 = null) : ?string
128+
public function public71Another(?\DateTime $arg1, $arg2 = false) : ?string
129129
{
130130
return $this->_getSubject()->public71Another($arg1, $arg2);
131131
}

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

Lines changed: 61 additions & 25 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,32 +368,13 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
313368
$parameterInfo = [
314369
'name' => $parameter->getName(),
315370
'passedByReference' => $parameter->isPassedByReference(),
316-
'type' => $parameter->hasType()
317-
? $parameter->getType()->getName() : null,
318371
'variadic' => $parameter->isVariadic()
319372
];
320-
321-
if ($parameter->isArray()) {
322-
$parameterInfo['type'] = 'array';
323-
} elseif ($parameter->getClass()) {
324-
$parameterInfo['type'] = $this->_getFullyQualifiedClassName($parameter->getClass()->getName());
325-
} elseif ($parameter->isCallable()) {
326-
$parameterInfo['type'] = 'callable';
327-
}
328-
329-
if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) {
330-
$defaultValue = $parameter->getDefaultValue();
331-
if (is_string($defaultValue)) {
332-
$parameterInfo['defaultValue'] = $parameter->getDefaultValue();
333-
} elseif ($defaultValue === null) {
334-
$parameterInfo['defaultValue'] = $this->_getNullDefaultValue();
335-
} else {
336-
$parameterInfo['defaultValue'] = $defaultValue;
337-
}
373+
if ($type = $this->extractParameterType($parameter)) {
374+
$parameterInfo['type'] = $type;
338375
}
339-
340-
if ($parameter->allowsNull() && $parameterInfo['type']) {
341-
$parameterInfo['type'] = '?' .$parameterInfo['type'];
376+
if ($default = $this->extractParameterDefaultValue($parameter)) {
377+
$parameterInfo['defaultValue'] = $default;
342378
}
343379

344380
return $parameterInfo;

0 commit comments

Comments
 (0)