Skip to content

Commit dfa9c35

Browse files
author
Oleksii Korshenko
committed
Merge branch 'MAGETWO-49585-type-declaration' into pull-request
2 parents c5374ca + 0e6de79 commit dfa9c35

File tree

11 files changed

+135
-18
lines changed

11 files changed

+135
-18
lines changed

app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public function aroundGetExtensionAttributes(
5959
$paymentToken = $paymentExtension->getVaultPaymentToken();
6060
if ($paymentToken === null) {
6161
$paymentToken = $this->paymentTokenManagement->getByPaymentId($payment->getEntityId());
62-
$paymentExtension->setVaultPaymentToken($paymentToken);
62+
if ($paymentToken instanceof \Magento\Vault\Api\Data\PaymentTokenInterface) {
63+
$paymentExtension->setVaultPaymentToken($paymentToken);
64+
}
6365
$payment->setExtensionAttributes($paymentExtension);
6466
}
6567

lib/internal/Magento/Framework/Api/Code/Generator/ExtensionAttributesGenerator.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class ExtensionAttributesGenerator extends \Magento\Framework\Code\Generator\Ent
2424
*/
2525
protected $config;
2626

27+
/**
28+
* @var \Magento\Framework\Reflection\TypeProcessor
29+
*/
30+
private $typeProcessor;
31+
2732
/**
2833
* @var array
2934
*/
@@ -58,6 +63,22 @@ public function __construct(
5863
);
5964
}
6065

