Issues (163)

src/Symbol/NamespaceRegistry.php (5 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Symbol;
16
17
use function array_filter;
18
use function array_map;
19
use function array_pop;
20
use function array_unique;
21
use function count;
22
use function explode;
23
use function implode;
24
use function ltrim;
25
use function Safe\preg_match;
26
use function str_contains;
27
use function strtolower;
28
use function trim;
29
use const SORT_STRING;
30
31
final class NamespaceRegistry
32
{
33
    /**
34
     * @var list<string>
0 ignored issues
show
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...
35
     */
36
    private array $names;
37
38
    /**
39
     * @var list<string>
40
     */
41
    private array $regexes;
42
43
    private bool $containsGlobalNamespace;
44
45
    /**
46
     * @param string[] $namespaceNames
47
     * @param string[] $namespaceRegexes
48
     */
49
    public static function create(
50
        array $namespaceNames = [],
51
        array $namespaceRegexes = []
52
    ): self {
53
        return new self(
54
            array_unique(
55
                array_map(
56
                    static fn (string $namespaceName) => strtolower(trim($namespaceName, '\\')),
57
                    $namespaceNames,
58
                ),
59
                SORT_STRING,
60
            ),
61
            array_unique($namespaceRegexes, SORT_STRING),
62
        );
63
    }
64
65
    /**
66
     * @param list<string> $namespaceNames
67
     * @param list<string> $namespaceRegexes
68
     */
69
    private function __construct(
70
        array $namespaceNames,
71
        array $namespaceRegexes
72
    ) {
73
        $this->names = $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 $names.

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...
74
        $this->regexes = $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 $regexes.

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...
75
76
        $this->containsGlobalNamespace = count(
77
            array_filter(
78
                $namespaceNames,
79
                static fn (string $name) => '' === $name,
80
            ),
81
        ) !== 0;
82
    }
83
84
    public function belongsToRegisteredNamespace(string $symbolName): bool
85
    {
86
        return $this->isRegisteredNamespace(
87
            self::extractNameNamespace($symbolName),
88
        );
89
    }
90
91
    /**
92
     * Checks if the given namespace matches one of the registered namespace
93
     * names, is a sub-namespace of a registered namespace name or matches any
94
     * regex provided.
95
     */
96
    public function isRegisteredNamespace(string $namespaceName): bool
97
    {
98
        if ($this->containsGlobalNamespace) {
99
            return true;
100
        }
101
102
        $originalNamespaceName = ltrim($namespaceName, '\\');
103
        $normalizedNamespaceName = strtolower($originalNamespaceName);
104
105
        foreach ($this->names as $excludedNamespaceName) {
106
            if ('' === $excludedNamespaceName
107
                || str_contains($normalizedNamespaceName, $excludedNamespaceName)
108
            ) {
109
                return true;
110
            }
111
        }
112
113
        foreach ($this->regexes as $excludedNamespace) {
114
            if (preg_match($excludedNamespace, $originalNamespaceName)) {
115
                return true;
116
            }
117
        }
118
119
        return false;
120
    }
121
122
    /**
123
     * @internal
124
     *
125
     * @return list<string>
126
     */
127
    public function getNames(): array
128
    {
129
        return $this->names;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->names returns the type array which is incompatible with the documented return type Humbug\PhpScoper\Symbol\list.
Loading history...
130
    }
131
132
    /**
133
     * @internal
134
     *
135
     * @return list<string>
136
     */
137
    public function getRegexes(): array
138
    {
139
        return $this->regexes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->regexes returns the type array which is incompatible with the documented return type Humbug\PhpScoper\Symbol\list.
Loading history...
140
    }
141
142
    private static function extractNameNamespace(string $name): string
143
    {
144
        $nameParts = explode('\\', $name);
145
146
        array_pop($nameParts);
147
148
        return [] === $nameParts ? '' : implode('\\', $nameParts);
149
    }
150
}
151