Completed
Push — master ( c8e170...6b0b35 )
by Nikola
01:10
created

Extension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 0
dl 0
loc 57
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A empty() 0 10 2
validate() 0 1 ?
A from() 0 4 1
A fromString() 0 4 1
A getIdentifiers() 0 4 1
A toString() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version\Extension;
6
7
use Version\Exception\InvalidVersion;
8
9
abstract class Extension
10
{
11
    protected const IDENTIFIERS_SEPARATOR = '.';
12
13
    /** @var array */
14
    private $identifiers;
15
16 38
    protected function __construct(string ...$identifiers)
17
    {
18 38
        foreach ($identifiers as $identifier) {
19 38
            $this->validate($identifier);
20
        }
21
22 34
        $this->identifiers = $identifiers;
23 34
    }
24
25 85
    public static function empty()
26
    {
27 85
        static $noExtension = null;
28
29 85
        if (null === $noExtension) {
30
            $noExtension = new static(...[]);
31
        }
32
33 85
        return $noExtension;
34
    }
35
36
    /**
37
     * @throws InvalidVersion
38
     */
39
    abstract protected function validate(string $identifier): void;
40
41 8
    public static function from(string $identifier, string ...$identifiers)
42
    {
43 8
        return new static($identifier, ...$identifiers);
44
    }
45
46 30
    public static function fromString(string $extension)
47
    {
48 30
        return new static(...explode(self::IDENTIFIERS_SEPARATOR, $extension));
49
    }
50
51 58
    public function getIdentifiers(): array
52
    {
53 58
        return $this->identifiers;
54
    }
55
56 9
    public function toString(): string
57
    {
58 9
        return implode(self::IDENTIFIERS_SEPARATOR, $this->identifiers);
59
    }
60
61
    public function __toString(): string
62
    {
63
        return $this->toString();
64
    }
65
}
66