Skip to content

Commit a375e13

Browse files
committed
Improve the generated Interceptor classes
1 parent 62ffa42 commit a375e13

File tree

5 files changed

+37
-137
lines changed

5 files changed

+37
-137
lines changed

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

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
2121
public function publicChildMethod(\Laminas\Code\Generator\ClassGenerator $classGenerator, $param1 = '', $param2 = '\\', $param3 = '\'', array $array = [])
2222
{
2323
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'publicChildMethod');
24-
if (!$pluginInfo) {
25-
return parent::publicChildMethod($classGenerator, $param1, $param2, $param3, $array);
26-
} else {
27-
return $this->___callPlugins('publicChildMethod', func_get_args(), $pluginInfo);
28-
}
24+
return $pluginInfo ? $this->___callPlugins('publicChildMethod', func_get_args(), $pluginInfo) : parent::publicChildMethod($classGenerator, $param1, $param2, $param3, $array);
2925
}
3026

3127
/**
@@ -34,11 +30,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
3430
public function publicMethodWithReference(\Laminas\Code\Generator\ClassGenerator &$classGenerator, &$param1, array &$array)
3531
{
3632
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'publicMethodWithReference');
37-
if (!$pluginInfo) {
38-
return parent::publicMethodWithReference($classGenerator, $param1, $array);
39-
} else {
40-
return $this->___callPlugins('publicMethodWithReference', func_get_args(), $pluginInfo);
41-
}
33+
return $pluginInfo ? $this->___callPlugins('publicMethodWithReference', func_get_args(), $pluginInfo) : parent::publicMethodWithReference($classGenerator, $param1, $array);
4234
}
4335

4436
/**
@@ -47,11 +39,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
4739
public function publicChildWithoutParameters()
4840
{
4941
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'publicChildWithoutParameters');
50-
if (!$pluginInfo) {
51-
return parent::publicChildWithoutParameters();
52-
} else {
53-
return $this->___callPlugins('publicChildWithoutParameters', func_get_args(), $pluginInfo);
54-
}
42+
return $pluginInfo ? $this->___callPlugins('publicChildWithoutParameters', func_get_args(), $pluginInfo) : parent::publicChildWithoutParameters();
5543
}
5644

5745
/**
@@ -60,11 +48,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
6048
public function public71($arg1, string $arg2, ?int $arg3, ?int $arg4 = null) : void
6149
{
6250
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'public71');
63-
if (!$pluginInfo) {
64-
parent::public71($arg1, $arg2, $arg3, $arg4);
65-
} else {
66-
$this->___callPlugins('public71', func_get_args(), $pluginInfo);
67-
}
51+
$pluginInfo ? $this->___callPlugins('public71', func_get_args(), $pluginInfo) : parent::public71($arg1, $arg2, $arg3, $arg4);
6852
}
6953

7054
/**
@@ -73,11 +57,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
7357
public function public71Another(?\DateTime $arg1, $arg2 = false) : ?string
7458
{
7559
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'public71Another');
76-
if (!$pluginInfo) {
77-
return parent::public71Another($arg1, $arg2);
78-
} else {
79-
return $this->___callPlugins('public71Another', func_get_args(), $pluginInfo);
80-
}
60+
return $pluginInfo ? $this->___callPlugins('public71Another', func_get_args(), $pluginInfo) : parent::public71Another($arg1, $arg2);
8161
}
8262

8363
/**
@@ -86,11 +66,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
8666
public function publicWithSelf($arg = false) : \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
8767
{
8868
$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-
}
69+
return $pluginInfo ? $this->___callPlugins('publicWithSelf', func_get_args(), $pluginInfo) : parent::publicWithSelf($arg);
9470
}
9571

9672
/**
@@ -99,11 +75,7 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
9975
public function publicParentMethod(\Laminas\Code\Generator\DocBlockGenerator $docBlockGenerator, $param1 = '', $param2 = '\\', $param3 = '\'', array $array = [])
10076
{
10177
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'publicParentMethod');
102-
if (!$pluginInfo) {
103-
return parent::publicParentMethod($docBlockGenerator, $param1, $param2, $param3, $array);
104-
} else {
105-
return $this->___callPlugins('publicParentMethod', func_get_args(), $pluginInfo);
106-
}
78+
return $pluginInfo ? $this->___callPlugins('publicParentMethod', func_get_args(), $pluginInfo) : parent::publicParentMethod($docBlockGenerator, $param1, $param2, $param3, $array);
10779
}
10880

10981
/**
@@ -112,10 +84,6 @@ class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithN
11284
public function publicParentWithoutParameters()
11385
{
11486
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'publicParentWithoutParameters');
115-
if (!$pluginInfo) {
116-
return parent::publicParentWithoutParameters();
117-
} else {
118-
return $this->___callPlugins('publicParentWithoutParameters', func_get_args(), $pluginInfo);
119-
}
87+
return $pluginInfo ? $this->___callPlugins('publicParentWithoutParameters', func_get_args(), $pluginInfo) : parent::publicParentWithoutParameters();
12088
}
12189
}

dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/blacklist/common.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# Example:
44
# app/code/Magento/Catalog
55
# dev/tests/static/framework/bootstrap.php
6-
lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
76
lib/internal/Magento/Framework/Cache/Backend/Eaccelerator.php
87
lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php
8+
lib/internal/Magento/Framework/Interception/Test/Unit/Code/Generator/InterceptorTest.php
9+
lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
910
dev/tests/integration/framework/deployTestModules.php
1011
dev/tests/integration/testsuite/Magento/Framework/Filter/DirectiveProcessor/SimpleDirectiveTest.php
1112
dev/tests/integration/testsuite/Magento/Framework/Session/ConfigTest.php
@@ -21,3 +22,4 @@ app/code/Magento/OfflineShipping/Test/Unit/Model/ResourceModel/Carrier/Tablerate
2122
app/code/Magento/Staging/Test/Unit/Model/Entity/Action/UpdateVersionTest.php
2223
app/code/Magento/Staging/Test/Unit/Model/Operation/DeleteTest.php
2324
app/code/Magento/Staging/Test/Unit/Model/Operation/Update/DefaultTemporaryUpdateProcessorTest.php
25+

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

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@
77

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

10-
/**
11-
* Class Interceptor
12-
˚*
13-
* @package Magento\Framework\Interception\Code\Generator
14-
*/
15-
class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract
10+
use Magento\Framework\Code\Generator\EntityAbstract;
11+
12+
class Interceptor extends EntityAbstract
1613
{
17-
/**
18-
* Entity type
19-
*/
20-
const ENTITY_TYPE = 'interceptor';
14+
public const ENTITY_TYPE = 'interceptor';
2115

2216
/**
2317
* Returns default result class name
@@ -52,9 +46,8 @@ protected function _getDefaultConstructorDefinition()
5246
$parameters = [];
5347
$body = "\$this->___init();\n";
5448
if ($constructor) {
55-
foreach ($constructor->getParameters() as $parameter) {
56-
$parameters[] = $this->_getMethodParameterInfo($parameter);
57-
}
49+
$parameters = array_map([$this, '_getMethodParameterInfo'], $constructor->getParameters());
50+
5851
$body .= count($parameters)
5952
? "parent::__construct({$this->_getParameterList($parameters)});"
6053
: "parent::__construct();";
@@ -70,7 +63,7 @@ protected function _getDefaultConstructorDefinition()
7063
/**
7164
* Returns list of methods for class generator
7265
*
73-
* @return mixed
66+
* @return array
7467
*/
7568
protected function _getClassMethods()
7669
{
@@ -107,33 +100,26 @@ protected function isInterceptedMethod(\ReflectionMethod $method)
107100
*/
108101
protected function _getMethodInfo(\ReflectionMethod $method)
109102
{
110-
$parameters = [];
111-
foreach ($method->getParameters() as $parameter) {
112-
$parameters[] = $this->_getMethodParameterInfo($parameter);
113-
}
103+
$parameters = array_map([$this, '_getMethodParameterInfo'], $method->getParameters());
114104

115105
$returnTypeValue = $this->getReturnTypeValue($method);
116106
$methodInfo = [
117107
'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(),
118108
'parameters' => $parameters,
119109
'body' => str_replace(
120110
[
121-
'%methodName%',
111+
'%method%',
122112
'%return%',
123113
'%parameters%'
124114
],
125115
[
126116
$method->getName(),
127-
$returnTypeValue === 'void' ? '' : ' return',
117+
$returnTypeValue === 'void' ? '' : 'return ',
128118
$this->_getParameterList($parameters)
129119
],
130120
<<<'METHOD_BODY'
131-
$pluginInfo = $this->pluginList->getNext($this->subjectType, '%methodName%');
132-
if (!$pluginInfo) {
133-
%return% parent::%methodName%(%parameters%);
134-
} else {
135-
%return% $this->___callPlugins('%methodName%', func_get_args(), $pluginInfo);
136-
}
121+
$pluginInfo = $this->pluginList->getNext($this->subjectType, '%method%');
122+
%return%$pluginInfo ? $this->___callPlugins('%method%', func_get_args(), $pluginInfo) : parent::%method%(%parameters%);
137123
METHOD_BODY
138124
),
139125
'returnType' => $returnTypeValue,
@@ -206,11 +192,7 @@ protected function _validateData()
206192

207193
if ($resultClassName !== $sourceClassName . '\\Interceptor') {
208194
$this->_addError(
209-
'Invalid Interceptor class name [' .
210-
$resultClassName .
211-
']. Use ' .
212-
$sourceClassName .
213-
'\\Interceptor'
195+
'Invalid Interceptor class name ' . $resultClassName . '. Use ' . $sourceClassName . '\\Interceptor'
214196
);
215197
$result = false;
216198
}

lib/internal/Magento/Framework/Interception/Test/Unit/Code/Generator/_files/Interceptor.txt

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
1818
public function getValue()
1919
{
2020
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getValue');
21-
if (!$pluginInfo) {
22-
return parent::getValue();
23-
} else {
24-
return $this->___callPlugins('getValue', func_get_args(), $pluginInfo);
25-
}
21+
return $pluginInfo ? $this->___callPlugins('getValue', func_get_args(), $pluginInfo) : parent::getValue();
2622
}
2723

2824
/**
@@ -31,11 +27,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
3127
public function setValue($value)
3228
{
3329
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'setValue');
34-
if (!$pluginInfo) {
35-
return parent::setValue($value);
36-
} else {
37-
return $this->___callPlugins('setValue', func_get_args(), $pluginInfo);
38-
}
30+
return $pluginInfo ? $this->___callPlugins('setValue', func_get_args(), $pluginInfo) : parent::setValue($value);
3931
}
4032

4133
/**
@@ -44,11 +36,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
4436
public function & getReference()
4537
{
4638
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getReference');
47-
if (!$pluginInfo) {
48-
return parent::getReference();
49-
} else {
50-
return $this->___callPlugins('getReference', func_get_args(), $pluginInfo);
51-
}
39+
return $pluginInfo ? $this->___callPlugins('getReference', func_get_args(), $pluginInfo) : parent::getReference();
5240
}
5341

5442
/**
@@ -57,11 +45,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
5745
public function firstVariadicParameter(... $variadicValue)
5846
{
5947
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'firstVariadicParameter');
60-
if (!$pluginInfo) {
61-
return parent::firstVariadicParameter(... $variadicValue);
62-
} else {
63-
return $this->___callPlugins('firstVariadicParameter', func_get_args(), $pluginInfo);
64-
}
48+
return $pluginInfo ? $this->___callPlugins('firstVariadicParameter', func_get_args(), $pluginInfo) : parent::firstVariadicParameter(... $variadicValue);
6549
}
6650

6751
/**
@@ -70,11 +54,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
7054
public function secondVariadicParameter($value, ... $variadicValue)
7155
{
7256
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'secondVariadicParameter');
73-
if (!$pluginInfo) {
74-
return parent::secondVariadicParameter($value, ... $variadicValue);
75-
} else {
76-
return $this->___callPlugins('secondVariadicParameter', func_get_args(), $pluginInfo);
77-
}
57+
return $pluginInfo ? $this->___callPlugins('secondVariadicParameter', func_get_args(), $pluginInfo) : parent::secondVariadicParameter($value, ... $variadicValue);
7858
}
7959

8060
/**
@@ -83,10 +63,6 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
8363
public function byRefVariadic(&... $variadicValue)
8464
{
8565
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'byRefVariadic');
86-
if (!$pluginInfo) {
87-
return parent::byRefVariadic(... $variadicValue);
88-
} else {
89-
return $this->___callPlugins('byRefVariadic', func_get_args(), $pluginInfo);
90-
}
66+
return $pluginInfo ? $this->___callPlugins('byRefVariadic', func_get_args(), $pluginInfo) : parent::byRefVariadic(... $variadicValue);
9167
}
9268
}

lib/internal/Magento/Framework/Interception/Test/Unit/Code/Generator/_files/TInterceptor.txt

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
1818
public function returnVoid() : void
1919
{
2020
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'returnVoid');
21-
if (!$pluginInfo) {
22-
parent::returnVoid();
23-
} else {
24-
$this->___callPlugins('returnVoid', func_get_args(), $pluginInfo);
25-
}
21+
$pluginInfo ? $this->___callPlugins('returnVoid', func_get_args(), $pluginInfo) : parent::returnVoid();
2622
}
2723

2824
/**
@@ -31,11 +27,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
3127
public function getNullableValue() : ?string
3228
{
3329
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getNullableValue');
34-
if (!$pluginInfo) {
35-
return parent::getNullableValue();
36-
} else {
37-
return $this->___callPlugins('getNullableValue', func_get_args(), $pluginInfo);
38-
}
30+
return $pluginInfo ? $this->___callPlugins('getNullableValue', func_get_args(), $pluginInfo) : parent::getNullableValue();
3931
}
4032

4133
/**
@@ -44,11 +36,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
4436
public function getValue() : string
4537
{
4638
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getValue');
47-
if (!$pluginInfo) {
48-
return parent::getValue();
49-
} else {
50-
return $this->___callPlugins('getValue', func_get_args(), $pluginInfo);
51-
}
39+
return $pluginInfo ? $this->___callPlugins('getValue', func_get_args(), $pluginInfo) : parent::getValue();
5240
}
5341

5442
/**
@@ -57,11 +45,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
5745
public function setValue(string $value)
5846
{
5947
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'setValue');
60-
if (!$pluginInfo) {
61-
return parent::setValue($value);
62-
} else {
63-
return $this->___callPlugins('setValue', func_get_args(), $pluginInfo);
64-
}
48+
return $pluginInfo ? $this->___callPlugins('setValue', func_get_args(), $pluginInfo) : parent::setValue($value);
6549
}
6650

6751
/**
@@ -70,11 +54,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
7054
public function typeHintedFirstVariadicParameter(string ... $variadicValue)
7155
{
7256
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'typeHintedFirstVariadicParameter');
73-
if (!$pluginInfo) {
74-
return parent::typeHintedFirstVariadicParameter(... $variadicValue);
75-
} else {
76-
return $this->___callPlugins('typeHintedFirstVariadicParameter', func_get_args(), $pluginInfo);
77-
}
57+
return $pluginInfo ? $this->___callPlugins('typeHintedFirstVariadicParameter', func_get_args(), $pluginInfo) : parent::typeHintedFirstVariadicParameter(... $variadicValue);
7858
}
7959

8060
/**
@@ -83,11 +63,7 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
8363
public function typeHintedSecondVariadicParameter(string $value, string ... $variadicValue)
8464
{
8565
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'typeHintedSecondVariadicParameter');
86-
if (!$pluginInfo) {
87-
return parent::typeHintedSecondVariadicParameter($value, ... $variadicValue);
88-
} else {
89-
return $this->___callPlugins('typeHintedSecondVariadicParameter', func_get_args(), $pluginInfo);
90-
}
66+
return $pluginInfo ? $this->___callPlugins('typeHintedSecondVariadicParameter', func_get_args(), $pluginInfo) : parent::typeHintedSecondVariadicParameter($value, ... $variadicValue);
9167
}
9268

9369
/**
@@ -96,10 +72,6 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\TSample
9672
public function byRefTypeHintedVariadic(string &... $variadicValue)
9773
{
9874
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'byRefTypeHintedVariadic');
99-
if (!$pluginInfo) {
100-
return parent::byRefTypeHintedVariadic(... $variadicValue);
101-
} else {
102-
return $this->___callPlugins('byRefTypeHintedVariadic', func_get_args(), $pluginInfo);
103-
}
75+
return $pluginInfo ? $this->___callPlugins('byRefTypeHintedVariadic', func_get_args(), $pluginInfo) : parent::byRefTypeHintedVariadic(... $variadicValue);
10476
}
10577
}

0 commit comments

Comments
 (0)