TagReference::extractTagFromAlias()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di\Reference;
6
7
use Yiisoft\Definitions\Reference;
8
9
/**
10
 * TagReference is a helper class used to specify a reference to a tag.
11
 * For example, `TagReference::to('my-tag')` specifies a reference to all services that are tagged with `tag@my-tag`.
12
 */
13
final class TagReference
14
{
15
    private const PREFIX = 'tag@';
16
17 1
    private function __construct()
18
    {
19 1
    }
20
21 1
    public static function to(string $tag): Reference
22
    {
23 1
        return Reference::to(self::PREFIX . $tag);
24
    }
25
26 13
    public static function extractTagFromAlias(string $alias): string
27
    {
28 13
        if (!str_starts_with($alias, self::PREFIX)) {
29 2
            throw new \InvalidArgumentException(sprintf('Alias "%s" is not a tag alias.', $alias));
30
        }
31 11
        return substr($alias, 4);
32
    }
33
34 132
    public static function isTagAlias(string $id): bool
35
    {
36 132
        return str_starts_with($id, self::PREFIX);
37
    }
38
}
39