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 457d29b

Browse filesBrowse files
greg0ireGrégoire Paris
authored and
Grégoire Paris
committed
Introduce weak vendors mode
A new mode is introduced, in which deprecations coming from the vendors are not taken into account when deciding to exit with an error code. In this mode, deprecations coming from the vendors are segregated from other deprecations.
1 parent 71a0d05 commit 457d29b
Copy full SHA for 457d29b

File tree

Expand file treeCollapse file tree

7 files changed

+186
-6
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+186
-6
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
+51-6Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class DeprecationErrorHandler
2020
{
2121
const MODE_WEAK = 'weak';
22+
const MODE_WEAK_VENDORS = 'weak_vendors';
2223
const MODE_DISABLED = 'disabled';
2324

2425
private static $isRegistered = false;
@@ -28,6 +29,7 @@ class DeprecationErrorHandler
2829
*
2930
* The following reporting modes are supported:
3031
* - use "weak" to hide the deprecation report but keep a global count;
32+
* - use "weak_vendors" to act as "weak" but only for vendors;
3133
* - use "/some-regexp/" to stop the test suite whenever a deprecation
3234
* message matches the given regular expression;
3335
* - use a number to define the upper bound of allowed deprecations,
@@ -52,13 +54,37 @@ public static function register($mode = 0)
5254
if (false === $mode) {
5355
$mode = getenv('SYMFONY_DEPRECATIONS_HELPER');
5456
}
55-
if (DeprecationErrorHandler::MODE_WEAK !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
57+
if (DeprecationErrorHandler::MODE_WEAK !== $mode && DeprecationErrorHandler::MODE_WEAK_VENDORS !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
5658
$mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
5759
}
5860

5961
return $memoizedMode = $mode;
6062
};
6163

64+
$inVendors = function ($path) {
65+
/** @var string[] absolute paths to vendor directories */
66+
static $vendors;
67+
if (null === $vendors) {
68+
foreach (get_declared_classes() as $class) {
69+
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
70+
$r = new \ReflectionClass($class);
71+
$v = dirname(dirname($r->getFileName()));
72+
if (file_exists($v.'/composer/installed.json')) {
73+
$vendors[] = $v;
74+
}
75+
}
76+
}
77+
}
78+
$path = realpath($path) ?: $path;
79+
foreach ($vendors as $vendor) {
80+
if (0 === strpos($path, $vendor) && false !== strpbrk(substr($path, strlen($vendor), 1), '/'.DIRECTORY_SEPARATOR)) {
81+
return true;
82+
}
83+
}
84+
85+
return false;
86+
};
87+
6288
$deprecations = array(
6389
'unsilencedCount' => 0,
6490
'remainingCount' => 0,
@@ -69,7 +95,13 @@ public static function register($mode = 0)
6995
'legacy' => array(),
7096
'other' => array(),
7197
);
72-
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode, $UtilPrefix) {
98+
if (self::MODE_WEAK_VENDORS === $mode) {
99+
$deprecations += array(
100+
'remaining vendorCount' => 0,
101+
'remaining vendor' => array(),
102+
);
103+
}
104+
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode, $UtilPrefix, $inVendors) {
73105
$mode = $getMode();
74106
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || DeprecationErrorHandler::MODE_DISABLED === $mode) {
75107
$ErrorHandler = $UtilPrefix.'ErrorHandler';
@@ -80,6 +112,8 @@ public static function register($mode = 0)
80112
$trace = debug_backtrace(true);
81113
$group = 'other';
82114

115+
$isWeak = DeprecationErrorHandler::MODE_WEAK === $mode || (DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode && $isVendor = $inVendors($file));
116+
83117
$i = count($trace);
84118
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) {
85119
// No-op
@@ -99,6 +133,8 @@ public static function register($mode = 0)
99133
|| in_array('legacy', $Test::getGroups($class, $method), true)
100134
) {
101135
$group = 'legacy';
136+
} elseif (DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode && $isVendor) {
137+
$group = 'remaining vendor';
102138
} else {
103139
$group = 'remaining';
104140
}
@@ -117,13 +153,13 @@ public static function register($mode = 0)
117153

