Skip to content

Commit

Permalink
MAGETWO-72283: Code generate: support variadic parameter #10771
Browse files Browse the repository at this point in the history
  • Loading branch information
ishakhsuvarov authored Sep 7, 2017
2 parents 6296647 + b7cc191 commit aa2915b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ClassGenerator extends \Zend\Code\Generator\ClassGenerator implements
'type' => 'setType',
'defaultValue' => 'setDefaultValue',
'passedByReference' => 'setPassedByReference',
'variadic' => 'setVariadic',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
$parameterInfo = [
'name' => $parameter->getName(),
'passedByReference' => $parameter->isPassedByReference(),
'type' => $parameter->getType()
'type' => $parameter->getType(),
'variadic' => $parameter->isVariadic()
];

if ($parameter->isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase

const FLAG_REFERENCE = 'passedByReference';

const FLAG_VARIADIC = 'variadic';

/**#@-*/

/**
Expand All @@ -38,6 +40,7 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
self::FLAG_FINAL => 'isFinal',
self::FLAG_ABSTRACT => 'isAbstract',
self::FLAG_REFERENCE => 'getPassedByReference',
self::FLAG_VARIADIC => 'getVariadic',
];

/**
Expand Down Expand Up @@ -65,7 +68,13 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
'final' => true,
'static' => true,
'parameters' => [
['name' => 'data', 'type' => 'array', 'defaultValue' => [], 'passedByReference' => true],
[
'name' => 'data',
'type' => 'array',
'defaultValue' => [],
'passedByReference' => true,
'variadic' => false
],
],
'body' => 'return 1;',
'docblock' => ['shortDescription' => 'test short description'],
Expand Down Expand Up @@ -205,6 +214,9 @@ public function testAddMethods()
$actualDefaultValue = $actualParameter->getDefaultValue();
$this->assertEquals($parameterData['defaultValue'], $actualDefaultValue->getValue());
}

// assert variadic flag
$this->_assertFlag(self::FLAG_VARIADIC, $parameterData, $actualParameter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ protected function _getParameterList(array $parameters)
', ',
array_map(
function ($item) {
return "$" . $item['name'];
$output = '';
if ($item['variadic']) {
$output .= '... ';
}

$output .= "\${$item['name']}";
return $output;
},
$parameters
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,43 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
return $this->___callPlugins('getReference', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function firstVariadicParameter(... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'firstVariadicParameter');
if (!$pluginInfo) {
return parent::firstVariadicParameter(... $variadicValue);
} else {
return $this->___callPlugins('firstVariadicParameter', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function secondVariadicParameter($value, ... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'secondVariadicParameter');
if (!$pluginInfo) {
return parent::secondVariadicParameter($value, ... $variadicValue);
} else {
return $this->___callPlugins('secondVariadicParameter', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function byRefVariadic(&... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'byRefVariadic');
if (!$pluginInfo) {
return parent::byRefVariadic(... $variadicValue);
} else {
return $this->___callPlugins('byRefVariadic', func_get_args(), $pluginInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Sample
{
private $attribute;
private $variadicAttribute;

public function getValue()
{
Expand All @@ -22,4 +23,20 @@ public function setValue($value)
public function & getReference()
{
}

public function firstVariadicParameter(...$variadicValue)
{
$this->variadicAttribute = $variadicValue;
}

public function secondVariadicParameter($value, ...$variadicValue)
{
$this->attribute = $value;
$this->variadicAttribute = $variadicValue;
}

public function byRefVariadic(& ...$variadicValue)
{
$this->variadicAttribute = $variadicValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,43 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
return $this->___callPlugins('setValue', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function typeHintedFirstVariadicParameter(string ... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'typeHintedFirstVariadicParameter');
if (!$pluginInfo) {
return parent::typeHintedFirstVariadicParameter(... $variadicValue);
} else {
return $this->___callPlugins('typeHintedFirstVariadicParameter', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function typeHintedSecondVariadicParameter(string $value, string ... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'typeHintedSecondVariadicParameter');
if (!$pluginInfo) {
return parent::typeHintedSecondVariadicParameter($value, ... $variadicValue);
} else {
return $this->___callPlugins('typeHintedSecondVariadicParameter', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function byRefTypeHintedVariadic(string &... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'byRefTypeHintedVariadic');
if (!$pluginInfo) {
return parent::byRefTypeHintedVariadic(... $variadicValue);
} else {
return $this->___callPlugins('byRefTypeHintedVariadic', func_get_args(), $pluginInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class TSample
{
private $value;
private $variadicValue;

public function getValue() : string
{
Expand All @@ -18,4 +19,20 @@ public function setValue(string $value)
{
$this->value = $value;
}

public function typeHintedFirstVariadicParameter(string ...$variadicValue)
{
$this->variadicValue = $variadicValue;
}

public function typeHintedSecondVariadicParameter(string $value, string ...$variadicValue)
{
$this->value = $value;
$this->variadicValue = $variadicValue;
}

public function byRefTypeHintedVariadic(string & ...$variadicValue)
{
$this->variadicValue = $variadicValue;
}
}

0 comments on commit aa2915b

Please sign in to comment.