Passed
Push — master ( 371c66...7106c5 )
by Théo
01:55
created

Configuration::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 getPrefix(): string
68
    {
69
        return $this->prefix;
70
    }
71
72
    /**
73
     * @return array<string, array{string, string}>
74
     */
75
    public function getFilesWithContents(): array
76
    {
77
        return $this->filesWithContents;
78
    }
79
80
    public function getPatcher(): Patcher
81
    {
82
        return $this->patcher;
83
    }
84
85
    public function getSymbolsConfiguration(): SymbolsConfiguration
86
    {
87
        return $this->symbolsConfiguration;
88
    }
89
90
    /**
91
     * @return array<string, array{string, string}>
92
     */
93
    public function getExcludedFilesWithContents(): array
94
    {
95
        return $this->excludedFilesWithContents;
96
    }
97
98
    private static function validatePrefix(string $prefix): void
99
    {
100
        if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
101
            throw new InvalidArgumentException(
102
                sprintf(
103
                    'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"',
104
                    $prefix,
105
                ),
106
            );
107
        }
108
109
        if (preg_match('/\\\{2,}/', $prefix)) {
110
            throw new InvalidArgumentException(
111
                sprintf(
112
                    'Invalid namespace separator sequence. Got "%s"',
113
                    $prefix,
114
                ),
115
            );
116
        }
117
    }
118
}
119