Passed
Push — main ( 8d1023...74092d )
by Chema
53s
created

AliasRegistry::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Container;
6
7
/**
8
 * Manages service name aliases with resolution caching.
9
 * Allows accessing the same service with multiple identifiers.
10
 */
11
final class AliasRegistry
12
{
13
    /** @var array<string,string> */
14
    private array $aliases = [];
15
16
    /** @var array<string,string> */
17
    private array $resolvedCache = [];
18
19
    /**
20
     * Create an alias for a service.
21
     */
22 8
    public function add(string $alias, string $id): void
23
    {
24 8
        $this->aliases[$alias] = $id;
25
        // Clear cached resolutions when aliases change
26 8
        $this->resolvedCache = [];
27
    }
28
29
    /**
30
     * Resolve an alias to its actual service ID with caching.
31
     * Returns the input if no alias exists.
32
     */
33 59
    public function resolve(string $id): string
34
    {
35 59
        if (isset($this->resolvedCache[$id])) {
36 54
            return $this->resolvedCache[$id];
37
        }
38
39 59
        $resolved = $this->aliases[$id] ?? $id;
40 59
        $this->resolvedCache[$id] = $resolved;
41
42 59
        return $resolved;
43
    }
44
45
    /**
46
     * Check if an alias exists.
47
     */
48 2
    public function has(string $alias): bool
49
    {
50 2
        return isset($this->aliases[$alias]);
51
    }
52
}
53