8000 [PHPUnitBridge] Improved deprecations display · symfony/symfony@c55a89e · GitHub
[go: up one dir, main page]

Skip to content

Commit c55a89e

Browse files
greg0irefabpot
authored andcommitted
[PHPUnitBridge] Improved deprecations display
1 parent f0fbdee commit c55a89e

File tree

10 files changed

+517
-166
lines changed

10 files changed

+517
-166
lines changed

src/Symfony/Bridge/PhpUnit/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* ignore verbosity settings when the build fails because of deprecations
8+
* added per-group verbosity
9+
410
5.0.0
511
-----
612

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Util\ErrorHandler;
1616
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
1717
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
18+
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\DeprecationGroup;
1819
use Symfony\Component\ErrorHandler\DebugClassLoader;
1920

2021
/**
@@ -30,24 +31,20 @@ class DeprecationErrorHandler
3031

3132
private $mode;
3233
private $configuration;
33-
private $deprecations = [
34-
'unsilencedCount' => 0,
35-
'remaining selfCount' => 0,
36-
'legacyCount' => 0,
37-
'otherCount' => 0,
38-
'remaining directCount' => 0,
39-
'remaining indirectCount' => 0,
40-
'unsilenced' => [],
41-
'remaining self' => [],
42-
'legacy' => [],
43-
'other' => [],
44-
'remaining direct' => [],
45-
'remaining indirect' => [],
46-
];
34+
35+
/**
36+
* @var DeprecationGroup[]
37+
*/
38+
private $deprecationGroups = [];
4739

4840
private static $isRegistered = false;
4941
private static $isAtLeastPhpUnit83;
5042

