-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Configuration.php
211 lines (186 loc) · 5.25 KB
/
Configuration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
/**
* @internal
*/
class Configuration
{
/**
* @var int[]
*/
private $thresholds;
/**
* @var string
*/
private $regex;
/**
* @var bool
*/
private $enabled = true;
/**
* @var bool
*/
private $verboseOutput = true;
/**
* @param int[] $thresholds A hash associating groups to thresholds
* @param string $regex Will be matched against messages, to decide
* whether to display a stack trace
* @param bool $verboseOutput
*/
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = true)
{
$groups = ['total', 'indirect', 'direct', 'self'];
foreach ($thresholds as $group => $threshold) {
if (!\in_array($group, $groups, true)) {
throw new \InvalidArgumentException(sprintf('Unrecognized threshold "%s", expected one of "%s".', $group, implode('", "', $groups)));
}
if (!is_numeric($threshold)) {
throw new \InvalidArgumentException(sprintf('Threshold for group "%s" has invalid value "%s".', $group, $threshold));
}
$this->thresholds[$group] = (int) $threshold;
}
if (isset($this->thresholds['direct'])) {
$this->thresholds += [
'self' => $this->thresholds['direct'],
];
}
if (isset($this->thresholds['indirect'])) {
$this->thresholds += [
'direct' => $this->thresholds['indirect'],
'self' => $this->thresholds['indirect'],
];
}
foreach ($groups as $group) {
if (!isset($this->thresholds[$group])) {
$this->thresholds[$group] = 999999;
}
}
$this->regex = $regex;
$this->verboseOutput = $verboseOutput;
}
/**
* @return bool
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* @param mixed[] $deprecations
*
* @return bool
*/
public function tolerates(array $deprecations)
{
$deprecationCounts = [];
foreach ($deprecations as $key => $deprecation) {
if (false !== strpos($key, 'Count') && false === strpos($key, 'legacy')) {
$deprecationCounts[$key] = $deprecation;
}
}
if (array_sum($deprecationCounts) > $this->thresholds['total']) {
return false;
}
foreach (['self', 'direct', 'indirect'] as $deprecationType) {
if ($deprecationCounts['remaining '.$deprecationType.'Count'] > $this->thresholds[$deprecationType]) {
return false;
}
}
return true;
}
/**
* @param string $message
*
* @return bool
*/
public function shouldDisplayStackTrace($message)
{
return '' !== $this->regex && preg_match($this->regex, $message);
}
/**
* @return bool
*/
public function isInRegexMode()
{
return '' !== $this->regex;
}
/**
* @return bool
*/
public function verboseOutput()
{
return $this->verboseOutput;
}
/**
* @param string $serializedConfiguration an encoded string, for instance
* max[total]=1234&max[indirect]=42
*
* @return self
*/
public static function fromUrlEncodedString($serializedConfiguration)
{
parse_str($serializedConfiguration, $normalizedConfiguration);
foreach (array_keys($normalizedConfiguration) as $key) {
if (!\in_array($key, ['max', 'disabled', 'verbose'], true)) {
throw new \InvalidArgumentException(sprintf('Unknown configuration option "%s".', $key));
}
}
if (isset($normalizedConfiguration['disabled'])) {
return self::inDisabledMode();
}
$verboseOutput = true;
if (isset($normalizedConfiguration['verbose'])) {
$verboseOutput = (bool) $normalizedConfiguration['verbose'];
}
return new self(
isset($normalizedConfiguration['max']) ? $normalizedConfiguration['max'] : [],
'',
$verboseOutput
);
}
/**
* @return self
*/
public static function inDisabledMode()
{
$configuration = new self();
$configuration->enabled = false;
return $configuration;
}
/**
* @return self
*/
public static function inStrictMode()
{
return new self(['total' => 0]);
}
/**
* @return self
*/
public static function inWeakMode()
{
return new self([], '', false);
}
/**
* @return self
*/
public static function fromNumber($upperBound)
{
return new self(['total' => $upperBound]);
}
/**
* @return self
*/
public static function fromRegex($regex)
{
return new self([], $regex);
}
}