Skip to content

Commit 9fed236

Browse files
author
oleksandrkravchuk
committed
PHPStan add support of magic methods of Data Object.
Get rid of magic numbers.
1 parent c7a02e0 commit 9fed236

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

dev/tests/static/framework/Magento/PhpStan/Reflection/Php/DataObjectClassReflectionExtension.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
*/
2121
class DataObjectClassReflectionExtension implements MethodsClassReflectionExtension
2222
{
23-
private const MAGIC_METHODS_PREFIXES = ['get', 'set', 'uns', 'has'];
23+
private const MAGIC_METHODS_PREFIXES = [
24+
'get',
25+
'set',
26+
'uns',
27+
'has'
28+
];
29+
30+
private const PREFIX_LENGTH = 3;
2431

2532
/**
2633
* @var Container
@@ -57,22 +64,32 @@ public function hasMethod(ClassReflection $classReflection, string $methodName):
5764
// In case when annotation already available for the method, we will not use 'magic methods' approach.
5865
return false;
5966
}
60-
6167
if ($classReflection->isSubclassOf(DataObject::class) || $classReflection->getName() == DataObject::class) {
62-
return in_array(substr($methodName, 0, 3), self::MAGIC_METHODS_PREFIXES);
68+
return in_array($this->getPrefix($methodName), self::MAGIC_METHODS_PREFIXES);
6369
}
64-
6570
/** SessionManager redirects all calls to `__call` to container which extends DataObject */
6671
if ($classReflection->isSubclassOf(SessionManager::class)
6772
|| $classReflection->getName() === SessionManager::class
6873
) {
6974
/** @see \Magento\Framework\Session\SessionManager::__call */
70-
return in_array(substr($methodName, 0, 3), self::MAGIC_METHODS_PREFIXES);
75+
return in_array($this->getPrefix($methodName), self::MAGIC_METHODS_PREFIXES);
7176
}
7277

7378
return false;
7479
}
7580

81+
/**
82+
* Get prefix from method name.
83+
*
84+
* @param string $methodName
85+
*
86+
* @return string
87+
*/
88+
private function getPrefix(string $methodName): string
89+
{
90+
return (string)substr($methodName, 0, self::PREFIX_LENGTH);
91+
}
92+
7693
/**
7794
* Get method reflection instance.
7895
*

dev/tests/static/framework/Magento/PhpStan/Reflection/Php/DataObjectMethodReflection.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*/
3131
class DataObjectMethodReflection implements MethodReflection
3232
{
33+
private const PREFIX_LENGTH = 3;
34+
3335
/**
3436
* @var ClassReflection
3537
*/
@@ -138,6 +140,16 @@ public function getVariants(): array
138140
];
139141
}
140142

143+
/**
144+
* Get prefix from method name.
145+
*
146+
* @return string
147+
*/
148+
private function getMethodNamePrefix(): string
149+
{
150+
return (string)substr($this->methodName, 0, self::PREFIX_LENGTH);
151+
}
152+
141153
/**
142154
* Get Magic Methods parameters.
143155
*
@@ -146,7 +158,7 @@ public function getVariants(): array
146158
private function getMethodParameters(): array
147159
{
148160
$params = [];
149-
switch (substr($this->methodName, 0, 3)) {
161+
switch ($this->getMethodNamePrefix()) {
150162
case 'set':
151163
$params[] = new DummyParameter(
152164
'value',
@@ -179,7 +191,7 @@ private function getMethodParameters(): array
179191
*/
180192
private function getReturnType(): Type
181193
{
182-
switch (substr($this->methodName, 0, 3)) {
194+
switch ($this->getMethodNamePrefix()) {
183195
case 'set':
184196
case 'uns':
185197
$returnType = new ObjectType($this->classReflection->getName());

0 commit comments

Comments
 (0)