1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace NamespaceProtector; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use NamespaceProtector\Db\DbKeyValue; |
8
|
|
|
use NamespaceProtector\Db\BooleanMatchNameSpace; |
9
|
|
|
use NamespaceProtector\Metadata\VendorNamespace; |
10
|
|
|
use NamespaceProtector\Scanner\ComposerJsonInterface; |
11
|
|
|
use NamespaceProtector\Metadata\VendorNamespaceInterface; |
12
|
|
|
|
13
|
|
|
final class EnvironmentDataLoader implements EnvironmentDataLoaderInterface |
14
|
|
|
{ |
15
|
|
|
public const NAMESPACE_PROJECT = 'NamespaceProtector'; |
16
|
|
|
|
17
|
|
|
/** @var DbKeyValue */ |
18
|
|
|
private $collectBaseClasses; |
19
|
|
|
|
20
|
|
|
/** @var DbKeyValue */ |
21
|
|
|
private $collectBaseInterfaces; |
22
|
|
|
|
23
|
|
|
/** @var DbKeyValue */ |
24
|
|
|
private $collectBaseFunctions; |
25
|
|
|
|
26
|
|
|
/** @var DbKeyValue */ |
27
|
|
|
private $collectBaseConstants; |
28
|
|
|
|
29
|
|
|
/** @var DbKeyValue */ |
30
|
|
|
private $collectComposerNamespace; |
31
|
|
|
|
32
|
|
|
private VendorNamespaceInterface $vendorNamespaces; |
33
|
|
|
|
34
|
|
|
/** @var ComposerJsonInterface */ |
35
|
|
|
private $composerJson; |
36
|
|
|
|
37
|
|
|
public function __construct(ComposerJsonInterface $composerJson) |
38
|
|
|
{ |
39
|
|
|
$this->composerJson = $composerJson; |
40
|
|
|
$this->initializeVars(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function initializeVars(): void |
44
|
|
|
{ |
45
|
|
|
$this->collectBaseClasses = new DbKeyValue(); |
46
|
|
|
$this->collectBaseInterfaces = new DbKeyValue(); |
47
|
|
|
$this->collectBaseFunctions = new DbKeyValue(); |
48
|
|
|
$this->collectBaseConstants = new DbKeyValue(); |
49
|
|
|
$this->collectComposerNamespace = new DbKeyValue(); |
50
|
|
|
$this->vendorNamespaces = new VendorNamespace(new BooleanMatchNameSpace()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function load(): void |
54
|
|
|
{ |
55
|
|
|
/** @var string|int $value */ |
56
|
|
|
$fetchValue = function ($key, $value): string {return (string)$value; }; |
57
|
|
|
|
58
|
|
|
/** @var string|int $value */ |
59
|
|
|
$fetchKey = function ($key, $value): string {return (string)$key; }; |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->collectBaseFunctions = $this->fillFromArray(\get_defined_functions()['internal'], $fetchValue); |
62
|
|
|
$this->collectBaseInterfaces = $this->fillFromArray(\get_declared_interfaces(), $fetchValue); |
63
|
|
|
|
64
|
|
|
$internalClass = []; |
65
|
|
|
foreach (get_declared_classes() as $class) { |
66
|
|
|
$aClass = new ReflectionClass($class); |
67
|
|
|
if ($aClass->isInternal()) { |
68
|
|
|
$internalClass[$class] = $aClass->getName(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->collectBaseClasses = $this->fillFromArray($internalClass, $fetchKey); |
73
|
|
|
$this->collectBaseConstants = $this->fillFromArray(\get_defined_constants(), $fetchKey); |
74
|
|
|
$this->collectComposerNamespace = $this->fillFromArray($this->composerJson->getPsr4Ns(), $fetchKey); |
75
|
|
|
|
76
|
|
|
$this->vendorNamespaces->load(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return DbKeyValue |
81
|
|
|
*/ |
82
|
|
|
public function getCollectBaseClasses(): DbKeyValue |
83
|
|
|
{ |
84
|
|
|
return $this->collectBaseClasses; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return DbKeyValue |
89
|
|
|
*/ |
90
|
|
|
public function getCollectBaseInterfaces(): DbKeyValue |
91
|
|
|
{ |
92
|
|
|
return $this->collectBaseInterfaces; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return DbKeyValue |
97
|
|
|
*/ |
98
|
|
|
public function getCollectBaseFunctions(): DbKeyValue |
99
|
|
|
{ |
100
|
|
|
return $this->collectBaseFunctions; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return DbKeyValue |
105
|
|
|
*/ |
106
|
|
|
public function getCollectBaseConstants(): DbKeyValue |
107
|
|
|
{ |
108
|
|
|
return $this->collectBaseConstants; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return DbKeyValue |
113
|
|
|
*/ |
114
|
|
|
public function getCollectComposerNamespace(): DbKeyValue |
115
|
|
|
{ |
116
|
|
|
return $this->collectComposerNamespace; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array<mixed> $collections |
121
|
|
|
*/ |
122
|
|
|
private function fillFromArray(array $collections, Closure $fetchValue): DbKeyValue |
123
|
|
|
{ |
124
|
|
|
$db = new DbKeyValue(); |
125
|
|
|
|
126
|
|
|
/** @var string $value */ |
127
|
|
|
foreach ($collections as $key => $value) { |
128
|
|
|
/** @var string $checkValue */ |
129
|
|
|
$checkValue = $fetchValue($key, $value); |
130
|
|
|
$pos = \strpos($checkValue, self::NAMESPACE_PROJECT); |
131
|
|
|
|
132
|
|
|
if ($pos === false) { |
133
|
|
|
$db->add((string)$key, (string)$value); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $db; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function vendorNamespaces(): VendorNamespaceInterface |
141
|
|
|
{ |
142
|
|
|
return $this->vendorNamespaces; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.