Passed
Pull Request — master (#629)
by Théo
02:06
created

Configuration::withPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Configuration;
16
17
use Humbug\PhpScoper\Patcher\Patcher;
18
use InvalidArgumentException;
19
use function Safe\preg_match;
20
use function Safe\sprintf;
21
22
final class Configuration
23
{
24
    private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u';
25
26
    private ?string $path;
27
    private string $prefix;
28
    private array $filesWithContents;
29
    private array $excludedFilesWithContents;
30
    private Patcher $patcher;
31
    private SymbolsConfiguration $symbolsConfiguration;
32
33
    /**
34
     * @param string|null                          $path                      Absolute path to the configuration file loaded.
35
     * @param string                               $prefix                    The prefix applied.
36
     * @param array<string, array{string, string}> $filesWithContents         Array of tuple with the
37
     *                                            first argument being the file path and the second
38
     *                                            its contents
39
     * @param array<string, array{string, string}> $excludedFilesWithContents Array of tuple
40
     *                                            with the first argument being the file path and
41
     *                                            the second its contents
42
     * @param SymbolsConfiguration                 $symbolsConfiguration
43
     */
44
    public function __construct(
45
        ?string $path,
46
        string $prefix,
47
        array $filesWithContents,
48
        array $excludedFilesWithContents,
49
        Patcher $patcher,
50
        SymbolsConfiguration $symbolsConfiguration
51
    ) {
52
        self::validatePrefix($prefix);
53
54
        $this->path = $path;
55
        $this->prefix = $prefix;
56
        $this->filesWithContents = $filesWithContents;
57
        $this->patcher = $patcher;
58
        $this->symbolsConfiguration = $symbolsConfiguration;
59
        $this->excludedFilesWithContents = $excludedFilesWithContents;
60
    }
61
62
    public function getPath(): ?string
63
    {
64
        return $this->path;
65
    }
66
67
    public function withPrefix(string $prefix): self
68
    {
69
        return new self(
70
            $this->path,
71
            $prefix,
72
            $this->filesWithContents,
73
            $this->excludedFilesWithContents,
74
            $this->patcher,
75
            $this->symbolsConfiguration,
76
        );
77
    }
78
79
    public function getPrefix(): string
80
    {
81
        return $this->prefix;
82
    }
83
84
    /**
85
     * @return array<string, array{string, string}>
86
     */
87
    public function getFilesWithContents(): array
88
    {
89
        return $this->filesWithContents;
90
    }
91
92
    public function getPatcher(): Patcher
93
    {
94
        return $this->patcher;
95
    }
96
97
    public function getSymbolsConfiguration(): SymbolsConfiguration
98
    {
99
        return $this->symbolsConfiguration;
100
    }
101
102
    /**
103
     * @return array<string, array{string, string}>
104
     */
105
    public function getExcludedFilesWithContents(): array
106
    {
107
        return $this->excludedFilesWithContents;
108
    }
109
110
    private static function validatePrefix(string $prefix): void
111
    {
112
        if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
113
            throw new InvalidArgumentException(
114
                sprintf(
115
                    'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"',
116
                    $prefix,
117
                ),
118
            );
119
        }
120
121
        if (preg_match('/\\\{2,}/', $prefix)) {
122
            throw new InvalidArgumentException(
123
                sprintf(
124
                    'Invalid namespace separator sequence. Got "%s"',
125
                    $prefix,
126
                ),
127
            );
128
        }
129
    }
130
}
131