EnvironmentDataLoader::initializeVars()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

56
        $fetchKey = function (string|int $key, /** @scrutinizer ignore-unused */ $value): string {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
Documentation Bug introduced by
The doc comment array-key at position 0 could not be parsed: Unknown type name 'array-key' at position 0 in array-key.
Loading history...
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