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

AliasRegistry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 40
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 10 2
A has() 0 3 1
A add() 0 5 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