66+
/**
67+
* Get type processor
68+
*
69+
* @return \Magento\Framework\Reflection\TypeProcessor
70+
* @deprecated
71+
*/
72+
private function getTypeProcessor()
73+
{
74+
if ($this->typeProcessor === null) {
75+
$this->typeProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
76+
\Magento\Framework\Reflection\TypeProcessor::class
77+
);
78+
}
79+
return $this->typeProcessor;
80+
}
81+
6182
/**
6283
* {@inheritdoc}
6384
*/
@@ -90,9 +111,15 @@ protected function _getClassMethods()
90111
'body' => "return \$this->_get('{$attributeName}');",
91112
'docblock' => ['tags' => [['name' => 'return', 'description' => $attributeType . '|null']]],
92113
];
114+
$parameters = ['name' => $propertyName];
115+
// If the attribute type is a valid type declaration (e.g., interface, class, array) then use it to enforce
116+
// constraints on the generated setter methods
117+
if ($this->getTypeProcessor()->isValidTypeDeclaration($attributeType)) {
118+
$parameters['type'] = $attributeType;
119+
}
93120
$methods[] = [
94121
'name' => $setterName,
95-
'parameters' => [['name' => $propertyName]],
122+
'parameters' => [$parameters],
96123
'body' => "\$this->setData('{$attributeName}', \${$propertyName});" . PHP_EOL . "return \$this;",
97124
'docblock' => [
98125
'tags' => [

lib/internal/Magento/Framework/Api/Test/Unit/Code/Generator/ExtensionAttributesGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class ExtensionAttributesGeneratorTest extends \PHPUnit_Framework_TestCase
1515
*/
1616
protected $configMock;
1717

18+
/**
19+
* @var \Magento\Framework\Reflection\TypeProcessor|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $typeProcessorMock;
22+
1823
/**
1924
* @var \Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator|\PHPUnit_Framework_MockObject_MockObject
2025
*/
@@ -26,11 +31,17 @@ protected function setUp()
2631
->disableOriginalConstructor()
2732
->getMock();
2833

34+
$this->typeProcessorMock = $this->getMockBuilder('Magento\Framework\Reflection\TypeProcessor')
35+
->disableOriginalConstructor()
36+
->setMethods(null)
37+
->getMock();
38+
2939
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3040
$this->model = $objectManager->getObject(
3141
'Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator',
3242
[
3343
'config' => $this->configMock,
44+
'typeProcessor' => $this->typeProcessorMock,
3445
'sourceClassName' => '\Magento\Catalog\Api\Data\Product',
3546
'resultClassName' => '\Magento\Catalog\Api\Data\ProductExtension',
3647
'classGenerator' => null
@@ -55,6 +66,11 @@ public function testGenerate()
5566
Converter::DATA_TYPE => '\Magento\Bundle\Api\Data\OptionInterface[]',
5667
Converter::RESOURCE_PERMISSIONS => [],
5768
],
69+
// Ensure type declaration is added to argument of setter
70+
'complex_object_attribute_with_type_declaration' => [
71+
Converter::DATA_TYPE => '\Magento\Bundle\Api\Data\BundleOptionInterface',
72+
Converter::RESOURCE_PERMISSIONS => [],
73+
],
5874
],
5975
'Magento\Catalog\Api\Data\Product' => [
6076
'should_not_include' => [

lib/internal/Magento/Framework/Api/Test/Unit/Code/Generator/ExtensionAttributesInterfaceGeneratorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function testGenerate()
2929
Converter::DATA_TYPE => '\Magento\Bundle\Api\Data\OptionInterface[]',
3030
Converter::RESOURCE_PERMISSIONS => [],
3131
],
32+
// Ensure type declaration is added to argument of setter
33+
'complex_object_attribute_with_type_declaration' => [
34+
Converter::DATA_TYPE => '\Magento\Bundle\Api\Data\BundleOptionInterface',
35+
Converter::RESOURCE_PERMISSIONS => [],
36+
],
3237
],
3338
'Magento\Catalog\Api\Data\Product' => [
3439
'should_not_include' => [
@@ -38,12 +43,17 @@ public function testGenerate()
3843
],
3944
]
4045
);
46+
$typeProcessorMock = $this->getMockBuilder('Magento\Framework\Reflection\TypeProcessor')
47+
->disableOriginalConstructor()
48+
->setMethods(null)
49+
->getMock();
4150

4251
/** @var \Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceGenerator $model */
4352
$model = $objectManager->getObject(
4453
'Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceGenerator',
4554
[
4655
'config' => $configMock,
56+
'typeProcessor' => $typeProcessorMock,
4757
'sourceClassName' => '\Magento\Catalog\Api\Data\Product',
4858
'resultClassName' => '\Magento\Catalog\Api\Data\ProductExtensionInterface',
4959
'classGenerator' => null

lib/internal/Magento/Framework/Api/Test/Unit/Code/Generator/_files/SampleExtension.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,22 @@ class ProductExtension extends \Magento\Framework\Api\AbstractSimpleObject imple
4040
$this->setData('complex_object_attribute', $complexObjectAttribute);
4141
return $this;
4242
}
43+
44+
/**
45+
* @return \Magento\Bundle\Api\Data\BundleOptionInterface|null
46+
*/
47+
public function getComplexObjectAttributeWithTypeDeclaration()
48+
{
49+
return $this->_get('complex_object_attribute_with_type_declaration');
50+
}
51+
52+
/**
53+
* @param \Magento\Bundle\Api\Data\BundleOptionInterface $complexObjectAttributeWithTypeDeclaration
54+
* @return $this
55+
*/
56+
public function setComplexObjectAttributeWithTypeDeclaration(\Magento\Bundle\Api\Data\BundleOptionInterface $complexObjectAttributeWithTypeDeclaration)
57+
{
58+
$this->setData('complex_object_attribute_with_type_declaration', $complexObjectAttributeWithTypeDeclaration);
59+
return $this;
60+
}
4361
}

lib/internal/Magento/Framework/Api/Test/Unit/Code/Generator/_files/SampleExtensionInterface.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ interface ProductExtensionInterface extends \Magento\Framework\Api\ExtensionAttr
2626
* @return $this
2727
*/
2828
public function setComplexObjectAttribute($complexObjectAttribute);
29+
30+
/**
31+
* @return \Magento\Bundle\Api\Data\BundleOptionInterface|null
32+
*/
33+
public function getComplexObjectAttributeWithTypeDeclaration();
34+
35+
/**
36+
* @param \Magento\Bundle\Api\Data\BundleOptionInterface $complexObjectAttributeWithTypeDeclaration
37+
* @return $this
38+
*/
39+
public function setComplexObjectAttributeWithTypeDeclaration(\Magento\Bundle\Api\Data\BundleOptionInterface $complexObjectAttributeWithTypeDeclaration);
2940
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ protected function _setDataToObject($object, array $data, array $map)
8585
public function setClassDocBlock(array $docBlock)
8686
{
8787
$docBlockObject = new \Zend\Code\Generator\DocBlockGenerator();
88+
$docBlockObject->setWordWrap(false);
8889
$this->_setDataToObject($docBlockObject, $docBlock, $this->_docBlockOptions);
8990

9091
return parent::setDocBlock($docBlockObject);
@@ -122,6 +123,7 @@ public function addMethods(array $methods)
122123

123124
if (isset($methodOptions['docblock']) && is_array($methodOptions['docblock'])) {
124125
$docBlockObject = new \Zend\Code\Generator\DocBlockGenerator();
126+
$docBlockObject->setWordWrap(false);
125127
$this->_setDataToObject($docBlockObject, $methodOptions['docblock'], $this->_docBlockOptions);
126128

127129
$methodObject->setDocBlock($docBlockObject);
@@ -165,6 +167,7 @@ public function addProperties(array $properties)
165167
$docBlock = $propertyOptions['docblock'];
166168
if (is_array($docBlock)) {
167169
$docBlockObject = new \Zend\Code\Generator\DocBlockGenerator();
170+
$docBlockObject->setWordWrap(false);
168171
$this->_setDataToObject($docBlockObject, $docBlock, $this->_docBlockOptions);
169172
$propertyObject->setDocBlock($docBlockObject);
170173
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Code/Generator/_files/SampleRepository.txt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
namespace Magento\Framework\ObjectManager\Code\Generator;
22

33
/**
4-
* Repository class for @see
5-
* \Magento\Framework\ObjectManager\Code\Generator\SampleInterface
4+
* Repository class for @see \Magento\Framework\ObjectManager\Code\Generator\SampleInterface
65
*/
76
class SampleRepository implements \Magento\Framework\ObjectManager\Code\Generator\SampleRepositoryInterface
87
{
@@ -16,8 +15,7 @@ class SampleRepository implements \Magento\Framework\ObjectManager\Code\Generato
1615
/**
1716
* Collection Factory
1817
*
19-
* @var
20-
* \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory
18+
* @var \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory
2119
*/
2220
protected $sampleInterfaceSearchResultFactory = null;
2321

@@ -40,13 +38,9 @@ class SampleRepository implements \Magento\Framework\ObjectManager\Code\Generato
4038
/**
4139
* Repository constructor
4240
*
43-
* @param \Magento\Framework\ObjectManager\Code\Generator\SampleInterface
44-
* $sampleInterfacePersistor
45-
* @param
46-
* \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory
47-
* $sampleInterfaceSearchResultFactory
48-
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
49-
* $extensionAttributesJoinProcessor
41+
* @param \Magento\Framework\ObjectManager\Code\Generator\SampleInterface $sampleInterfacePersistor
42+
* @param \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory $sampleInterfaceSearchResultFactory
43+
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
5044
*/
5145
public function __construct(\Magento\Framework\ObjectManager\Code\Generator\SampleInterfacePersistor $sampleInterfacePersistor, \Magento\Framework\ObjectManager\Code\Generator\SampleSearchResultInterfaceFactory $sampleInterfaceSearchResultFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor)
5246
{

lib/internal/Magento/Framework/Reflection/Test/Unit/TypeProcessorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ public function testIsArrayType()
116116
$this->assertTrue($this->_typeProcessor->isArrayType('string[]'));
117117
}
118118

119+
public function testIsValidTypeDeclaration()
120+
{
121+
$this->assertTrue($this->_typeProcessor->isValidTypeDeclaration('Traversable')); // Interface
122+
$this->assertTrue($this->_typeProcessor->isValidTypeDeclaration('stdObj')); // Class
123+
$this->assertTrue($this->_typeProcessor->isValidTypeDeclaration('array'));
124+
$this->assertTrue($this->_typeProcessor->isValidTypeDeclaration('callable'));
125+
$this->assertTrue($this->_typeProcessor->isValidTypeDeclaration('self'));
126+
$this->assertTrue($this->_typeProcessor->isValidTypeDeclaration('self'));
127+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('string'));
128+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('string[]'));
129+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('int'));
130+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('float'));
131+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('double'));
132+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('boolean'));
133+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('[]'));
134+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('mixed[]'));
135+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('stdObj[]'));
136+
$this->assertFalse($this->_typeProcessor->isValidTypeDeclaration('Traversable[]'));
137+
}
138+
119139
public function getArrayItemType()
120140
{
121141
$this->assertEquals('string', $this->_typeProcessor->getArrayItemType('str[]'));

lib/internal/Magento/Framework/Reflection/TypeProcessor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,18 @@ public function isArrayType($type)
420420
return (bool)preg_match('/(\[\]$|^ArrayOf)/', $type);
421421
}
422422

423+
/**
424+
* Check if given type is valid to use as an argument type declaration
425+
*
426+
* @see http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
427+
* @param string $type
428+
* @return bool
429+
*/
430+
public function isValidTypeDeclaration($type)
431+
{
432+
return !($this->isTypeSimple($type) || $this->isTypeAny($type) || $this->isArrayType($type));
433+
}
434+
423435
/**
424436
* Get item type of the array.
425437
*

0 commit comments

Comments
 (0)