Passed
Push — master ( fa352d...0eb7bd )
by BruceScrutinizer
02:09
created

EnvironmentDataLoader::getCollectBaseInterfaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Scanner\ComposerJson;
9
10
final class EnvironmentDataLoader implements EnvironmentDataLoaderInterface
11
{
12
    public const NAMESPACE_PROJECT = 'NamespaceProtector';
13
14
    /** @var DbKeyValue */
15
    private $collectBaseClasses;
16
17
    /** @var DbKeyValue */
18
    private $collectBaseInterfaces;
19
20
    /** @var DbKeyValue */
21
    private $collectBaseFunctions;
22
23
    /** @var DbKeyValue */
24
    private $collectBaseConstants;
25
26
    /** @var DbKeyValue */
27
    private $collectComposerNamespace;
28
29
    /** @var ComposerJson */
30
    private $composerJson;
31
32
    public function __construct(ComposerJson $composerJson)
33
    {
34
        $this->composerJson = $composerJson;
35
        $this->initializeVars();
36
    }
37
38
    private function initializeVars(): void
39
    {
40
        $this->collectBaseClasses = new DbKeyValue();
41
        $this->collectBaseInterfaces = new DbKeyValue();
42
        $this->collectBaseFunctions = new DbKeyValue();
43
        $this->collectBaseConstants = new DbKeyValue();
44
        $this->collectComposerNamespace = new DbKeyValue();
45
    }
46
47
    public function load(): void
48
    {
49
        $fetchValue = function ($key, $value) {return $value; };
50
        $fetchKey = function ($key, $value) {return $key; };
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

50
        $fetchKey = function ($key, /** @scrutinizer ignore-unused */ $value) {return $key; };

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...
51
52
        $this->collectBaseFunctions = $this->fillFromArray(\get_defined_functions()['internal'], $fetchValue);
53
        $this->collectBaseInterfaces = $this->fillFromArray(\get_declared_interfaces(), $fetchValue);
54
55
        $internalClass = [];
56
        foreach (get_declared_classes() as $class) {
57
            $aClass = new ReflectionClass($class);
58
            if ($aClass->isInternal()) {
59
                $internalClass[$class] = $aClass->getName();
60
            }
61
        }
62
        $this->collectBaseClasses = $this->fillFromArray($internalClass, $fetchKey);
63
        $this->collectBaseConstants = $this->fillFromArray(\get_defined_constants(), $fetchKey);
64
        $this->collectComposerNamespace = $this->fillFromArray($this->composerJson->getPsr4Ns(), $fetchKey);
65
    }
66
67
    /**
68
     * @return DbKeyValue
69
     */
70
    public function getCollectBaseClasses(): DbKeyValue
71
    {
72
        return $this->collectBaseClasses;
73
    }
74
75
    /**
76
     * @return DbKeyValue
77
     */
78
    public function getCollectBaseInterfaces(): DbKeyValue
79
    {
80
        return $this->collectBaseInterfaces;
81
    }
82
83
    /**
84
     * @return DbKeyValue
85
     */
86
    public function getCollectBaseFunctions(): DbKeyValue
87
    {
88
        return $this->collectBaseFunctions;
89
    }
90
91
    /**
92
     * @return DbKeyValue
93
     */
94
    public function getCollectBaseConstants(): DbKeyValue
95
    {
96
        return $this->collectBaseConstants;
97
    }
98
99
    /**
100
     * @return DbKeyValue
101
     */
102
    public function getCollectComposerNamespace(): DbKeyValue
103
    {
104
        return $this->collectComposerNamespace;
105
    }
106
107
    /**
108
     * @param array<string> $collections
109
     */
110
    private function fillFromArray(array $collections, Closure $fetchValue): DbKeyValue
111
    {
112
        $db = new DbKeyValue();
113
        foreach ($collections as $key => $value) {
114
            $checkValue = $fetchValue($key, $value);
115
            $pos = \strpos($checkValue, self::NAMESPACE_PROJECT);
116
117
            if ($pos === false) {
118
                $db->add((string)$key, (string)$value);
119
            }
120
        }
121
122
        return $db;
123
    }
124
}
125