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