Configuration   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrefix() 0 3 1
A getFilesWithContents() 0 3 1
A __construct() 0 16 1
A getPath() 0 3 1
A withPrefix() 0 9 1
A validatePrefix() 0 16 3
A getExcludedFilesWithContents() 0 3 1
A getSymbolsConfiguration() 0 3 1
A getPatcher() 0 3 1
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
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...
33
     */
34
    private string $prefix;
35
36
    private array $filesWithContents;
37
    private array $excludedFilesWithContents;
38
    private Patcher $patcher;
39
    private SymbolsConfiguration $symbolsConfiguration;
40
41
    /**
42
     * @param non-empty-string|null $path   Absolute 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...
43
     * @param non-empty-string      $prefix The prefix applied.
44
     * @param array<string, array{string, string}> $filesWithContents Array of tuple with the
45
     *                                            first argument being the file path and the second
46
     *                                            its contents
47
     * @param array<string, array{string, string}> $excludedFilesWithContents Array of tuple
48
     *                                            with the first argument being the file path and
49
     *                                            the second its contents
50
     */
51
    public function __construct(
52
        ?string $path,
53
        string $prefix,
54
        array $filesWithContents,
55
        array $excludedFilesWithContents,
56
        Patcher $patcher,
57
        SymbolsConfiguration $symbolsConfiguration
58
    ) {
59
        self::validatePrefix($prefix);
60
61
        $this->path = $path;
62
        $this->prefix = $prefix;
63
        $this->filesWithContents = $filesWithContents;
64
        $this->excludedFilesWithContents = $excludedFilesWithContents;
65
        $this->patcher = $patcher;
66
        $this->symbolsConfiguration = $symbolsConfiguration;
67
    }
68
69
    /**
70
     * @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...
71
     */
72
    public function getPath(): ?string
73
    {
74
        return $this->path;
75
    }
76
77
    /**
78
     * @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...
79
     */
80
    public function withPrefix(string $prefix): self
81
    {
82
        return new self(
83
            $this->path,
84
            $prefix,
85
            $this->filesWithContents,
86
            $this->excludedFilesWithContents,
87
            $this->patcher,
88
            $this->symbolsConfiguration,
89
        );
90
    }
91
92
    /**
93
     * @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...
94
     */
95
    public function getPrefix(): string
96
    {
97
        return $this->prefix;
98
    }
99
100
    /**
101
     * @return array<string, array{string, string}>
102
     */
103
    public function getFilesWithContents(): array
104
    {
105
        return $this->filesWithContents;
106
    }
107
108
    /**
109
     * @return array<string, array{string, string}>
110
     */
111
    public function getExcludedFilesWithContents(): array
112
    {
113
        return $this->excludedFilesWithContents;
114
    }
115
116
    public function getPatcher(): Patcher
117
    {
118
        return $this->patcher;
119
    }
120
121
    public function getSymbolsConfiguration(): SymbolsConfiguration
122
    {
123
        return $this->symbolsConfiguration;
124
    }
125
126
    private static function validatePrefix(string $prefix): void
127
    {
128
        if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
129
            throw new InvalidArgumentException(
130
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

130
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
131
                    'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"',
132
                    $prefix,
133
                ),
134
            );
135
        }
136
137
        if (preg_match('/\\\{2,}/', $prefix)) {
138
            throw new InvalidArgumentException(
139
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

139
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
140
                    'Invalid namespace separator sequence. Got "%s"',
141
                    $prefix,
142
                ),
143
            );
144
        }
145
    }
146
}
147