Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61fd043

Browse files
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 1d50688 commit 61fd043

File tree

7 files changed

+196
-6
lines changed

7 files changed

+196
-6
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines 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
}
@@ -167,9 +203,18 @@ public static function register($mode = 0)
167203
return $b['count'] - $a['count'];
168204
};
169205

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

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
eval(<<<'EOPHP'
4+
namespace PHPUnit\Util;
5+
6+
class Test
7+
{
8+
public static function getGroups()
9+
{
10+
return array();
11+
}
12+
}
13+
EOPHP
14+
);
15+
16+
@trigger_error('root deprecation', E_USER_DEPRECATED);
17+
18+
class FooTestCase
19+
{
20+
public function testLegacyFoo()
21+
{
22+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
23+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
24+
}
25+
26+
public function testNonLegacyBar()
27+
{
28+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
29+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
30+
}
31+
}
32+
33+
$foo = new FooTestCase();
34+
$foo->testLegacyFoo();
35+
$foo->testNonLegacyBar();
Lines 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';
Lines 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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"just here": "for the detection"}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
eval(<<<'EOPHP'
22+
namespace PHPUnit\Util;
23+
24+
class Test
25+
{
26+
public static function getGroups()
27+
{
28+
return array();
29+
}
30+
}
31+
EOPHP
32+
);
33+
34+
class FooTestCase
35+
{
36+
public function testLegacyFoo()
37+
{
38+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
39+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
40+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
41+
}
42+
43+
public function testNonLegacyBar()
44+
{
45+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
46+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
47+
}
48+
}
49+
50+
$foo = new FooTestCase();
51+
$foo->testLegacyFoo();
52+
$foo->testNonLegacyBar();
53+
54+
?>
55+
--EXPECTF--
56+
Unsilenced deprecation notices (3)
57+
58+
unsilenced foo deprecation: 2x
59+
2x in FooTestCase::testLegacyFoo
60+
61+
unsilenced bar deprecation: 1x
62+
1x in FooTestCase::testNonLegacyBar
63+
64+
Remaining deprecation notices (1)
65+
66+
silenced bar deprecation: 1x
67+
1x in FooTestCase::testNonLegacyBar
68+
69+
Legacy deprecation notices (1)
70+
71+
Other deprecation notices (1)
72+
73+
root deprecation: 1x
74+
Lines 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)