Passed
Push — master ( 034eb5...6e4694 )
by Théo
03:24 queued 01:46
created

Configuration::getInternalClasses()   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 2
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 InvalidArgumentException;
18
use function Safe\preg_match;
19
use function Safe\sprintf;
20
21
final class Configuration
22
{
23
    private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u';
24
25
    private ?string $path;
26
    private string $prefix;
27
    private array $filesWithContents;
28
    private array $whitelistedFilesWithContents;
29
    private array $patchers;
30
    private Whitelist $whitelist;
31
32
    /**
33
     * @param string|null $path                   Absolute path to the configuration file loaded.
34
     * @param string      $prefix                 The prefix applied.
35
     * @param array<string, array{string, string}> $filesWithContents Array of tuple with the
36
     *                                            first argument being the file path and the second
37
     *                                            its contents
38
     * @param array<string, array{string, string}> $whitelistedFilesWithContents Array of tuple
39
     *                                    with the first argument being the file path and the
40
     *                                    second its contents
41
     * @param callable[]  $patchers               List of closures which can alter the content of
42
     *                                            the files being scoped.
43
     * @param Whitelist   $whitelist              List of classes that will not be scoped.
44
     *                                            returning a boolean which if `true` means the
45
     *                                            class should be scoped
46
     *                                            (i.e. is ignored) or scoped otherwise.
47
     */
48
    public function __construct(
49
        ?string $path,
50
        string $prefix,
51
        array $filesWithContents,
52
        array $whitelistedFilesWithContents,
53
        array $patchers,
54
        Whitelist $whitelist
55
    ) {
56
        self::validatePrefix($prefix);
57
58
        $this->path = $path;
59
        $this->prefix = $prefix;
60
        $this->filesWithContents = $filesWithContents;
61
        $this->patchers = $patchers;
62
        $this->whitelist = $whitelist;
63
        $this->whitelistedFilesWithContents = $whitelistedFilesWithContents;
64
    }
65
66
    public function getPath(): ?string
67
    {
68
        return $this->path;
69
    }
70
71
    public function getPrefix(): string
72
    {
73
        return $this->prefix;
74
    }
75
76
    /**
77
     * @return array<string, array{string, string}>
78
     */
79 3
    public function getFilesWithContents(): array
80
    {
81 3
        return $this->filesWithContents;
82 1
    }
83
84 2
    /**
85
     * @return callable[]
86
     */
87
    public function getPatchers(): array
88
    {
89
        return $this->patchers;
90
    }
91
92
    public function getWhitelist(): Whitelist
93
    {
94 2
        return $this->whitelist;
95
    }
96
97
    /**
98
     * @return array<string, array{string, string}>
99
     */
100
    public function getWhitelistedFilesWithContents(): array
101
    {
102
        return $this->whitelistedFilesWithContents;
103
    }
104 2
105
    /**
106
     * @return string[]
107
     */
108
    public function getInternalClasses(): array
109
    {
110
        return [];
111
    }
112
113
    /**
114 2
     * @return string[]
115
     */
116 2
    public function getInternalFunctions(): array
117
    {
118
        return [];
119
    }
120
121
    /**
122
     * @return string[]
123
     */
124
    public function getInternalConstants(): array
125
    {
126 3
        return [];
127
    }
128 2
129
    private static function validatePrefix(string $prefix): void
130 2
    {
131
        if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
132 2
            throw new InvalidArgumentException(
133
                sprintf(
134 2
                    'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"',
135
                    $prefix,
136 2
                ),
137
            );
138 2
        }
139 2
140 2
        if (preg_match('/\\\{2,}/', $prefix)) {
141
            throw new InvalidArgumentException(
142 2
                sprintf(
143
                    'Invalid namespace separator sequence. Got "%s"',
144
                    $prefix,
145
                ),
146
            );
147
        }
148
    }
149
}
150