Skip to content

Commit 56b35a3

Browse files
authored
Merge pull request #52 from shochdoerfer/fix/dataobject_magic_methods
Fix params for DataObject get & set
2 parents 93d0b50 + a105166 commit 56b35a3

File tree

4 files changed

+300
-56
lines changed

4 files changed

+300
-56
lines changed

src/bitExpert/PHPStan/Magento/Reflection/AbstractMagicMethodReflectionExtension.php

Lines changed: 106 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract class AbstractMagicMethodReflectionExtension implements MethodsClassRef
3434
* @param ClassReflection $classReflection
3535
* @param string $methodName
3636
* @return MethodReflection
37+
* @throws ShouldNotHappenException
3738
*/
3839
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
3940
{
@@ -58,27 +59,42 @@ public function getMethod(ClassReflection $classReflection, string $methodName):
5859
* @param ClassReflection $classReflection
5960
* @param string $methodName
6061
* @return MethodReflection
62+
* @throws ShouldNotHappenException
6163
*/
62-
private function returnGetMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
64+
protected function returnGetMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
6365
{
64-
$params = [
65-
new DummyParameter(
66-
'key',
67-
new StringType(),
68-
true,
69-
null,
70-
false,
71-
null
72-
),
73-
new DummyParameter(
74-
'index',
75-
new UnionType([new StringType(), new IntegerType(), new NullType()]),
76-
true,
77-
null,
78-
false,
79-
null
80-
)
81-
];
66+
$params = [];
67+
if ($methodName === 'getData') {
68+
$params = [
69+
new DummyParameter(
70+
'key',
71+
new StringType(),
72+
true,
73+
null,
74+
false,
75+
null
76+
),
77+
new DummyParameter(
78+
'index',
79+
new UnionType([new StringType(), new IntegerType(), new NullType()]),
80+
true,
81+
null,
82+
false,
83+
null
84+
)
85+
];
86+
} else {
87+
$params = [
88+
new DummyParameter(
89+
'value',
90+
new MixedType(),
91+
true,
92+
null,
93+
false,
94+
null
95+
)
96+
];
97+
}
8298

8399
$returnType = new MixedType();
84100

@@ -93,26 +109,48 @@ private function returnGetMagicMethod(ClassReflection $classReflection, string $
93109
return new MagicMethodReflection($methodName, $classReflection, [$variants]);
94110
}
95111

96-
private function returnSetMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
112+
/**
113+
* Helper method to create magic method reflection for set() calls.
114+
*
115+
* @param ClassReflection $classReflection
116+
* @param string $methodName
117+
* @return MethodReflection
118+
* @throws ShouldNotHappenException
119+
*/
120+
protected function returnSetMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
97121
{
98-
$params = [
99-
new DummyParameter(
100-
'key',
101-
new UnionType([new StringType(), new ArrayType(new MixedType(), new MixedType())]),
102-
true,
103-
null,
104-
false,
105-
null
106-
),
107-
new DummyParameter(
108-
'value',
109-
new MixedType(),
110-
true,
111-
null,
112-
false,
113-
null
114-
)
115-
];
122+
$params = [];
123+
if ($methodName === 'setData') {
124+
$params = [
125+
new DummyParameter(
126+
'key',
127+
new UnionType([new StringType(), new ArrayType(new MixedType(), new MixedType())]),
128+
true,
129+
null,
130+
false,
131+
null
132+
),
133+
new DummyParameter(
134+
'value',
135+
new MixedType(),
136+
true,
137+
null,
138+
false,
139+
null
140+
)
141+
];
142+
} else {
143+
$params = [
144+
new DummyParameter(
145+
'value',
146+
new MixedType(),
147+
true,
148+
null,
149+
false,
150+
null
151+
)
152+
];
153+
}
116154

117155
$returnType = new ObjectType($classReflection->getName());
118156

@@ -127,7 +165,15 @@ private function returnSetMagicMethod(ClassReflection $classReflection, string $
127165
return new MagicMethodReflection($methodName, $classReflection, [$variants]);
128166
}
129167

130-
private function returnUnsetMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
168+
/**
169+
* Helper method to create magic method reflection for unset() calls.
170+
*
171+
* @param ClassReflection $classReflection
172+
* @param string $methodName
173+
* @return MethodReflection
174+
* @throws ShouldNotHappenException
175+
*/
176+
protected function returnUnsetMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
131177
{
132178
$params = [
133179
new DummyParameter(
@@ -153,18 +199,28 @@ private function returnUnsetMagicMethod(ClassReflection $classReflection, string
153199
return new MagicMethodReflection($methodName, $classReflection, [$variants]);
154200
}
155201

156-
private function returnHasMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
202+
/**
203+
* Helper method to create magic method reflection for has() calls.
204+
*
205+
* @param ClassReflection $classReflection
206+
* @param string $methodName
207+
* @return MethodReflection
208+
*/
209+
protected function returnHasMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
157210
{
158-
$params = [
159-
new DummyParameter(
160-
'key',
161-
new StringType(),
162-
true,
163-
null,
164-
false,
165-
null
166-
)
167-
];
211+
$params = [];
212+
if ($methodName === 'hasData') {
213+
$params = [
214+
new DummyParameter(
215+
'key',
216+
new StringType(),
217+
true,
218+
null,
219+
false,
220+
null
221+
)
222+
];
223+
}
168224

169225
$returnType = new BooleanType();
170226

src/bitExpert/PHPStan/Magento/Reflection/Framework/Session/SessionManagerMagicMethodReflectionExtension.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@
1313
namespace bitExpert\PHPStan\Magento\Reflection\Framework\Session;
1414

1515
use bitExpert\PHPStan\Magento\Reflection\AbstractMagicMethodReflectionExtension;
16+
use bitExpert\PHPStan\Magento\Reflection\MagicMethodReflection;
1617
use PHPStan\Reflection\ClassReflection;
18+
use PHPStan\Reflection\FunctionVariant;
19+
use PHPStan\Reflection\MethodReflection;
20+
use PHPStan\Reflection\Php\DummyParameter;
21+
use PHPStan\ShouldNotHappenException;
22+
use PHPStan\Type\BooleanType;
23+
use PHPStan\Type\Generic\TemplateTypeMap;
24+
use PHPStan\Type\IntegerType;
25+
use PHPStan\Type\MixedType;
26+
use PHPStan\Type\NullType;
27+
use PHPStan\Type\StringType;
28+
use PHPStan\Type\UnionType;
1729

1830
class SessionManagerMagicMethodReflectionExtension extends AbstractMagicMethodReflectionExtension
1931
{
@@ -27,4 +39,60 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
2739
return $classReflection->isSubclassOf('Magento\Framework\Session\SessionManager') &&
2840
in_array(substr($methodName, 0, 3), ['get', 'set', 'uns', 'has']);
2941
}
42+
43+
/**
44+
* Helper method to create magic method reflection for get() calls.
45+
*
46+
* @param ClassReflection $classReflection
47+
* @param string $methodName
48+
* @return MethodReflection
49+
* @throws ShouldNotHappenException
50+
*/
51+
protected function returnGetMagicMethod(ClassReflection $classReflection, string $methodName): MethodReflection
52+
{
53+
$params = [];
54+
if ($methodName === 'getData') {
55+
$params = [
56+
new DummyParameter(
57+
'key',
58+
new StringType(),
59+
true,
60+
null,
61+
false,
62+
null
63+
),
64+
new DummyParameter(
65+
'cache',
66+
new BooleanType(),
67+
true,
68+
null,
69+
false,
70+
null
71+
)
72+
];
73+
} else {
74+
$params = [
75+
new DummyParameter(
76+
'value',
77+
new MixedType(),
78+
true,
79+
null,
80+
false,
81+
null
82+
)
83+
];
84+
}
85+
86+
$returnType = new MixedType();
87+
88+
$variants = new FunctionVariant(
89+
TemplateTypeMap::createEmpty(),
90+
null,
91+
$params,
92+
false,
93+
$returnType
94+
);
95+
96+
return new MagicMethodReflection($methodName, $classReflection, [$variants]);
97+
}
3098
}

tests/bitExpert/PHPStan/Magento/Reflection/Framework/DataObjectMagicMethodReflectionExtensionUnitTest.php

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DataObjectMagicMethodReflectionExtensionUnitTest extends TestCase
2727
* @var DataObjectMagicMethodReflectionExtension
2828
*/
2929
private $extension;
30+
3031
/**
3132
* @var ClassReflection|\PHPUnit\Framework\MockObject\MockObject
3233
*/
@@ -41,9 +42,9 @@ protected function setUp(): void
4142
/**
4243
* @test
4344
*/
44-
public function returnMagicMethodReflectionForGetMethod(): void
45+
public function returnMagicMethodReflectionForGetDataMethod(): void
4546
{
46-
$methodReflection = $this->extension->getMethod($this->classReflection, 'getTest');
47+
$methodReflection = $this->extension->getMethod($this->classReflection, 'getData');
4748

4849
$variants = $methodReflection->getVariants();
4950
$params = $variants[0]->getParameters();
@@ -58,9 +59,25 @@ public function returnMagicMethodReflectionForGetMethod(): void
5859
/**
5960
* @test
6061
*/
61-
public function returnMagicMethodReflectionForSetMethod(): void
62+
public function returnMagicMethodReflectionForGetMethod(): void
6263
{
63-
$methodReflection = $this->extension->getMethod($this->classReflection, 'setTest');
64+
$methodReflection = $this->extension->getMethod($this->classReflection, 'getTest');
65+
66+
$variants = $methodReflection->getVariants();
67+
$params = $variants[0]->getParameters();
68+
69+
$this->assertCount(1, $variants);
70+
$this->assertInstanceOf(MixedType::class, $variants[0]->getReturnType());
71+
$this->assertCount(1, $params);
72+
$this->assertInstanceOf(MixedType::class, $params[0]->getType());
73+
}
74+
75+
/**
76+
* @test
77+
*/
78+
public function returnMagicMethodReflectionForSetDataMethod(): void
79+
{
80+
$methodReflection = $this->extension->getMethod($this->classReflection, 'setData');
6481

6582
$variants = $methodReflection->getVariants();
6683
$params = $variants[0]->getParameters();
@@ -72,6 +89,22 @@ public function returnMagicMethodReflectionForSetMethod(): void
7289
$this->assertInstanceOf(MixedType::class, $params[1]->getType());
7390
}
7491

92+
/**
93+
* @test
94+
*/
95+
public function returnMagicMethodReflectionForSetMethod(): void
96+
{
97+
$methodReflection = $this->extension->getMethod($this->classReflection, 'setTest');
98+
99+
$variants = $methodReflection->getVariants();
100+
$params = $variants[0]->getParameters();
101+
102+
$this->assertCount(1, $variants);
103+
$this->assertInstanceOf(ObjectType::class, $variants[0]->getReturnType());
104+
$this->assertCount(1, $params);
105+
$this->assertInstanceOf(MixedType::class, $params[0]->getType());
106+
}
107+
75108
/**
76109
* @test
77110
*/
@@ -91,9 +124,9 @@ public function returnMagicMethodReflectionForUnsetMethod(): void
91124
/**
92125
* @test
93126
*/
94-
public function returnMagicMethodReflectionForHasMethod(): void
127+
public function returnMagicMethodReflectionForHasDataMethod(): void
95128
{
96-
$methodReflection = $this->extension->getMethod($this->classReflection, 'hasTest');
129+
$methodReflection = $this->extension->getMethod($this->classReflection, 'hasData');
97130

98131
$variants = $methodReflection->getVariants();
99132
$params = $variants[0]->getParameters();
@@ -104,6 +137,21 @@ public function returnMagicMethodReflectionForHasMethod(): void
104137
$this->assertInstanceOf(StringType::class, $params[0]->getType());
105138
}
106139

140+
/**
141+
* @test
142+
*/
143+
public function returnMagicMethodReflectionForHasMethod(): void
144+
{
145+
$methodReflection = $this->extension->getMethod($this->classReflection, 'hasTest');
146+
147+
$variants = $methodReflection->getVariants();
148+
$params = $variants[0]->getParameters();
149+
150+
$this->assertCount(1, $variants);
151+
$this->assertInstanceOf(BooleanType::class, $variants[0]->getReturnType());
152+
$this->assertCount(0, $params);
153+
}
154+
107155
/**
108156
* @test
109157
*/

0 commit comments

Comments
 (0)