43+
public function __construct()
44+
{
45+
$this->resetDeprecationGroups();
46+
}
47+
5148
/**
5249
* Registers and configures the deprecation handler.
5350
*
@@ -135,9 +132,9 @@ public function handleError($type, $msg, $file, $line, $context = [])
135132
$group = 'legacy';
136133
} else {
137134
$group = [
138-
Deprecation::TYPE_SELF => 'remaining self',
139-
Deprecation::TYPE_DIRECT => 'remaining direct',
140-
Deprecation::TYPE_INDIRECT => 'remaining indirect',
135+
Deprecation::TYPE_SELF => 'self',
136+
Deprecation::TYPE_DIRECT => 'direct',
137+
Deprecation::TYPE_INDIRECT => 'indirect',
141138
Deprecation::TYPE_UNDETERMINED => 'other',
142139
][$deprecation->getType()];
143140
}
@@ -148,18 +145,14 @@ public function handleError($type, $msg, $file, $line, $context = [])
148145
exit(1);
149146
}
150147
if ('legacy' !== $group) {
151-
$ref = &$this->deprecations[$group][$msg]['count'];
152-
++$ref;
153-
$ref = &$this->deprecations[$group][$msg][$class.'::'.$method];
154-
++$ref;
148+
$this->deprecationGroups[$group]->addNoticeFromObject($msg, $class, $method);
149+
} else {
150+
$this->deprecationGroups[$group]->addNotice();
155151
}
156152
} else {
157-
$ref = &$this->deprecations[$group][$msg]['count'];
158-
++$ref;
153+
$this->deprecationGroups[$group]->addNoticeFromProceduralCode($msg);
159154
}
160155

161-
++$this->deprecations[$group.'Count'];
162-
163156
return null;
164157
}
165158

@@ -184,34 +177,44 @@ public function shutdown()
184177
echo "\n", self::colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
185178
}
186179

187-
$groups = ['unsilenced', 'remaining self', 'remaining direct', 'remaining indirect', 'legacy', 'other'];
188-
189-
$this->displayDeprecations($groups, $configuration);
180+
$groups = array_keys($this->deprecationGroups);
190181

191182
// store failing status
192-
$isFailing = !$configuration->tolerates($this->deprecations);
183+
$isFailing = !$configuration->tolerates($this->deprecationGroups);
193184

194-
// reset deprecations array
195-
foreach ($this->deprecations as $group => $arrayOrInt) {
196-
$this->deprecations[$group] = \is_int($arrayOrInt) ? 0 : [];
197-
}
185+
$this->displayDeprecations($groups, $configuration, $isFailing);
186+
187+
$this->resetDeprecationGroups();
198188

199189
register_shutdown_function(function () use ($isFailing, $groups, $configuration) {
200-
foreach ($this->deprecations as $group => $arrayOrInt) {
201-
if (0 < (\is_int($arrayOrInt) ? $arrayOrInt : \count($arrayOrInt))) {
190+
foreach ($this->deprecationGroups as $group) {
191+
if ($group->count() > 0) {
202192
echo "Shutdown-time deprecations:\n";
203193
break;
204194
}
205195
}
206196

207-
$this->displayDeprecations($groups, $configuration);
197+
$isFailingAtShutdown = !$configuration->tolerates($this->deprecationGroups);
198+
$this->displayDeprecations($groups, $configuration, $isFailingAtShutdown);
208199

209-
if ($isFailing || !$configuration->tolerates($this->deprecations)) {
200+
if ($isFailing || $isFailingAtShutdown) {
210201
exit(1);
211202
}
212203
});
213204
}
214205

206+
private function resetDeprecationGroups()
207+
{
208+
$this->deprecationGroups = [
209+
'unsilenced' => new DeprecationGroup(),
210+
'self' => new DeprecationGroup(),
211+
'direct' => new DeprecationGroup(),
212+
'indirect' => new DeprecationGroup(),
213+
'legacy' => new DeprecationGroup(),
214+
'other' => new DeprecationGroup(),
215+
];
216+
}
217+
215218
private function getConfiguration()
216219
{
217220
if (null !== $this->configuration) {
@@ -270,31 +273,38 @@ private static function colorize($str, $red)
270273
/**
271274
* @param string[] $groups
272275
* @param Configuration $configuration
276+
* @param bool $isFailing
273277
*/
274-
private function displayDeprecations($groups, $configuration)
278+
private function displayDeprecations($groups, $configuration, $isFailing)< E377 /div>
275279
{
276280
$cmp = function ($a, $b) {
277-
return $b['count'] - $a['count'];
281+
return $b->count() - $a->count();
278282
};
279283

280284
foreach ($groups as $group) {
281-
if ($this->deprecations[$group.'Count']) {
285+
if ($this->deprecationGroups[$group]->count()) {
282286
echo "\n", self::colorize(
283-
sprintf('%s deprecation notices (%d)', ucfirst($group), $this->deprecations[$group.'Count']),
284-
'legacy' !== $group && 'remaining indirect' !== $group
287+
sprintf(
288+
'%s deprecation notices (%d)',
289+
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
290+
$this->deprecationGroups[$group]->count()
291+
),
292+
'legacy' !== $group && 'indirect' !== $group
285293
), "\n";
286294

287-
if (!$configuration->verboseOutput()) {
295+
if ('legacy' !== $group && !$configuration->verboseOutput($group) && !$isFailing) {
288296
continue;
289297
}
290-
uasort($this->deprecations[$group], $cmp);
298+
$notices = $this->deprecationGroups[$group]->notices();
299+
uasort($notices, $cmp);
291300

292-
foreach ($this->deprecations[$group] as $msg => $notices) {
293-
echo "\n ", $notices['count'], 'x: ', $msg, "\n";
301+
foreach ($notices as $msg => $notice) {
302+
echo "\n ", $notice->count(), 'x: ', $msg, "\n";
294303

295-
arsort($notices);
304+
$countsByCaller = $notice->getCountsByCaller();
305+
arsort($countsByCaller);
296306

297-
foreach ($notices as $method => $count) {
307+
foreach ($countsByCaller as $method => $count) {
298308
if ('count' !== $method) {
299309
echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
300310
}

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Configuration.php

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ class Configuration
3232
private $enabled = true;
3333

3434
/**
35-
* @var bool
35+
* @var bool[]
3636
*/
37-
private $verboseOutput = true;
37+
private $verboseOutput;
3838

3939
/**
4040
* @param int[] $thresholds A hash associating groups to thresholds
4141
* @param string $regex Will be matched against messages, to decide
4242
* whether to display a stack trace
43-
* @param bool $verboseOutput
43+
* @param bool[] $verboseOutput Keyed by groups
4444
*/
45-
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = true)
45+
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = [])
4646
{
4747
$groups = ['total', 'indirect', 'direct', 'self'];
4848

@@ -72,7 +72,21 @@ private function __construct(array $thresholds = [], $regex = '', $verboseOutput
7272
}
7373
}
7474
$this->regex = $regex;
75-
$this->verboseOutput = $verboseOutput;
75+
76+
$this->verboseOutput = [
77+
'unsilenced' => true,
78+
'direct' => true,
79+
'indirect' => true,
80+
'self' => true,
81+
'other' => true,
82+
];
83+
84+
foreach ($verboseOutput as $group => $status) {
85+
if (!isset($this->verboseOutput[$group])) {
86+
throw new \InvalidArgumentException(sprintf('Unsupported verbosity group "%s", expected one of "%s"', $group, implode('", "', array_keys($this->verboseOutput))));
87+
}
88+
$this->verboseOutput[$group] = (bool) $status;
89+
}
7690
}
7791

7892
/**
@@ -84,24 +98,26 @@ public function isEnabled()
8498
}
8599

86100
/**
87-
* @param mixed[] $deprecations
101+
* @param DeprecationGroup[] $deprecationGroups
88102
*
89103
* @return bool
90104
*/
91-
public function tolerates(array $deprecations)
105+
public function tolerates(array $deprecationGroups)
92106
{
93-
$deprecationCounts = [];
94-
foreach ($deprecations as $key => $deprecation) {
95-
if (false !== strpos($key, 'Count') && false === strpos($key, 'legacy')) {
96-
$deprecationCounts[$key] = $deprecation;
107+
$grandTotal = 0;
108+
109+
foreach ($deprecationGroups as $name => $group) {
110+
if ('legacy' !== $name) {
111+
$grandTotal += $group->count();
97112
}
98113
}
99114

100-
if (array_sum($deprecationCounts) > $this->thresholds['total']) {
115+
if ($grandTotal > $this->thresholds['total']) {
101116
return false;
102117
}
118+
103119
foreach (['self', 'direct', 'indirect'] as $deprecationType) {
104-
if ($deprecationCounts['remaining '.$deprecationType.'Count'] > $this->thresholds[$deprecationType]) {
120+
if ($deprecationGroups[$deprecationType]->count() > $this->thresholds[$deprecationType]) {
105121
return false;
106122
}
107123
}
@@ -130,9 +146,9 @@ public function isInRegexMode()
130146
/**
131147
* @return bool
132148
*/
133-
public function verboseOutput()
149+
public function verboseOutput($group)
134150
{
135-
return $this->verboseOutput;
151+
return $this->verboseOutput[$group];
136152
}
137153

138154
/**
@@ -145,7 +161,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
145161
{
146162
parse_str($serializedConfiguration, $normalizedConfiguration);
147163
foreach (array_keys($normalizedConfiguration) as $key) {
148-
if (!\in_array($key, ['max', 'disabled', 'verbose'], true)) {
164+
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet'], true)) {
149165
throw new \InvalidArgumentException(sprintf('Unknown configuration option "%s"', $key));
150166
}
151167
}
@@ -154,9 +170,19 @@ public static function fromUrlEncodedString($serializedConfiguration)
154170
return self::inDisabledMode();
155171
}
156172

157-
$verboseOutput = true;
158-
if (isset($normalizedConfiguration['verbose'])) {
159-
$verboseOutput = (bool) $normalizedConfiguration['verbose'];
173+
$verboseOutput = [];
174+
if (!isset($normalizedConfiguration['verbose'])) {
175+
$normalizedConfiguration['verbose'] = true;
176+
}
177+
178+
foreach (['unsilenced', 'direct', 'indirect', 'self', 'other'] as $group) {
179+
$verboseOutput[$group] = (bool) $normalizedConfiguration['verbose'];
180+
}
181+
182+
if (isset($normalizedConfiguration['quiet']) && \is_array($normalizedConfiguration['quiet'])) {
183+
foreach ($normalizedConfiguration['quiet'] as $shushedGroup) {
184+
$verboseOutput[$shushedGroup] = false;
185+
}
160186
}
161187

162188
return new self(
@@ -190,7 +216,12 @@ public static function inStrictMode()
190216
*/
191217
public static function inWeakMode()
192218
{
193-
return new self([], '', false);
219+
$verboseOutput = [];
220+
foreach (['unsilenced', 'direct', 'indirect', 'self', 'other'] as $group) {
221+
$verboseOutput[$group] = false;
222+
}
223+
224+
return new self([], '', $verboseOutput);
194225
}
195226

196227
/**

0 commit comments

Comments
 (0)
0