Completed
Push — master ( c5181d...c8e170 )
by Nikola
01:19
created

Extension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 62
ccs 17
cts 22
cp 0.7727
rs 10
c 0
b 0
f 0

9 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 isEmpty() 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
    public function isEmpty(): bool
57
    {
58
        return empty($this->identifiers);
59
    }
60
61 9
    public function toString(): string
62
    {
63 9
        return implode(self::IDENTIFIERS_SEPARATOR, $this->identifiers);
64
    }
65
66
    public function __toString(): string
67
    {
68
        return $this->toString();
69
    }
70
}
71