Skip to content

Commit f374b9e

Browse files
author
Sergey Shvets
committed
MAGETWO-95415: Can't generate Plugin if object method returns self
1 parent 8460e4e commit f374b9e

File tree

5 files changed

+122
-18
lines changed

5 files changed

+122
-18
lines changed

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

Lines changed: 27 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,22 +99,32 @@ 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()
111122
{
112123
}
113124

114125
/**
126+
* Test method
127+
*
115128
* @param mixed $arg1
116129
* @param string $arg2
117130
* @param int|null $arg3
@@ -130,6 +143,8 @@ public function public71(
130143
}
131144

132145
/**
146+
* Test method
147+
*
133148
* @param \DateTime|null $arg1
134149
* @param mixed $arg2
135150
*
@@ -140,4 +155,16 @@ public function public71(
140155
public function public71Another(?\DateTime $arg1, $arg2 = false): ?string
141156
{
142157
}
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+
}
143170
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
8080
}
8181
}
8282

83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function publicWithSelf($arg = false) : \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
87+
{
88+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'publicWithSelf');
89+
if (!$pluginInfo) {
90+
return parent::publicWithSelf($arg);
91+
} else {
92+
return $this->___callPlugins('publicWithSelf', func_get_args(), $pluginInfo);
93+
}
94+
}
95+
8396
/**
8497
* {@inheritdoc}
8598
*/

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ class Proxy extends \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespa
130130
return $this->_getSubject()->public71Another($arg1, $arg2);
131131
}
132132

133+
/**
134+
* {@inheritdoc}
135+
*/
136+
public function publicWithSelf($arg = false) : \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
137+
{
138+
return $this->_getSubject()->publicWithSelf($arg);
139+
}
140+
133141
/**
134142
* {@inheritdoc}
135143
*/

lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace Magento\Framework\Interception\Code\Generator;
88

9+
/**
10+
* Class Interceptor
11+
˚*
12+
* @package Magento\Framework\Interception\Code\Generator
13+
*/
914
class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract
1015
{
1116
/**
@@ -14,6 +19,8 @@ class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract
1419
const ENTITY_TYPE = 'interceptor';
1520

1621
/**
22+
* Returns default result class name
23+
*
1724
* @param string $modelClassName
1825
* @return string
1926
*/
@@ -102,10 +109,7 @@ protected function _getMethodInfo(\ReflectionMethod $method)
102109
$parameters[] = $this->_getMethodParameterInfo($parameter);
103110
}
104111

105-
$returnType = $method->getReturnType();
106-
$returnTypeValue = $returnType
107-
? ($returnType->allowsNull() ? '?' : '') .$returnType->getName()
108-
: null;
112+
$returnTypeValue = $this->getReturnTypeValue($method->getReturnType());
109113
$methodInfo = [
110114
'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(),
111115
'parameters' => $parameters,
@@ -137,6 +141,8 @@ protected function _getMethodInfo(\ReflectionMethod $method)
137141
}
138142

139143
/**
144+
* Return parameters list
145+
*
140146
* @param array $parameters
141147
* @return string
142148
*/
@@ -175,14 +181,16 @@ protected function _generateCode()
175181
} else {
176182
$this->_classGenerator->setExtendedClass($typeName);
177183
}
178-
$this->_classGenerator->addTrait('\\'. \Magento\Framework\Interception\Interceptor::class);
179-
$interfaces[] = '\\'. \Magento\Framework\Interception\InterceptorInterface::class;
184+
$this->_classGenerator->addTrait('\\' . \Magento\Framework\Interception\Interceptor::class);
185+
$interfaces[] = '\\' . \Magento\Framework\Interception\InterceptorInterface::class;
180186
$this->_classGenerator->setImplementedInterfaces($interfaces);
181187
return parent::_generateCode();
182188
}
183189

