Completed
Push — master ( 0446fa...04d0ad )
by Jaap
32s queued 11s
created

Source::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link https://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Configuration;
15
16
use ArrayAccess;
17
use BadMethodCallException;
18
use OutOfBoundsException;
19
use phpDocumentor\Dsn;
20
use phpDocumentor\Path;
21
use function array_map;
22
use function in_array;
23
use function ltrim;
24
use function rtrim;
25
use function sprintf;
26
use function strpos;
27
use function substr;
28
29
/** @implements ArrayAccess<string, Path[]|Dsn> */
30
final class Source implements ArrayAccess
31
{
32
    /** @var Dsn */
33
    private $dsn;
34
35
    /** @var Path[] */
36
    private $paths;
37
38
    /** @param Path[] $paths */
39
    public function __construct(Dsn $dsn, array $paths)
40
    {
41
        $this->dsn = $dsn;
42
        $this->paths = $paths;
43
    }
44
45
    public function withDsn(Dsn $dsn) : Source
46
    {
47
        $self = clone $this;
48
        $self->dsn = $dsn;
49
50
        return $self;
51
    }
52
53
    /** @param Path[] $paths */
54
    public function withPaths(array $paths) : Source
55
    {
56
        $self = clone $this;
57
        $self->paths = $paths;
58
59
        return $self;
60
    }
61
62
    public function dsn() : Dsn
63
    {
64
        return $this->dsn;
65
    }
66
67
    /** @return Path[] */
68
    public function paths() : array
69
    {
70
        return $this->paths;
71
    }
72
73
    /** @return string[] */
74
    public function globPatterns() : array
75
    {
76
        return array_map(
77
            function (Path $path) : string {
78
                return $this->pathToGlobPattern((string) $path);
79
            },
80
            $this->paths
81
        );
82
    }
83
84
    private function normalizePath(string $path) : string
85
    {
86
        if (strpos($path, '.') === 0) {
87
            $path = ltrim($path, '.');
88
        }
89
90
        if (strpos($path, '/') !== 0) {
91
            $path = '/' . $path;
92
        }
93
94
        return rtrim($path, '/');
95
    }
96
97
    private function pathToGlobPattern(string $path) : string
98
    {
99
        $path = $this->normalizePath($path);
100
101
        if (substr($path, -1) !== '*' && strpos($path, '.') === false) {
102
            $path .= '/**/*';
103
        }
104
105
        return $path;
106
    }
107
108
    /** @param string $offset */
109
    public function offsetExists($offset) : bool
110
    {
111
        return in_array($offset, ['dsn', 'paths']);
112
    }
113
114
    /**
115
     * @param string $offset
116
     *
117
     * @return Path[]|Dsn
118
     */
119
    public function offsetGet($offset)
120
    {
121
        switch ($offset) {
122
            case 'dsn':
123
                return $this->dsn;
124
            case 'paths':
125
                return $this->paths;
126
            default:
127
                throw new OutOfBoundsException(sprintf('Offset %s does not exist', $offset));
128
        }
129
    }
130
131
    /**
132
     * @param string $offset
133
     * @param mixed $value
134
     */
135
    public function offsetSet($offset, $value) : void
136
    {
137
        throw new BadMethodCallException('Cannot set offset of ' . self::class);
138
    }
139
140
    /** @param string $offset */
141
    public function offsetUnset($offset) : void
142
    {
143
        throw new BadMethodCallException('Cannot unset offset of ' . self::class);
144
    }
145
}
146