Passed
Pull Request — master (#565)
by Théo
02:31
created

NamespaceRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Humbug\PhpScoper\Symbol;
6
7
use function array_pop;
8
use function array_unique;
9
use function explode;
10
use function implode;
11
use function ltrim;
12
use function Safe\preg_match;
13
use function Safe\substr;
14
use function strpos;
15
use function strtolower;
16
17
final class NamespaceRegistry
18
{
19
    /**
20
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type Humbug\PhpScoper\Symbol\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
     */
22
    private array $namespaceRegexes;
23
24
    /**
25
     * @var list<string>
26
     */
27
    private array $namespaceNames;
28
29
    /**
30
     * @param string[] $namespaceRegexes
31
     * @param string[] $namespaceNames
32
     */
33
    public static function create(
34
        array $namespaceRegexes = [],
35
        array $namespaceNames = []
36
    ): self {
37
        return new self(
38
            array_unique($namespaceRegexes),
39
            array_unique($namespaceNames),
40
        );
41
    }
42
43
    /**
44
     * @param list<string> $namespaceRegexes
45
     * @param list<string> $namespaceNames
46
     */
47
    public function __construct(
48
        array $namespaceRegexes,
49
        array $namespaceNames
50
    ) {
51
        $this->namespaceRegexes = $namespaceRegexes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespaceRegexes of type array is incompatible with the declared type Humbug\PhpScoper\Symbol\list of property $namespaceRegexes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        $this->namespaceNames = $namespaceNames;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespaceNames of type array is incompatible with the declared type Humbug\PhpScoper\Symbol\list of property $namespaceNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
    }
54
55
    public function belongsToRegisteredNamespace(string $name): bool
56
    {
57
        return $this->isRegisteredNamespace(
58
            self::extractNameNamespace($name),
59
        );
60
    }
61
62
    public function isRegisteredNamespace(string $name): bool
63
    {
64
        $name = strtolower(ltrim($name, '\\'));
65
66
        foreach ($this->namespaceNames as $excludedNamespaceName) {
67
            if ('' === $excludedNamespaceName) {
68
                return true;
69
            }
70
71
            if (0 !== strpos($name, $excludedNamespaceName)) {
72
                continue;
73
            }
74
75
            $nameParts = explode('\\', $name);
76
77
            foreach (explode('\\', $excludedNamespaceName) as $index => $excludedNamespacePart) {
78
                if ($nameParts[$index] !== $excludedNamespacePart) {
79
                    return false;
80
                }
81
            }
82
83
            return true;
84
        }
85
86
        foreach ($this->namespaceRegexes as $excludedNamespace) {
87
            if (preg_match($excludedNamespace, $name)) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    private static function extractNameNamespace(string $name): string
96
    {
97
        $name = strtolower($name);
98
99
        if (0 === strpos($name, '\\')) {
100
            $name = substr($name, 1);
101
        }
102
103
        $nameParts = explode('\\', $name);
104
105
        array_pop($nameParts);
106
107
        return [] === $nameParts ? '' : implode('\\', $nameParts);
108
    }
109
}
110