Skip to content

Commit 9289039

Browse files
ENGCOM-3568: [Backport-2.2] Code generation improvement for php 7.1 #19398
- Merge Pull Request #19398 from swnsma/magento2-1:Code-Generation-Improvement-For-PHP-7.1 - Merged commits: 1. 8b900d4 2. f674bad 3. 541ce18 4. d33db64 5. c0c0a37 6. 3e7f0eb 7. d7efcca 8. d249036 9. 87ab26f 10. 41c8da0 11. f0758f3 12. a33f89d
2 parents ace297a + a33f89d commit 9289039

File tree

11 files changed

+760
-52
lines changed

11 files changed

+760
-52
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Code\Generator71Test;
7+
8+
use Zend\Code\Generator\DocBlockGenerator;
9+
10+
class ParentClassWithNamespace
11+
{
12+
/**
13+
* Public parent method
14+
*
15+
* @param \Zend\Code\Generator\DocBlockGenerator $docBlockGenerator
16+
* @param string $param1
17+
* @param string $param2
18+
* @param string $param3
19+
* @param array $array
20+
*
21+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
22+
*/
23+
public function publicParentMethod(
24+
DocBlockGenerator $docBlockGenerator,
25+
$param1 = '',
26+
$param2 = '\\',
27+
$param3 = '\'',
28+
array $array = []
29+
) {
30+
}
31+
32+
/**
33+
* Protected parent method
34+
*
35+
* @param \Zend\Code\Generator\DocBlockGenerator $docBlockGenerator
36+
* @param string $param1
37+
* @param string $param2
38+
* @param string $param3
39+
* @param array $array
40+
*
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
42+
*/
43+
protected function _protectedParentMethod(
44+
DocBlockGenerator $docBlockGenerator,
45+
$param1 = '',
46+
$param2 = '\\',
47+
$param3 = '\'',
48+
array $array = []
49+
) {
50+
}
51+
52+
/**
53+
* Private parent method
54+
*
55+
* @param \Zend\Code\Generator\DocBlockGenerator $docBlockGenerator
56+
* @param string $param1
57+
* @param string $param2
58+
* @param string $param3
59+
* @param array $array
60+
*
61+
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
62+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
63+
*/
64+
private function _privateParentMethod(
65+
DocBlockGenerator $docBlockGenerator,
66+
$param1 = '',
67+
$param2 = '\\',
68+
$param3 = '\'',
69+
array $array = []
70+
) {
71+
}
72+
73+
public function publicParentWithoutParameters()
74+
{
75+
}
76+
77+
public static function publicParentStatic()
78+
{
79+
}
80+
81+
/**
82+
* @SuppressWarnings(PHPMD.FinalImplementation) Suppressed as is a fixture but not a real code
83+
*/
84+
final public function publicParentFinal()
85+
{
86+
}
87+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Code\Generator71Test;
7+
8+
use Zend\Code\Generator\ClassGenerator;
9+
10+
/**
11+
* Class SourceClassWithNamespace
12+
*/
13+
class SourceClassWithNamespace extends ParentClassWithNamespace
14+
{
15+
/**
16+
* Public child constructor
17+
*
18+
* @param string $param1
19+
* @param string $param2
20+
* @param string $param3
21+
*
22+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
23+
*/
24+
public function __construct($param1 = '', $param2 = '\\', $param3 = '\'')
25+
{
26+
}
27+
28+
/**
29+
* Public child method
30+
*
31+
* @param \Zend\Code\Generator\ClassGenerator $classGenerator
32+
* @param string $param1
33+
* @param string $param2
34+
* @param string $param3
35+
* @param array $array
36+
* @return void
37+
*
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
39+
*/
40+
public function publicChildMethod(
41+
ClassGenerator $classGenerator,
42+
$param1 = '',
43+
$param2 = '\\',
44+
$param3 = '\'',
45+
array $array = []
46+
) {
47+
}
48+
49+
/**
50+
* Public child method with reference
51+
*
52+
* @param \Zend\Code\Generator\ClassGenerator $classGenerator
53+
* @param string $param1
54+
* @param array $array
55+
*
56+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
57+
*/
58+
public function publicMethodWithReference(ClassGenerator &$classGenerator, &$param1, array &$array)
59+
{
60+
}
61+
62+
/**
63+
* Protected child method
64+
*
65+
* @param \Zend\Code\Generator\ClassGenerator $classGenerator
66+
* @param string $param1
67+
* @param string $param2
68+
* @param string $param3
69+
*
70+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
71+
*/
72+
protected function _protectedChildMethod(
73+
ClassGenerator $classGenerator,
74+
$param1 = '',
75+
$param2 = '\\',
76+
$param3 = '\''
77+
) {
78+
}
79+
80+
/**
81+
* Private child method
82+
*
83+
* @param \Zend\Code\Generator\ClassGenerator $classGenerator
84+
* @param string $param1
85+
* @param string $param2
86+
* @param string $param3
87+
* @param array $array
88+
* @return void
89+
*
90+
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
91+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
92+
*/
93+
private function _privateChildMethod(
94+
ClassGenerator $classGenerator,
95+
$param1 = '',
96+
$param2 = '\\',
97+
$param3 = '\'',
98+
array $array = []
99+
) {
100+
}
101+
102+
/**
103+
* Test method
104+
*/
105+
public function publicChildWithoutParameters()
106+
{
107+
}
108+
109+
/**
110+
* Test method
111+
*/
112+
public static function publicChildStatic()
113+
{
114+
}
115+
116+
/**
117+
* Test method
118+
*
119+
* @SuppressWarnings(PHPMD.FinalImplementation) Suppressed as is a fixture but not a real code
120+
*/
121+
final public function publicChildFinal()
122+
{
123+
}
124+
125+
/**
126+
* Test method
127+
*
128+
* @param mixed $arg1
129+
* @param string $arg2
130+
* @param int|null $arg3
131+
* @param int|null $arg4
132+
*
133+
* @return void
134+
*
135+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
136+
*/
137+
public function public71(
138+
$arg1,
139+
string $arg2,
140+
?int $arg3,
141+
?int $arg4 = null
142+
): void {
143+
}
144+
145+
/**
146+
* Test method
147+
*
148+
* @param \DateTime|null $arg1
149+
* @param mixed $arg2
150+
*
151+
* @return null|string
152+
*
153+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
154+
*/
155+
public function public71Another(?\DateTime $arg1, $arg2 = false): ?string
156+
{
157+
}
158+
159+
/**
160+
* Test method
161+
*
162+
* @param bool $arg
163+
* @return SourceClassWithNamespace
164+
*
165+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
166+
*/
167+
public function publicWithSelf($arg = false): self
168+
{
169+
}
170+
}

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
*/
2424
class GeneratorTest extends TestCase
2525
{
26-
const CLASS_NAME_WITH_NAMESPACE = GeneratorTest\SourceClassWithNamespace::class;
26+
const CLASS_NAME_WITH_NAMESPACE = \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace::class;
27+
const CLASS_NAME_WITH_NAMESPACE_71 = \Magento\Framework\Code\Generator71Test\SourceClassWithNamespace::class;
2728

2829
/**
2930
* @var Generator
@@ -172,6 +173,62 @@ public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespac
172173
$this->assertEquals($expectedContent, $content);
173174
}
174175

176+
/**
177+
* @requires PHP 7.1
178+
*/
179+
public function testGenerateClassProxyWithNamespace71()
180+
{
181+
$proxyClassName = self::CLASS_NAME_WITH_NAMESPACE_71 . '\Proxy';
182+
$result = false;
183+
$generatorResult = $this->_generator->generateClass($proxyClassName);
184+
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
185+
$result = true;
186+
}
187+
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
188+
189+
$proxy = Bootstrap::getObjectManager()->create($proxyClassName);
190+
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE_71, $proxy);
191+
192+
// This test is only valid if the factory created the object if Autoloader did not pick it up automatically
193+
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
194+
$content = $this->_clearDocBlock(
195+
file_get_contents(
196+
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE_71 . '\Proxy')
197+
)
198+
);
199+
$expectedContent = $this->_clearDocBlock(
200+
file_get_contents(__DIR__ . '/_expected71/SourceClassWithNamespaceProxy.php.sample')
201+
);
202+
$this->assertEquals($expectedContent, $content);
203+
}
204+
}
205+
206+
/**
207+
* @requires PHP 7.1
208+
*/
209+
public function testGenerateClassInterceptorWithNamespace71()
210+
{
211+
$interceptorClassName = self::CLASS_NAME_WITH_NAMESPACE_71 . '\Interceptor';
212+
$result = false;
213+
$generatorResult = $this->_generator->generateClass($interceptorClassName);
214+
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
215+
$result = true;
216+
}
217+
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
218+
219+
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
220+
$content = $this->_clearDocBlock(
221+
file_get_contents(
222+
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE_71 . '\Interceptor')
223+
)
224+
);
225+
$expectedContent = $this->_clearDocBlock(
226+
file_get_contents(__DIR__ . '/_expected71/SourceClassWithNamespaceInterceptor.php.sample')
227+
);
228+
$this->assertEquals($expectedContent, $content);
229+
}
230+
}
231+
175232
/**
176233
* It tries to generate a new class file when the generated directory is read-only
177234
*/

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
use Zend\Code\Generator\ClassGenerator;
99

10+
/**
11+
* Class SourceClassWithNamespace
12+
*/
1013
class SourceClassWithNamespace extends ParentClassWithNamespace
1114
{
1215
/**
@@ -96,15 +99,23 @@ private function _privateChildMethod(
9699
) {
97100
}
98101

102+
/**
103+
* Test method
104+
*/
99105
public function publicChildWithoutParameters()
100106
{
101107
}
102108

109+
/**
110+
* Test method
111+
*/
103112
public static function publicChildStatic()
104113
{
105114
}
106115

107116
/**
117+
* Test method
118+
*
108119
* @SuppressWarnings(PHPMD.FinalImplementation) Suppressed as is a fixture but not a real code
109120
*/
110121
final public function publicChildFinal()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace;
33

44
/**
5+
* Interceptor class for @see \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
6+
*
57
* Copyright © Magento, Inc. All rights reserved.
68
* See COPYING.txt for license details.
79
*/

0 commit comments

Comments
 (0)