Passed
Pull Request — master (#587)
by Théo
02:07
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 9
dl 0
loc 22
rs 9.9332
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Humbug\PhpScoper\Whitelist;
19
use InvalidArgumentException;
20
use function Safe\preg_match;
21
use function Safe\sprintf;
22
23
final class Configuration
24
{
25
    private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u';
26
27
    private ?string $path;
28
    private string $prefix;
29
    private array $filesWithContents;
30
    private array $whitelistedFilesWithContents;
31
    private Patcher $patcher;
32
    private SymbolsConfiguration $symbolsConfiguration;
33
34
    /**
35
     * @var string[]
36
     */
37
    private array $internalClasses;
38
39
    /**
40
     * @var string[]
41
     */
42
    private array $internalFunctions;
43
44
    /**
45
     * @var string[]
46
     */
47
    private array $internalConstants;
48
49
    /**
50
     * @param string|null                          $path                         Absolute path to the configuration file loaded.
51
     * @param string                               $prefix                       The prefix applied.
52
     * @param array<string, array{string, string}> $filesWithContents            Array of tuple with the
53
     *                                            first argument being the file path and the second
54
     *                                            its contents
55
     * @param array<string, array{string, string}> $whitelistedFilesWithContents Array of tuple
56
     *                                            with the first argument being the file path and
57
     *                                            the second its contents
58
     * @param SymbolsConfiguration                 $symbolsConfiguration
59
     * @param string[]                             $internalClasses
60
     * @param string[]                             $internalFunctions
61
     * @param string[]                             $internalConstants
62
     */
63
    public function __construct(
64
        ?string $path,
65
        string $prefix,
66
        array $filesWithContents,
67
        array $whitelistedFilesWithContents,
68
        Patcher $patcher,
69
        SymbolsConfiguration $symbolsConfiguration,
70
        array $internalClasses,
71
        array $internalFunctions,
72
        array $internalConstants
73
    ) {
74
        self::validatePrefix($prefix);
75
76
        $this->path = $path;
77
        $this->prefix = $prefix;
78
        $this->filesWithContents = $filesWithContents;
79
        $this->patcher = $patcher;
80
        $this->symbolsConfiguration = $symbolsConfiguration;
81
        $this->whitelistedFilesWithContents = $whitelistedFilesWithContents;
82
        $this->internalClasses = $internalClasses;
83
        $this->internalFunctions = $internalFunctions;
84
        $this->internalConstants = $internalConstants;
85
    }
86
87
    public function getPath(): ?string
88
    {
89
        return $this->path;
90
    }
91
92
    public function getPrefix(): string
93
    {
94
        return $this->prefix;
95
    }
96
97
    /**
98
     * @return array<string, array{string, string}>
99
     */
100
    public function getFilesWithContents(): array
101
    {
102
        return $this->filesWithContents;
103
    }
104
105
    public function getPatcher(): Patcher
106
    {
107
        return $this->patcher;
108
    }
109
110
    public function getSymbolsConfiguration(): SymbolsConfiguration
111
    {
112
        return $this->symbolsConfiguration;
113
    }
114
115
    /**
116
     * @return array<string, array{string, string}>
117
     */
118
    public function getWhitelistedFilesWithContents(): array
119
    {
120
        return $this->whitelistedFilesWithContents;
121
    }
122
123
    /**
124
     * @return string[]
125
     */
126
    public function getInternalClasses(): array
127
    {
128
        return $this->internalClasses;
129
    }
130
131
    /**
132
     * @return string[]
133
     */
134
    public function getInternalFunctions(): array
135
    {
136
        return $this->internalFunctions;
137
    }
138
139
    /**
140
     * @return string[]
141
     */
142
    public function getInternalConstants(): array
143
    {
144
        return $this->internalConstants;
145
    }
146
147
    private static function validatePrefix(string $prefix): void
148
    {
149
        if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) {
150
            throw new InvalidArgumentException(
151
                sprintf(
152
                    'The prefix needs to be composed solely of letters, digits and backslashes (as namespace separators). Got "%s"',
153
                    $prefix,
154
                ),
155
            );
156
        }
157
158
        if (preg_match('/\\\{2,}/', $prefix)) {
159
            throw new InvalidArgumentException(
160
                sprintf(
161
                    'Invalid namespace separator sequence. Got "%s"',
162
                    $prefix,
163
                ),
164
            );
165
        }
166
    }
167
}
168