-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathDataObjectClassReflectionExtension.php
105 lines (94 loc) · 3.24 KB
/
DataObjectClassReflectionExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\PhpStan\Reflection\Php;
use Magento\Framework\DataObject;
use Magento\Framework\Session\SessionManager;
use PHPStan\DependencyInjection\Container;
use PHPStan\Reflection\Annotations\AnnotationsMethodsClassReflectionExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
/**
* Extension to add support of magic methods (get/set/uns/has) based on @see DataObject
*/
class DataObjectClassReflectionExtension implements MethodsClassReflectionExtension
{
private const MAGIC_METHODS_PREFIXES = [
'get',
'set',
'uns',
'has'
];
private const PREFIX_LENGTH = 3;
/**
* @var Container
*/
private $container;
/**
* @var AnnotationsMethodsClassReflectionExtension
*/
private $annotationsMethodsClassReflectionExtension;
/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
$this->annotationsMethodsClassReflectionExtension = $this->container
->getByType(AnnotationsMethodsClassReflectionExtension::class);
}
/**
* Check if class has relations with DataObject and requested method can be considered as a magic method.
*
* @param ClassReflection $classReflection
* @param string $methodName
*
* @return bool
*/
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
// Workaround due to annotation extension always loads last.
if ($this->annotationsMethodsClassReflectionExtension->hasMethod($classReflection, $methodName)) {
// In case when annotation already available for the method, we will not use 'magic methods' approach.
return false;
}
if ($classReflection->isSubclassOf(DataObject::class) || $classReflection->getName() == DataObject::class) {
return in_array($this->getPrefix($methodName), self::MAGIC_METHODS_PREFIXES);
}
/** SessionManager redirects all calls to `__call` to container which extends DataObject */
if ($classReflection->isSubclassOf(SessionManager::class)
|| $classReflection->getName() === SessionManager::class
) {
/** @see \Magento\Framework\Session\SessionManager::__call */
return in_array($this->getPrefix($methodName), self::MAGIC_METHODS_PREFIXES);
}
return false;
}
/**
* Get prefix from method name.
*
* @param string $methodName
*
* @return string
*/
private function getPrefix(string $methodName): string
{
return (string)substr($methodName, 0, self::PREFIX_LENGTH);
}
/**
* Get method reflection instance.
*
* @param ClassReflection $classReflection
* @param string $methodName
*
* @return MethodReflection
*/
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
return new DataObjectMethodReflection($classReflection, $methodName);
}
}