Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit15cce3b

Browse 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 vendorsare not taken into account when deciding to exit with an error code. Inthis mode, deprecations coming from the vendors are segregated fromother deprecations.
1 parent71a0d05 commit15cce3b

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
constMODE_WEAK ='weak';
22+
constMODE_WEAK_VENDORS ='weak_vendors';
2223
constMODE_DISABLED ='disabled';
2324

2425
privatestatic$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 ($vendorsas$vendor) {
80+
if (0 ===strpos($path,$vendor) &&false !==strpbrk(substr($path,strlen($vendor),1),'/'.DIRECTORY_SEPARATOR)) {
81+
returntrue;
82+
}
83+
}
84+
85+
returnfalse;
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 ($groupsas$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

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+
publicfunctiontestLegacyFoo()
21+
{
22+
@trigger_error('silenced foo deprecation',E_USER_DEPRECATED);
23+
trigger_error('unsilenced foo deprecation',E_USER_DEPRECATED);
24+
}
25+
26+
publicfunctiontestNonLegacyBar()
27+
{
28+
@trigger_error('silenced bar deprecation',E_USER_DEPRECATED);
29+
trigger_error('unsilenced bar deprecation',E_USER_DEPRECATED);
30+
}
31+
}
32+
33+
$foo =newFooTestCase();
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+
requirePHPUNIT_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+
publicfunctiontestLegacyFoo()
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+
publicfunctiontestNonLegacyBar()
44+
{
45+
@trigger_error('silenced bar deprecation',E_USER_DEPRECATED);
46+
trigger_error('unsilenced bar deprecation',E_USER_DEPRECATED);
47+
}
48+
}
49+
50+
$foo =newFooTestCase();
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+
requirePHPUNIT_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 deprecationnotices (2)
22+
23+
Remaining vendor deprecationnotices (1)
24+
25+
Legacy deprecationnotices (1)
26+
27+
Other deprecationnotices (1)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp