Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3b50ce3

Browse filesBrowse files
committed
Add log file option for deprecations
1 parent c82c997 commit 3b50ce3
Copy full SHA for 3b50ce3

File tree

4 files changed

+119
-19
lines changed
Filter options

4 files changed

+119
-19
lines changed

‎src/Symfony/Bridge/PhpUnit/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* added `logFile` option to write deprecations to a file instead of echoing them
8+
49
5.1.0
510
-----
611

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
+25-11Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,23 +291,34 @@ private static function colorize($str, $red)
291291
* @param string[] $groups
292292
* @param Configuration $configuration
293293
* @param bool $isFailing
294+
*
295+
* @throws InvalidArgumentException
294296
*/
295297
private function displayDeprecations($groups, $configuration, $isFailing)
296298
{
297299
$cmp = function ($a, $b) {
298300
return $b->count() - $a->count();
299301
};
300302

303+
if ($configuration->shouldWriteToLogFile()) {
304+
if (false === $handle = @fopen($file = $configuration->getLogFile(), 'a')) {
305+
throw new \InvalidArgumentException(sprintf('The configured log file "%s" is not writeable.', $file));
306+
}
307+
} else {
308+
$handle = fopen('php://output', 'w');
309+
}
310+
301311
foreach ($groups as $group) {
302312
if ($this->deprecationGroups[$group]->count()) {
303-
echo "\n", self::colorize(
304-
sprintf(
305-
'%s deprecation notices (%d)',
306-
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
307-
$this->deprecationGroups[$group]->count()
308-
),
309-
'legacy' !== $group && 'indirect' !== $group
310-
), "\n";
313+
$deprecationGroupMessage = sprintf(
314+
'%s deprecation notices (%d)',
315+
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
316+
$this->deprecationGroups[$group]->count()
317+
);
318+
echo "\n", self::colorize($deprecationGroupMessage, 'legacy' !== $group && 'indirect' !== $group), "\n";
319+
if ($configuration->shouldWriteToLogFile()) {
320+
fwrite($handle, "\n$deprecationGroupMessage\n");
321+
}
311322

312323
if ('legacy' !== $group && !$configuration->verboseOutput($group) && !$isFailing) {
313324
continue;
@@ -316,22 +327,25 @@ private function displayDeprecations($groups, $configuration, $isFailing)
316327
uasort($notices, $cmp);
317328

318329
foreach ($notices as $msg => $notice) {
319-
echo "\n ", $notice->count(), 'x: ', $msg, "\n";
330+
fwrite($handle, sprintf("\n %sx: %s\n", $notice->count(), $msg));
320331

321332
$countsByCaller = $notice->getCountsByCaller();
322333
arsort($countsByCaller);
323334

324335
foreach ($countsByCaller as $method => $count) {
325336
if ('count' !== $method) {
326-
echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
337+
fwrite($handle, sprintf(" %dx in %s\n",
338+
$count,
339+
preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method))
340+
);
327341
}
328342
}
329343
}
330344
}
331345
}
332346

333347
if (!empty($notices)) {
334-
echo "\n";
348+
fwrite($handle, "\n");
335349
}
336350
}
337351

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Configuration.php
+28-8Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ class Configuration
5252
private $baselineDeprecations = [];
5353

5454
/**
55-
* @param int[] $thresholds A hash associating groups to thresholds
56-
* @param string $regex Will be matched against messages, to decide whether to display a stack trace
57-
* @param bool[] $verboseOutput Keyed by groups
58-
* @param bool $generateBaseline Whether to generate or update the baseline file
59-
* @param string $baselineFile The path to the baseline file
55+
* @var string|null
6056
*/
61-
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = [], $generateBaseline = false, $baselineFile = '')
57+
private $logFile = null;
58+
59+
/**
60+
* @param int[] $thresholds A hash associating groups to thresholds
61+
* @param string $regex Will be matched against messages, to decide whether to display a stack trace
62+
* @param bool[] $verboseOutput Keyed by groups
63+
* @param bool $generateBaseline Whether to generate or update the baseline file
64+
* @param string $baselineFile The path to the baseline file
65+
* @param string|null $logFile The path to the log file
66+
*/
67+
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = [], $generateBaseline = false, $baselineFile = '', $logFile = null)
6268
{
6369
$groups = ['total', 'indirect', 'direct', 'self'];
6470

@@ -119,6 +125,8 @@ private function __construct(array $thresholds = [], $regex = '', $verboseOutput
119125
throw new \InvalidArgumentException(sprintf('The baselineFile "%s" does not exist.', $this->baselineFile));
120126
}
121127
}
128+
129+
$this->logFile = $logFile;
122130
}
123131

124132
/**
@@ -238,6 +246,16 @@ public function verboseOutput($group)
238246
return $this->verboseOutput[$group];
239247
}
240248

249+
public function shouldWriteToLogFile()
250+
{
251+
return null !== $this->logFile;
252+
}
253+
254+
public function getLogFile()
255+
{
256+
return $this->logFile;
257+
}
258+
241259
/**
242260
* @param string $serializedConfiguration an encoded string, for instance
243261
* max[total]=1234&max[indirect]=42
@@ -248,7 +266,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
248266
{
249267
parse_str($serializedConfiguration, $normalizedConfiguration);
250268
foreach (array_keys($normalizedConfiguration) as $key) {
251-
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile'], true)) {
269+
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile', 'logFile'], true)) {
252270
throw new \InvalidArgumentException(sprintf('Unknown configuration option "%s".', $key));
253271
}
254272
}
@@ -260,6 +278,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
260278
'quiet' => [],
261279
'generateBaseline' => false,
262280
'baselineFile' => '',
281+
'logFile' => null,
263282
];
264283

265284
if ('' === $normalizedConfiguration['disabled'] || filter_var($normalizedConfiguration['disabled'], \FILTER_VALIDATE_BOOLEAN)) {
@@ -282,7 +301,8 @@ public static function fromUrlEncodedString($serializedConfiguration)
282301
'',
283302
$verboseOutput,
284303
filter_var($normalizedConfiguration['generateBaseline'], \FILTER_VALIDATE_BOOLEAN),
285-
$normalizedConfiguration['baselineFile']
304+
$normalizedConfiguration['baselineFile'],
305+
$normalizedConfiguration['logFile']
286306
);
287307
}
288308

+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
Test DeprecationErrorHandler with log file
3+
--FILE--
4+
<?php
5+
$filename = tempnam(sys_get_temp_dir(), 'sf-').uniqid();
6+
$k = 'SYMFONY_DEPRECATIONS_HELPER';
7+
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'logFile='.$filename);
8+
putenv('ANSICON');
9+
putenv('ConEmuANSI');
10+
putenv('TERM');
11+
12+
$vendor = __DIR__;
13+
while (!file_exists($vendor.'/vendor')) {
14+
$vendor = dirname($vendor);
15+
}
16+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
17+
require PHPUNIT_COMPOSER_INSTALL;
18+
require_once __DIR__.'/../../bootstrap.php';
19+
20+
class FooTestCase
21+
{
22+
public function testLegacyFoo()
23+
{
24+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
25+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
26+
}
27+
28+
public function testLegacyBar()
29+
{
30+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
31+
}
32+
}
33+
34+
@trigger_error('root deprecation', E_USER_DEPRECATED);
35+
36+
$foo = new FooTestCase();
37+
$foo->testLegacyFoo();
38+
$foo->testLegacyBar();
39+
40+
register_shutdown_function(function () use ($filename) {
41+
var_dump(file_get_contents($filename));
42+
});
43+
?>
44+
--EXPECTF--
45+
Unsilenced deprecation notices (3)
46+
47+
Other deprecation notices (1)
48+
string(234) "
49+
Unsilenced deprecation notices (3)
50+
51+
2x: unsilenced foo deprecation
52+
2x in FooTestCase::testLegacyFoo
53+
54+
1x: unsilenced bar deprecation
55+
1x in FooTestCase::testLegacyBar
56+
57+
Other deprecation notices (1)
58+
59+
1x: root deprecation
60+
61+
"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.