|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace NamespaceProtector\Parser\Node; |
|
6
|
|
|
|
|
7
|
|
|
use NamespaceProtector\Entry\Entry; |
|
8
|
|
|
use NamespaceProtector\Config\Config; |
|
9
|
|
|
use NamespaceProtector\Db\BooleanMatchKey; |
|
10
|
|
|
use NamespaceProtector\Db\BooleanMatchPos; |
|
11
|
|
|
use NamespaceProtector\Db\BooleanMatchValue; |
|
12
|
|
|
use NamespaceProtector\Db\DbKeyValueInterface; |
|
13
|
|
|
use NamespaceProtector\Db\MatchCollectionInterface; |
|
14
|
|
|
use NamespaceProtector\EnvironmentDataLoaderInterface; |
|
15
|
|
|
use NamespaceProtector\Parser\Node\Event\EventProcessNodeInterface; |
|
16
|
|
|
|
|
17
|
|
|
class ProcessUseStatement |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var EnvironmentDataLoaderInterface */ |
|
20
|
|
|
private $metadataLoader; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Config */ |
|
23
|
|
|
private $globalConfig; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
EnvironmentDataLoaderInterface $metadataLoader, |
|
27
|
|
|
Config $configGlobal |
|
28
|
|
|
) { |
|
29
|
|
|
$this->globalConfig = $configGlobal; |
|
30
|
|
|
$this->metadataLoader = $metadataLoader; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function __invoke(EventProcessNodeInterface $event): void |
|
34
|
|
|
{ |
|
35
|
|
|
$val = new Entry($event->getNodeName()); |
|
36
|
|
|
if ($this->isFalsePositives($val)) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($this->globalConfig->getMode() === Config::MODE_MAKE_VENDOR_PRIVATE) { |
|
41
|
|
|
$this->withModeVendorPrivate($val, $event); |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (true === $this->isInConfiguredComposerPsr4Namespaces($val)) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (true === $this->isInPrivateConfiguredEntries($val)) { |
|
50
|
|
|
$event->foundError(); |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function withModeVendorPrivate(Entry $currentNamespaceAccess, EventProcessNodeInterface $event): void |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->isInPublicConfiguredEntries($currentNamespaceAccess)) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (true === $this->isInConfiguredComposerPsr4Namespaces($currentNamespaceAccess)) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$event->foundError(); |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function isFalsePositives(Entry $resultTocheck): bool |
|
70
|
|
|
{ |
|
71
|
|
|
$result = $this->stripFirstSlash($resultTocheck); |
|
72
|
|
|
|
|
73
|
|
|
if ($this->valueExist($this->metadataLoader->getCollectBaseConstants(), new BooleanMatchKey(), $result)) { |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($this->valueExist($this->metadataLoader->getCollectBaseFunctions(), new BooleanMatchValue(), $result)) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($this->valueExist($this->metadataLoader->getCollectBaseInterfaces(), new BooleanMatchValue(), $result)) { |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($this->valueExist($this->metadataLoader->getCollectBaseClasses(), new BooleanMatchValue(), $result)) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function valueExist(DbKeyValueInterface $collections, MatchCollectionInterface $matchCriteria, Entry $matchMe): bool |
|
93
|
|
|
{ |
|
94
|
|
|
if ($collections->booleanSearch($matchCriteria, $matchMe)) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function stripFirstSlash(Entry $token): Entry |
|
102
|
|
|
{ |
|
103
|
|
|
if ($token->get()[0] === '\\') { |
|
104
|
|
|
return new Entry(substr($token->get(), 1, strlen($token->get()))); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $token; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function isInPublicConfiguredEntries(Entry $currentNamespaceAccess): bool |
|
111
|
|
|
{ |
|
112
|
|
|
foreach ($this->globalConfig->getPublicEntries() as $publicEntry) { |
|
113
|
|
|
$publicEntry = \strtolower($publicEntry); |
|
114
|
|
|
$current = \strtolower($currentNamespaceAccess->get()); |
|
115
|
|
|
if (strpos($current, $publicEntry) !== false) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function isInPrivateConfiguredEntries(Entry $currentNamespaceAccess): bool |
|
124
|
|
|
{ |
|
125
|
|
|
foreach ($this->globalConfig->getPrivateEntries() as $privateEntry) { |
|
126
|
|
|
$privateEntry = \strtolower($privateEntry); |
|
127
|
|
|
$current = \strtolower($currentNamespaceAccess->get()); |
|
128
|
|
|
if (strpos($current, $privateEntry) !== false) { |
|
129
|
|
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function isInConfiguredComposerPsr4Namespaces(Entry $val): bool |
|
136
|
|
|
{ |
|
137
|
|
|
$val = $this->stripFirstSlash($val); |
|
138
|
|
|
|
|
139
|
|
|
if ($this->metadataLoader |
|
140
|
|
|
->getCollectComposerNamespace() |
|
141
|
|
|
->booleanSearch(new BooleanMatchPos(), $val) |
|
142
|
|
|
) { |
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|