Passed
Pull Request — master (#599)
by Théo
02:07
created

SymbolRegistry::getNames()   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
2
3
declare(strict_types=1);
4
5
namespace Humbug\PhpScoper\Symbol;
6
7
use function array_key_exists;
8
use function array_keys;
9
use function array_map;
10
use function array_pop;
11
use function array_unique;
12
use function explode;
13
use function implode;
14
use function ltrim;
15
use function Safe\array_flip;
16
use function Safe\preg_match;
17
use function strtolower;
18
19
final class SymbolRegistry
20
{
21
    /**
22
     * @var array<string, mixed>
23
     */
24
    private array $names;
25
26
    /**
27
     * @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...
28
     */
29
    private array $regexes;
30
31
    private bool $constants;
32
33
    /**
34
     * @param string[] $names
35
     * @param string[] $regexes
36
     */
37
    public static function create(
38
        array $names = [],
39
        array $regexes = []
40
    ): self {
41
        return new self(
42
            array_unique(
43
                array_map('strtolower', $names),
44
            ),
45
            array_unique($regexes),
46
            false,
47
        );
48
    }
49
50
    /**
51
     * Unlike classes & functions, constants are not case-insensitive (although
52
     * the namespace part _is_). I.e. \Acme\FOO = \ACME\FOO but Acme\FOO ≠ Acme\Foo.
53
     *
54
     * @param string[] $names
55
     * @param string[] $regexes
56
     */
57
    public static function createForConstants(
58
        array $names = [],
59
        array $regexes = []
60
    ): self {
61
        return new self(
62
            array_unique(
63
                array_map(
64
                    static fn (string $name) => self::lowerCaseConstantName($name),
65
                    $names,
66
                ),
67
            ),
68
            array_unique($regexes),
69
            true,
70
        );
71
    }
72
73
    /**
74
     * @param list<string> $names
75
     * @param list<string> $regexes
76
     */
77
    private function __construct(
78
        array $names,
79
        array $regexes,
80
        bool $constants
81
    ) {
82
        $this->names = array_flip($names);
83
        $this->regexes = $regexes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $regexes 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...
84
        $this->constants = $constants;
85
    }
86
87
    public function matches(string $symbol): bool
88
    {
89
        $originalSymbol = ltrim($symbol, '\\');
90
        $symbol = $this->constants
91
            ? self::lowerCaseConstantName($originalSymbol)
92
            : strtolower($originalSymbol);
93
94
        if (array_key_exists($symbol, $this->names)) {
95
            return true;
96
        }
97
98
        foreach ($this->regexes as $regex) {
99
            if (preg_match($regex, $originalSymbol)) {
100
                return true;
101
            }
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * @internal
109
     *
110
     * @return list<string>
111
     */
112
    public function getNames(): array
113
    {
114
        return array_keys($this->names);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_keys($this->names) returns the type array which is incompatible with the documented return type Humbug\PhpScoper\Symbol\list.
Loading history...
115
    }
116
117
    /**
118
     * @internal
119
     *
120
     * @erturn list<string>
121
     */
122
    public function getRegexes(): array
123
    {
124
        return $this->regexes;
125
    }
126
127
    /**
128
     * Transforms the constant FQ name "Acme\Foo\X" to "acme\foo\X" since the
129
     * namespace remains case-insensitive for constants regardless of whether
130
     * constants actually are case-insensitive.
131
     */
132
    private static function lowerCaseConstantName(string $name): string
133
    {
134
        $parts = explode('\\', $name);
135
136
        $lastPart = array_pop($parts);
137
138
        $parts = array_map('strtolower', $parts);
139
140
        $parts[] = $lastPart;
141
142
        return implode('\\', $parts);
143
    }
144
}
145