Passed
Push — master ( f21ca5...e0a8ac )
by Théo
02:11
created

Configuration::getInternalConstants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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;
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 $whitelistedFilesWithContents;
30
    private Patcher $patcher;
31
    private Whitelist $whitelist;
32
33
    /**
34
     * @var string[]
35
     */
36
    private array $internalClasses;
37
38
    /**
39
     * @var string[]
40
     */
41
    private array $internalFunctions;
42
43
    /**
44
     * @var string[]
45
     */
46
    private array $internalConstants;
47
48
    /**
49
     * @param string|null $path                   Absolute path to the configuration file loaded.
50
     * @param string      $prefix                 The prefix applied.
51
     * @param array<string, array{string, string}> $filesWithContents Array of tuple with the
52
     *                                            first argument being the file path and the second
53
     *                                            its contents
54
     * @param array<string, array{string, string}> $whitelistedFilesWithContents Array of tuple
55
     *                                            with the first argument being the file path and
56
     *                                            the second its contents
57
     * @param Whitelist   $whitelist              List of classes that will not be scoped.
58
     *                                            returning a boolean which if `true` means the
59
     *                                            class should be scoped
60
     *                                            (i.e. is ignored) or scoped otherwise.
61
     * @param string[]    $internalClasses
62
     * @param string[]    $internalFunctions
63
     * @param string[]    $internalConstants
64
     */
65
    public function __construct(
66
        ?string $path,
67
        string $prefix,
68
        array $filesWithContents,
69
        array $whitelistedFilesWithContents,
70
        Patcher $patcher,
71
        Whitelist $whitelist,
72
        array $internalClasses,
73
        array $internalFunctions,
74
        array $internalConstants
75
    ) {
76
        self::validatePrefix($prefix);
77
78
        $this->path = $path;
79 3
        $this->prefix = $prefix;
80
        $this->filesWithContents = $filesWithContents;
81 3
        $this->patcher = $patcher;
82 1
        $this->whitelist = $whitelist;
83
        $this->whitelistedFilesWithContents = $whitelistedFilesWithContents;
84 2
        $this->internalClasses = $internalClasses;
85
        $this->internalFunctions = $internalFunctions;
86
        $this->internalConstants = $internalConstants;
87
    }
88
89
    public function getPath(): ?string
90
    {
91
        return $this->path;
92
    }
93
94 2
    public function getPrefix(): string
95
    {
96
        return $this->prefix;
97
    }
98
99
    /**
100
     * @return array<string, array{string, string}>
101
     */
102
    public function getFilesWithContents(): array
103
    {
104 2
        return $this->filesWithContents;
105
    }
106
107
    public function getPatcher(): Patcher
108
    {
109
        return $this->patcher;
110
    }
111
112
    public function getWhitelist(): Whitelist
113
    {
114 2
        return $this->whitelist;
115
    }
116 2
117
    /**
118
     * @return array<string, array{string, string}>
119
     */
120
    public function getWhitelistedFilesWithContents(): array
121
    {
122
        return $this->whitelistedFilesWithContents;
123
    }
124
125
    /**
126 3
     * @return string[]
127
     */
128 2
    public function getInternalClasses(): array
129
    {
130 2
        return $this->internalClasses;
131
    }
132 2
133
    /**
134 2
     * @return string[]
135
     */
136 2
    public function getInternalFunctions(): array
137
    {
138 2
        return $this->internalFunctions;
139 2
    }
140 2
141
    /**
142 2
     * @return string[]
143
     */
144
    public function getInternalConstants(): array
145
    {
146
        return $this->internalConstants;
147
    }
148
149
    private static function validatePrefix(string $prefix): void
150
    {
151
        if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
152
            throw new InvalidArgumentException(
153
                sprintf(
154
                    'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"',
155
                    $prefix,
156 2
                ),
157
            );
158
        }
159
160
        if (preg_match('/\\\{2,}/', $prefix)) {
161
            throw new InvalidArgumentException(
162
                sprintf(
163
                    'Invalid namespace separator sequence. Got "%s"',
164 2
                    $prefix,
165 2
                ),
166 2
            );
167 2
        }
168 2
    }
169
}
170