118154
exit(1);
119155
}
120-
if ('legacy' !== $group && DeprecationErrorHandler::MODE_WEAK !== $mode) {
156+
if ('legacy' !== $group && !$isWeak) {
121157
$ref = &$deprecations[$group][$msg]['count'];
122158
++$ref;
123159
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
124160
++$ref;
125161
}
126-
} elseif (DeprecationErrorHandler::MODE_WEAK !== $mode) {
162+
} elseif (!$isWeak) {
127163
$ref = &$deprecations[$group][$msg]['count'];
128164
++$ref;
129165
}
@@ -168,9 +204,18 @@ public static function register($mode = 0)
168204
return $b['count'] - $a['count'];
169205
};
170206

171-
foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
207+
$groups = array('unsilenced', 'remaining');
208+
if (DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode) {
209+
$groups[] = 'remaining vendor';
210+
}
211+
array_push($groups, 'legacy', 'other');
212+
213+
foreach ($groups as $group) {
172214
if ($deprecations[$group.'Count']) {
173-
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";
215+
echo "\n", $colorize(
216+
sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']),
217+
'legacy' !== $group && 'remaining vendor' !== $group
218+
), "\n";
174219

175220
uasort($deprecations[$group], $cmp);
176221

+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
@trigger_error('root deprecation', E_USER_DEPRECATED);
4+
5+
class PHPUnit_Util_Test
6+
{
7+
public static function getGroups()
8+
{
9+
return array();
10+
}
11+
}
12+
13+
class FooTestCase
14+
{
15+
public function testLegacyFoo()
16+
{
17+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
18+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
19+
}
20+
21+
public function testNonLegacyBar()
22+
{
23+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
24+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
25+
}
26+
}
27+
28+
$foo = new FooTestCase();
29+
$foo->testLegacyFoo();
30+
$foo->testNonLegacyBar();
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require_once __DIR__.'/composer/autoload_real.php';
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class ComposerAutoloaderInitFake
4+
{
5+
}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"just here": "for the detection"}
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in weak vendors mode on a non vendor file
3+
--FILE--
4+
<?php
5+
6+
putenv('SYMFONY_DEPRECATIONS_HELPER=weak_vendors');
7+
putenv('ANSICON');
8+
putenv('ConEmuANSI');
9+
putenv('TERM');
10+
11+
$vendor = __DIR__;
12+
while (!file_exists($vendor.'/vendor')) {
13+
$vendor = dirname($vendor);
14+
}
15+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
16+
require PHPUNIT_COMPOSER_INSTALL;
17+
require_once __DIR__.'/../../bootstrap.php';
18+
19+
@trigger_error('root deprecation', E_USER_DEPRECATED);
20+
21+
class PHPUnit_Util_Test
22+
{
23+
public static function getGroups()
24+
{
25+
return array();
26+
}
27+
}
28+
29+
class FooTestCase
30+
{
31+
public function testLegacyFoo()
32+
{
33+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
34+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
35+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
36+
}
37+
38+
public function testNonLegacyBar()
39+
{
40+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
41+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
42+
}
43+
}
44+
45+
$foo = new FooTestCase();
46+
$foo->testLegacyFoo();
47+
$foo->testNonLegacyBar();
48+
49+
?>
50+
--EXPECTF--
51+
Unsilenced deprecation notices (3)
52+
53+
unsilenced foo deprecation: 2x
54+
2x in FooTestCase::testLegacyFoo
55+
56+
unsilenced bar deprecation: 1x
57+
1x in FooTestCase::testNonLegacyBar
58+
59+
Remaining deprecation notices (1)
60+
61+
silenced bar deprecation: 1x
62+
1x in FooTestCase::testNonLegacyBar
63+
64+
Legacy deprecation notices (1)
65+
66+
Other deprecation notices (1)
67+
68+
root deprecation: 1x
69+
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in weak vendors mode on vendor file
3+
--FILE--
4+
<?php
5+
6+
putenv('SYMFONY_DEPRECATIONS_HELPER=weak_vendors');
7+
putenv('ANSICON');
8+
putenv('ConEmuANSI');
9+
putenv('TERM');
10+
11+
$vendor = __DIR__;
12+
while (!file_exists($vendor.'/vendor')) {
13+
$vendor = dirname($vendor);
14+
}
15+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
16+
require PHPUNIT_COMPOSER_INSTALL;
17+
require_once __DIR__.'/../../bootstrap.php';
18+
require __DIR__.'/fake_vendor/autoload.php';
19+
require __DIR__.'/fake_vendor/acme/lib/deprecation_riddled.php';
20+
--EXPECTF--
21+
Unsilenced deprecation notices (2)
22+
23+
Remaining vendor deprecation notices (1)
24+
25+
Legacy deprecation notices (1)
26+
27+
Other deprecation notices (1)

0 commit comments

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