-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathClassPreferencePhp.php
170 lines (148 loc) · 5.33 KB
/
ClassPreferencePhp.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Ampersand\PatchHelper\Checks;
use Ampersand\PatchHelper\Checks;
use Ampersand\PatchHelper\Exception\VirtualTypeException;
use Ampersand\PatchHelper\Helper\Magento2Instance;
use Ampersand\PatchHelper\Patchfile\Entry as PatchEntry;
class ClassPreferencePhp extends AbstractCheck
{
/**
* @var string[] $vendorNamespaces
*/
private $vendorNamespaces = [];
/**
* @param Magento2Instance $m2
* @param PatchEntry $patchEntry
* @param string $appCodeFilepath
* @param array<string, array<string, string>> $warnings
* @param array<string, array<string, string>> $infos
* @param array<string, array<string, string>> $ignored
* @param array<int, string> $vendorNamespaces
*/
public function __construct(
Magento2Instance $m2,
PatchEntry $patchEntry,
string $appCodeFilepath,
array &$warnings,
array &$infos,
array &$ignored,
array $vendorNamespaces
) {
$this->vendorNamespaces = $vendorNamespaces;
parent::__construct($m2, $patchEntry, $appCodeFilepath, $warnings, $infos, $ignored);
}
/**
* @return bool
*/
public function canCheck()
{
return pathinfo($this->patchEntry->getPath(), PATHINFO_EXTENSION) === 'php';
}
/**
* Use the object manager to check for preferences
*
* @return void
*/
public function check()
{
$file = $this->appCodeFilepath;
$class = ltrim($file, 'app/code/');
$class = preg_replace('/\\.[^.\\s]{3,4}$/', '', $class);
$class = str_replace('/', '\\', $class);
$preferences = [];
$areaConfig = $this->m2->getAreaConfig();
foreach (array_keys($areaConfig) as $area) {
if (isset($areaConfig[$area]['preferences'][$class])) {
$preference = $areaConfig[$area]['preferences'][$class];
if ($this->isThirdPartyPreference($class, $preference, $this->vendorNamespaces)) {
$preferences[] = $preference;
}
}
}
// Use raw framework
$preference = $this->m2->getConfig()->getPreference($class);
if ($this->isThirdPartyPreference($class, $preference, $this->vendorNamespaces)) {
$preferences[] = $preference;
}
$preferences = array_unique($preferences);
$type = Checks::TYPE_PREFERENCE;
if ($this->patchEntry->fileWasRemoved()) {
$type = Checks::TYPE_PREFERENCE_REMOVED;
}
foreach ($preferences as $preference) {
if ($this->patchEntry->vendorChangeIsNotMeaningful()) {
$this->ignored[$type][] = $preference;
} else {
$this->warnings[$type][] = $preference;
}
}
}
/**
* @param string $class
* @param string $preference
* @param string[] $vendorNamespaces
*
* @return bool
*/
private function isThirdPartyPreference(string $class, string $preference, array $vendorNamespaces = [])
{
if ($preference === $class || $preference === "$class\\Interceptor") {
// Class is not overridden
return false;
}
try {
$path = $this->getFilenameFromPhpClass($preference);
} catch (\Throwable $throwable) {
$tmpPreference = str_replace('\Interceptor', '', $preference);
$tmpPreference = trim($tmpPreference, '\\');
if (str_contains($throwable->getMessage(), 'not found')) {
if (!str_contains($throwable->getMessage(), $tmpPreference)) {
// this is a Class not found error, and its not about the preference we're investigating
// this means its one of the parent classes that is no longer valid, report it.
return true;
}
}
throw $throwable;
}
$pathModule = $this->m2->getModuleFromPath($this->patchEntry->getPath());
$preferenceModule = $this->m2->getModuleFromPath($path);
if ($preferenceModule && $preferenceModule == $pathModule) {
return false; // This preference is in the same module as the definition of the interface, do not report
}
if (!empty($vendorNamespaces)) {
foreach ($vendorNamespaces as $vendorNamespace) {
if (str_starts_with($preference, $vendorNamespace)) {
return true;
}
}
return false;
}
$pathsToIgnore = [
'/vendor/magento/',
'/generated/code/Magento/',
'/generation/Magento/',
'/setup/src/Magento/'
];
foreach ($pathsToIgnore as $pathToIgnore) {
if (str_contains($path, $pathToIgnore)) {
// Class is overridden by magento itself, ignore
return false;
}
}
return true;
}
/**
* @param string $class
* @return false|string
* @throws VirtualTypeException
*/
private function getFilenameFromPhpClass(string $class)
{
try {
$refClass = new \ReflectionClass($class);
} catch (\Exception $e) {
throw new VirtualTypeException("Could not instantiate $class (virtualType?)");
}
return realpath($refClass->getFileName());
}
}