Skip to content

Commit 87ab26f

Browse files
author
al.kravchuk
committed
magento/magento2##15505: Interceptor class methods do not support nullable return types.
Backport variadic, self return types compatibility for proxy/interceptor code generations. Backport nullable type hints for PHP7.1
1 parent d249036 commit 87ab26f

File tree

13 files changed

+625
-93
lines changed

13 files changed

+625
-93
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class GeneratorTest extends \PHPUnit\Framework\TestCase
2323
{
2424
const CLASS_NAME_WITH_NAMESPACE = \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace::class;
25+
const CLASS_NAME_WITH_NAMESPACE_71 = \Magento\Framework\Code\Generator71Test\SourceClassWithNamespace::class;
2526

2627
/**
2728
* @var \Magento\Framework\Code\Generator
@@ -185,4 +186,72 @@ public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespac
185186
);
186187
$this->assertEquals($expectedContent, $content);
187188
}
189+
190+
public function testGenerateClassProxyWithNamespace71()
191+
{
192+
if (!$this->shouldRun71()) {
193+
$this->assertTrue(true);
194+
return;
195+
}
196+
197+
$proxyClassName = self::CLASS_NAME_WITH_NAMESPACE_71 . '\Proxy';
198+
$result = false;
199+
$generatorResult = $this->_generator->generateClass($proxyClassName);
200+
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
201+
$result = true;
202+
}
203+
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
204+
205+
$proxy = Bootstrap::getObjectManager()->create($proxyClassName);
206+
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE_71, $proxy);
207+
208+
// This test is only valid if the factory created the object if Autoloader did not pick it up automatically
209+
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
210+
$content = $this->_clearDocBlock(
211+
file_get_contents(
212+
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE_71 . '\Proxy')
213+
)
214+
);
215+
$expectedContent = $this->_clearDocBlock(
216+
file_get_contents(__DIR__ . '/_expected71/SourceClassWithNamespaceProxy.php.sample')
217+
);
218+
$this->assertEquals($expectedContent, $content);
219+
}
220+
}
221+
222+
public function testGenerateClassInterceptorWithNamespace71()
223+
{
224+
if (!$this->shouldRun71()) {
225+
$this->assertTrue(true);
226+
return;
227+
}
228+
229+
$interceptorClassName = self::CLASS_NAME_WITH_NAMESPACE_71 . '\Interceptor';
230+
$result = false;
231+
$generatorResult = $this->_generator->generateClass($interceptorClassName);
232+
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
233+
$result = true;
234+
}
235+
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
236+
237+
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
238+
$content = $this->_clearDocBlock(
239+
file_get_contents(
240+
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE_71 . '\Interceptor')
241+
)
242+
);
243+
$expectedContent = $this->_clearDocBlock(
244+
file_get_contents(__DIR__ . '/_expected71/SourceClassWithNamespaceInterceptor.php.sample')
245+
);
246+
$this->assertEquals($expectedContent, $content);
247+
}
248+
}
249+
250+
/**
251+
* @return bool
252+
*/
253+
private function shouldRun71():bool
254+
{
255+
return version_compare(phpversion(), '7.1.0', '>=');
256+
}
188257
}

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,40 +122,6 @@ final public function publicChildFinal()
122122
{
123123
}
124124

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-
159125
/**
160126
* Test method
161127
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SourceClassWithNamespaceExtensionInterfaceFactory
4141
* @param array $data
4242
* @return \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespaceExtension
4343
*/
44-
public function create(array $data = [])
44+
public function create(array $data = array())
4545
{
4646
return $this->_objectManager->create($this->_instanceName, $data);
4747
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SourceClassWithNamespaceFactory
4141
* @param array $data
4242
* @return \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
4343
*/
44-
public function create(array $data = [])
44+
public function create(array $data = array())
4545
{
4646
return $this->_objectManager->create($this->_instanceName, $data);
4747
}

0 commit comments

Comments
 (0)