Passed
Pull Request — master (#632)
by Théo
02:05
created

Configuration::getPath()   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
    /**
27
     * @var non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
28
     */
29
    private ?string $path;
30
31
    /**
32
     * @var non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
33
     */
34
    private ?string $outputDir;
35
36
    /**
37
     * @var non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
38
     */
39
    private string $prefix;
40
41
    private array $filesWithContents;
42
    private array $excludedFilesWithContents;
43
    private Patcher $patcher;
44
    private SymbolsConfiguration $symbolsConfiguration;
45
46
    /**
47
     * @param non-empty-string|null                $path                      Absolute canonical path to the configuration file loaded.
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
48
     * @param non-empty-string|null                $outputDir                 Absolute canonical path to the output directory.
49
     * @param non-empty-string                     $prefix                    The prefix applied.
50
     * @param array<string, array{string, string}> $filesWithContents         Array of tuple with the
51
     *                                            first argument being the file path and the second
52
     *                                            its contents
53
     * @param array<string, array{string, string}> $excludedFilesWithContents Array of tuple
54
     *                                            with the first argument being the file path and
55
     *                                            the second its contents
56
     */
57
    public function __construct(
58
        ?string $path,
59
        ?string $outputDir,
60
        string $prefix,
61
        array $filesWithContents,
62
        array $excludedFilesWithContents,
63
        Patcher $patcher,
64
        SymbolsConfiguration $symbolsConfiguration
65
    ) {
66
        self::validatePrefix($prefix);
67
68
        $this->path = $path;
69
        $this->outputDir = $outputDir;
70
        $this->prefix = $prefix;
71
        $this->filesWithContents = $filesWithContents;
72
        $this->excludedFilesWithContents = $excludedFilesWithContents;
73
        $this->patcher = $patcher;
74
        $this->symbolsConfiguration = $symbolsConfiguration;
75
    }
76
77
    /**
78
     * @return non-empty-string|null Absolute canonical path
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
79
     */
80
    public function getPath(): ?string
81
    {
82
        return $this->path;
83
    }
84
85
    /**
86
     * @return non-empty-string|null Absolute canonical path
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
87
     */
88
    public function getOutputDir(): ?string
89
    {
90
        return $this->outputDir;
91
    }
92
93
    /**
94
     * @param non-empty-string $prefix
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
95
     */
96
    public function withPrefix(string $prefix): self
97
    {
98
        return new self(
99
            $this->path,
100
            $this->outputDir,
101
            $prefix,
102
            $this->filesWithContents,
103
            $this->excludedFilesWithContents,
104
            $this->patcher,
105
            $this->symbolsConfiguration,
106
        );
107
    }
108
109
    /**
110
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
111
     */
112
    public function getPrefix(): string
113
    {
114
        return $this->prefix;
115
    }
116
117
    /**
118
     * @return array<string, array{string, string}>
119
     */
120
    public function getFilesWithContents(): array
121
    {
122
        return $this->filesWithContents;
123
    }
124
125
    /**
126
     * @return array<string, array{string, string}>
127
     */
128
    public function getExcludedFilesWithContents(): array
129
    {
130
        return $this->excludedFilesWithContents;
131
    }
132
133
    public function getPatcher(): Patcher
134
    {
135
        return $this->patcher;
136
    }
137
138
    public function getSymbolsConfiguration(): SymbolsConfiguration
139
    {
140
        return $this->symbolsConfiguration;
141
    }
142
143
    private static function validatePrefix(string $prefix): void
144
    {
145
        if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
146
            throw new InvalidArgumentException(
147
                sprintf(
148
                    'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"',
149
                    $prefix,
150
                ),
151
            );
152
        }
153
154
        if (preg_match('/\\\{2,}/', $prefix)) {
155
            throw new InvalidArgumentException(
156
                sprintf(
157
                    'Invalid namespace separator sequence. Got "%s"',
158
                    $prefix,
159
                ),
160
            );
161
        }
162
    }
163
}
164