Skip to content

Commit dd69729

Browse files
author
Alexander Paliarush
committed
MAGETWO-31931: Implement ExtensionInterface Code Generator
- Fixed builds
1 parent 71e0aa4 commit dd69729

File tree

5 files changed

+73
-41
lines changed

5 files changed

+73
-41
lines changed

dev/tests/unit/testsuite/Magento/Framework/Api/Code/Generator/ExtensionGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testValidateException()
8585
$this->assertTrue(
8686
in_array(
8787
'Invalid extension name [\Magento\Catalog\Api\Data\ProductInterface].'
88-
. ' Use \Magento\Catalog\Api\Data\ProductExtension',
88+
. ' Use \Magento\Catalog\Api\Data\ProductExtension',
8989
$model->getErrors()
9090
),
9191
'Expected validation error message is missing.'
@@ -96,6 +96,7 @@ public function testValidateException()
9696
* Check if generated code matches provided expected result.
9797
*
9898
* @param string $expectedResult
99+
* @return void
99100
*/
100101
protected function validateGeneratedCode($expectedResult)
101102
{

dev/tests/unit/testsuite/Magento/Framework/Api/Code/Generator/ExtensionInterfaceGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testValidateException()
6666
$this->assertTrue(
6767
in_array(
6868
'Invalid extension interface name [\Magento\Catalog\Api\Data\ProductInterface].'
69-
. ' Use \Magento\Catalog\Api\Data\ProductExtensionInterface',
69+
. ' Use \Magento\Catalog\Api\Data\ProductExtensionInterface',
7070
$model->getErrors()
7171
),
7272
'Expected validation error message is missing.'

dev/tests/unit/testsuite/Magento/Framework/Code/_files/app/code/Magento/SomeModule/Model/SevenInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
// @codingStandardsIgnoreFile
8+
69
namespace Magento\SomeModule\Model;
710

811
use Magento\SomeModule\Model\Two\Test as TestTwo;

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

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,74 @@ public function generate()
2121
return $output;
2222
}
2323
}
24-
2524
$output = '';
2625
if (!$this->getName()) {
2726
return $output;
2827
}
2928

30-
if (null !== ($namespace = $this->getNamespaceName())) {
31-
$output .= 'namespace ' . $namespace . ';' . self::LINE_FEED . self::LINE_FEED;
32-
}
33-
34-
$uses = $this->getUses();
35-
if (!empty($uses)) {
36-
foreach ($uses as $use) {
37-
$output .= 'use ' . $use . ';' . self::LINE_FEED;
38-
}
39-
$output .= self::LINE_FEED;
40-
}
41-
29+
$output .= $this->generateDirectives();
4230
if (null !== ($docBlock = $this->getDocBlock())) {
4331
$docBlock->setIndentation('');
4432
$output .= $docBlock->generate();
4533
}
46-
4734
$output .= 'interface ' . $this->getName();
48-
4935
if (!empty($this->extendedClass)) {
5036
$output .= ' extends ' . $this->extendedClass;
5137
}
5238

53-
$output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
39+
$output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED
40+
. $this->generateMethods() . self::LINE_FEED . '}' . self::LINE_FEED;
41+
42+
return $output;
43+
}
44+
45+
/**
46+
* Instantiate interface method generator object.
47+
*
48+
* @return \Magento\Framework\Code\Generator\InterfaceMethodGenerator
49+
*/
50+
protected function createMethodGenerator()
51+
{
52+
return new \Magento\Framework\Code\Generator\InterfaceMethodGenerator();
53+
}
5454

55+
/**
56+
* Generate methods.
57+
*
58+
* @return string
59+
*/
60+
protected function generateMethods()
61+
{
62+
$output = '';
5563
$methods = $this->getMethods();
5664
if (!empty($methods)) {
5765
foreach ($methods as $method) {
5866
$output .= $method->generate() . self::LINE_FEED;
5967
}
6068
}
61-
62-
$output .= self::LINE_FEED . '}' . self::LINE_FEED;
63-
6469
return $output;
6570
}
6671

6772
/**
68-
* Instantiate interface method generator object.
73+
* Generate directives.
6974
*
70-
* @return \Magento\Framework\Code\Generator\InterfaceMethodGenerator
75+
* @return string
7176
*/
72-
protected function createMethodGenerator()
77+
protected function generateDirectives()
7378
{
74-
return new \Magento\Framework\Code\Generator\InterfaceMethodGenerator();
79+
$output = '';
80+
$namespace = $this->getNamespaceName();
81+
if (null !== $namespace) {
82+
$output .= 'namespace ' . $namespace . ';' . self::LINE_FEED . self::LINE_FEED;
83+
}
84+
85+
$uses = $this->getUses();
86+
if (!empty($uses)) {
87+
foreach ($uses as $use) {
88+
$output .= 'use ' . $use . ';' . self::LINE_FEED;
89+
}
90+
$output .= self::LINE_FEED;
91+
}
92+
return $output;
7593
}
7694
}

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,7 @@ class InterfaceMethodGenerator extends \Zend\Code\Generator\MethodGenerator
1515
*/
1616
public function generate()
1717
{
18-
if ($this->getVisibility() != self::VISIBILITY_PUBLIC) {
19-
throw new \LogicException(
20-
"Interface method visibility can only be 'public'. Method name: '{$this->getName()}'"
21-
);
22-
}
23-
if ($this->isFinal()) {
24-
throw new \LogicException(
25-
"Interface method cannot be marked as 'final'. Method name: '{$this->getName()}'"
26-
);
27-
}
28-
if ($this->isAbstract()) {
29-
throw new \LogicException(
30-
"'abstract' modifier cannot be used for interface method. Method name: '{$this->getName()}'"
31-
);
32-
}
33-
18+
$this->validateMethodModifiers();
3419
$output = '';
3520
if (!$this->getName()) {
3621
return $output;
@@ -61,4 +46,29 @@ public function generate()
6146

6247
return $output;
6348
}
49+
50+
/**
51+
* Ensure that used method modifiers are allowed for interface methods.
52+
*
53+
* @throws \LogicException
54+
* @return void
55+
*/
56+
protected function validateMethodModifiers()
57+
{
58+
if ($this->getVisibility() != self::VISIBILITY_PUBLIC) {
59+
throw new \LogicException(
60+
"Interface method visibility can only be 'public'. Method name: '{$this->getName()}'"
61+
);
62+
}
63+
if ($this->isFinal()) {
64+
throw new \LogicException(
65+
"Interface method cannot be marked as 'final'. Method name: '{$this->getName()}'"
66+
);
67+
}
68+
if ($this->isAbstract()) {
69+
throw new \LogicException(
70+
"'abstract' modifier cannot be used for interface method. Method name: '{$this->getName()}'"
71+
);
72+
}
73+
}
6474
}

0 commit comments

Comments
 (0)