184190
/**
185-
* {@inheritdoc}
191+
* Validates data
192+
*
193+
* @return bool
186194
*/
187195
protected function _validateData()
188196
{
@@ -205,4 +213,22 @@ protected function _validateData()
205213
}
206214
return $result;
207215
}
216+
217+
/**
218+
* Returns return type
219+
*
220+
* @param mixed $returnType
221+
* @return null|string
222+
*/
223+
private function getReturnTypeValue($returnType): ?string
224+
{
225+
$returnTypeValue = null;
226+
if ($returnType) {
227+
$returnTypeValue = ($returnType->allowsNull() ? '?' : '');
228+
$returnTypeValue .= ($returnType->getName() === 'self')
229+
? $this->getSourceClassName()
230+
: $returnType->getName();
231+
}
232+
return $returnTypeValue;
233+
}
208234
}

lib/internal/Magento/Framework/ObjectManager/Code/Generator/Proxy.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\Framework\ObjectManager\Code\Generator;
910

11+
/**
12+
* Class Proxy
13+
*
14+
* @package Magento\Framework\ObjectManager\Code\Generator
15+
*/
1016
class Proxy extends \Magento\Framework\Code\Generator\EntityAbstract
1117
{
1218
/**
@@ -20,6 +26,8 @@ class Proxy extends \Magento\Framework\Code\Generator\EntityAbstract
2026
const NON_INTERCEPTABLE_INTERFACE = \Magento\Framework\ObjectManager\NoninterceptableInterface::class;
2127

2228
/**
29+
* Returns default result class name
30+
*
2331
* @param string $modelClassName
2432
* @return string
2533
*/
@@ -112,13 +120,16 @@ protected function _getClassMethods()
112120
$reflectionClass = new \ReflectionClass($this->getSourceClassName());
113121
$publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
114122
foreach ($publicMethods as $method) {
115-
if (!($method->isConstructor() ||
123+
if (!(
124+
$method->isConstructor() ||
116125
$method->isFinal() ||
117126
$method->isStatic() ||
118-
$method->isDestructor()) && !in_array(
119-
$method->getName(),
120-
['__sleep', '__wakeup', '__clone']
121-
)
127+
$method->isDestructor()
128+
)
129+
&& !in_array(
130+
$method->getName(),
131+
['__sleep', '__wakeup', '__clone']
132+
)
122133
) {
123134
$methods[] = $this->_getMethodInfo($method);
124135
}
@@ -128,6 +139,8 @@ protected function _getClassMethods()
128139
}
129140

130141
/**
142+
* Generates code
143+
*
131144
* @return string
132145
*/
133146
protected function _generateCode()
@@ -160,10 +173,7 @@ protected function _getMethodInfo(\ReflectionMethod $method)
160173
$parameters[] = $this->_getMethodParameterInfo($parameter);
161174
}
162175

163-
$returnType = $method->getReturnType();
164-
$returnTypeValue = $returnType
165-
? ($returnType->allowsNull() ? '?' : '') .$returnType->getName()
166-
: null;
176+
$returnTypeValue = $this->getReturnTypeValue($method->getReturnType());
167177
$methodInfo = [
168178
'name' => $method->getName(),
169179
'parameters' => $parameters,
@@ -237,11 +247,13 @@ protected function _getMethodBody(
237247
}
238248

239249
return ($withoutReturn ? '' : 'return ')
240-
.'$this->_getSubject()->' . $methodCall . ';';
250+
. '$this->_getSubject()->' . $methodCall . ';';
241251
}
242252

243253
/**
244-
* {@inheritdoc}
254+
* Validates data
255+
*
256+
* @return bool
245257
*/
246258
protected function _validateData()
247259
{
@@ -259,4 +271,22 @@ protected function _validateData()
259271
}
260272
return $result;
261273
}
274+
275+
/**
276+
* Returns return type
277+
*
278+
* @param mixed $returnType
279+
* @return null|string
280+
*/
281+
private function getReturnTypeValue($returnType): ?string
282+
{
283+
$returnTypeValue = null;
284+
if ($returnType) {
285+
$returnTypeValue = ($returnType->allowsNull() ? '?' : '');
286+
$returnTypeValue .= ($returnType->getName() === 'self')
287+
? $this->getSourceClassName()
288+
: $returnType->getName();
289+
}
290+
return $returnTypeValue;
291+
}
262292
}

0 commit comments

Comments
 (0)