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 a8726df

Browse filesBrowse files
bug #38013 [PHPUnitBridge] Fix deprecation type detection when trigger_deprecation is used (l-vo)
This PR was merged into the 5.1 branch. Discussion ---------- [PHPUnitBridge] Fix deprecation type detection when trigger_deprecation is used | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | when trigger_deprecation is used, the deprecation types (self, direct, indirect) are not properly detected because the file where the deprecation come from is always `deprecation-contracts/functions.php`. This PR aims to fix that. Commits ------- fd39961 [PHPUnitBridge] Fix deprecation type detection when trigger_deprecation is used
2 parents a36c43b + fd39961 commit a8726df
Copy full SHA for a8726df

File tree

11 files changed

+230
-13
lines changed
Filter options

11 files changed

+230
-13
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ class Deprecation
5151
*/
5252
public function __construct($message, array $trace, $file)
5353
{
54+
if (isset($trace[2]['function']) && 'trigger_deprecation' === $trace[2]['function']) {
55+
$file = $trace[2]['file'];
56+
array_splice($trace, 1, 1);
57+
}
58+
5459
$this->trace = $trace;
5560
$this->message = $message;
56-
$i = \count($trace);
57-
while (1 < $i && $this->lineShouldBeSkipped($trace[--$i])) {
61+
$i = \count($this->trace);
62+
while (1 < $i && $this->lineShouldBeSkipped($this->trace[--$i])) {
5863
// No-op
5964
}
60-
$line = $trace[$i];
65+
$line = $this->trace[$i];
6166
$this->triggeringFile = $file;
6267
if (isset($line['object']) || isset($line['class'])) {
6368
set_error_handler(function () {});
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use acme\lib\SomeService;
6+
use foo\lib\SomeOtherService;
7+
8+
final class AppService
9+
{
10+
public function directDeprecationsTwoVendors()
11+
{
12+
$service1 = new SomeService();
13+
$service1->deprecatedApi();
14+
15+
$service2 = new SomeOtherService();
16+
$service2->deprecatedApi();
17+
}
18+
19+
public function selfDeprecation(bool $useContracts = false)
20+
{
21+
$args = [__FUNCTION__, __FUNCTION__];
22+
if ($useContracts) {
23+
trigger_deprecation('App', '3.0', sprintf('%s is deprecated, use %s_new instead.', ...$args));
24+
} else {
25+
@trigger_error(sprintf('Since App 3.0: %s is deprecated, use %s_new instead.', ...$args), E_USER_DEPRECATED);
26+
}
27+
}
28+
29+
public function directDeprecation(bool $useContracts = false)
30+
{
31+
$service = new SomeService();
32+
$service->deprecatedApi($useContracts);
33+
}
34+
35+
public function indirectDeprecation(bool $useContracts = false)
36+
{
37+
$service = new SomeService();
38+
$service->indirectDeprecatedApi($useContracts);
39+
}
40+
}
41+

‎src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/fake_vendor/acme/lib/SomeService.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/fake_vendor/acme/lib/SomeService.php
+14-5Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
namespace acme\lib;
44

5+
use bar\lib\AnotherService;
6+
57
class SomeService
68
{
7-
public function deprecatedApi()
9+
public function deprecatedApi(bool $useContracts = false)
10+
{
11+
$args = [__FUNCTION__, __FUNCTION__];
12+
if ($useContracts) {
13+
trigger_deprecation('acme/lib', '3.0', sprintf('%s is deprecated, use %s_new instead.', ...$args));
14+
} else {
15+
@trigger_error(sprintf('Since acme/lib 3.0: %s is deprecated, use %s_new instead.', ...$args), E_USER_DEPRECATED);
16+
}
17+
}
18+
19+
public function indirectDeprecatedApi(bool $useContracts = false)
820
{
9-
@trigger_error(
10-
__FUNCTION__.' is deprecated! You should stop relying on it!',
11-
E_USER_DEPRECATED
12-
);
21+
(new AnotherService())->deprecatedApi($useContracts);
1322
}
1423
}
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace bar\lib;
4+
5+
class AnotherService
6+
{
7+
public function deprecatedApi(bool $useContracts = false)
8+
{
9+
$args = [__FUNCTION__, __FUNCTION__];
10+
if ($useContracts) {
11+
trigger_deprecation('bar/lib', '3.0', sprintf('%s is deprecated, use %s_new instead.', ...$args));
12+
} else {
13+
@trigger_error(sprintf('Since bar/lib 3.0: %s is deprecated, use %s_new instead.', ...$args), E_USER_DEPRECATED);
14+
}
15+
}
16+
}

‎src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/fake_vendor/composer/autoload_real.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/fake_vendor/composer/autoload_real.php
+29-2Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,41 @@ public function getPrefixes()
99

1010
public function getPrefixesPsr4()
1111
{
12-
return [];
12+
return [
13+
'App\\Services\\' => [__DIR__.'/../../fake_app/'],
14+
'acme\\lib\\' => [__DIR__.'/../acme/lib/'],
15+
'bar\\lib\\' => [__DIR__.'/../bar/lib/'],
16+
];
17+
}
18+
19+
public function loadClass($className)
20+
{
21+
foreach ($this->getPrefixesPsr4() as $prefix => $baseDirs) {
22+
if (strpos($className, $prefix) !== 0) {
23+
continue;
24+
}
25+
26+
foreach ($baseDirs as $baseDir) {
27+
$file = str_replace([$prefix, '\\'], [$baseDir, '/'], $className.'.php');
28+
if (file_exists($file)) {
29+
require $file;
30+
}
31+
}
32+
}
1333
}
1434
}
1535

1636
class ComposerAutoloaderInitFake
1737
{
38+
private static $loader;
39+
1840
public static function getLoader()
1941
{
20-
return new ComposerLoaderFake();
42+
if (null === self::$loader) {
43+
self::$loader = new ComposerLoaderFake();
44+
spl_autoload_register([self::$loader, 'loadClass']);
45+
}
46+
47+
return self::$loader;
2148
}
2249
}

‎src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/lagging_vendor.phpt

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/lagging_vendor.phpt
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ require __DIR__.'/fake_vendor/acme/outdated-lib/outdated_file.php';
3535
--EXPECTF--
3636
Remaining indirect deprecation notices (1)
3737

38-
1x: deprecatedApi is deprecated! You should stop relying on it!
38+
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
3939
1x in SomeService::deprecatedApi from acme\lib

‎src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/partially_quiet.phpt

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/partially_quiet.phpt
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Remaining direct deprecation notices (1)
2828

2929
Remaining indirect deprecation notices (1)
3030

31-
1x: deprecatedApi is deprecated! You should stop relying on it!
31+
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
3232
1x in SomeService::deprecatedApi from acme\lib
3333

3434
Legacy deprecation notices (2)

‎src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/quiet_but_failing.phpt

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/quiet_but_failing.phpt
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ require __DIR__.'/fake_vendor/acme/outdated-lib/outdated_file.php';
3535
--EXPECTF--
3636
Remaining indirect deprecation notices (1)
3737

38-
1x: deprecatedApi is deprecated! You should stop relying on it!
38+
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
3939
1x in SomeService::deprecatedApi from acme\lib
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Test deprecation types with trigger_deprecation
3+
--FILE--
4+
<?php
5+
6+
$k = 'SYMFONY_DEPRECATIONS_HELPER';
7+
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'max[self]=0');
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+
eval(<<<'EOPHP'
21+
namespace PHPUnit\Util;
22+
23+
class Test
24+
{
25+
public static function getGroups()
26+
{
27+
return array();
28+
}
29+
}
30+
EOPHP
31+
);
32+
33+
require __DIR__.'/fake_vendor/autoload.php';
34+
35+
(new \App\Services\AppService())->selfDeprecation(true);
36+
(new \App\Services\AppService())->directDeprecation(true);
37+
(new \App\Services\AppService())->indirectDeprecation(true);
38+
trigger_deprecation('foo/bar', '2.0', 'func is deprecated, use new instead.');
39+
?>
40+
--EXPECTF--
41+
Remaining self deprecation notices (1)
42+
43+
1x: Since App 3.0: selfDeprecation is deprecated, use selfDeprecation_new instead.
44+
1x in AppService::selfDeprecation from App\Services
45+
46+
Remaining direct deprecation notices (1)
47+
48+
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
49+
1x in AppService::directDeprecation from App\Services
50+
51+
Remaining indirect deprecation notices (1)
52+
53+
1x: Since bar/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
54+
1x in AppService::indirectDeprecation from App\Services
55+
56+
Other deprecation notices (1)
57+
58+
1x: Since foo/bar 2.0: func is deprecated, use new instead.
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Test deprecation types with trigger_error
3+
--FILE--
4+
<?php
5+
6+
$k = 'SYMFONY_DEPRECATIONS_HELPER';
7+
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'max[self]=0');
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+
eval(<<<'EOPHP'
21+
namespace PHPUnit\Util;
22+
23+
class Test
24+
{
25+
public static function getGroups()
26+
{
27+
return array();
28+
}
29+
}
30+
EOPHP
31+
);
32+
33+
require __DIR__.'/fake_vendor/autoload.php';
34+
35+
(new \App\Services\AppService())->selfDeprecation();
36+
(new \App\Services\AppService())->directDeprecation();
37+
(new \App\Services\AppService())->indirectDeprecation();
38+
@trigger_error('Since foo/bar 2.0: func is deprecated, use new instead.', E_USER_DEPRECATED);
39+
?>
40+
--EXPECTF--
41+
Remaining self deprecation notices (1)
42+
43+
1x: Since App 3.0: selfDeprecation is deprecated, use selfDeprecation_new instead.
44+
1x in AppService::selfDeprecation from App\Services
45+
46+
Remaining direct deprecation notices (1)
47+
48+
1x: Since acme/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
49+
1x in AppService::directDeprecation from App\Services
50+
51+
Remaining indirect deprecation notices (1)
52+
53+
1x: Since bar/lib 3.0: deprecatedApi is deprecated, use deprecatedApi_new instead.
54+
1x in AppService::indirectDeprecation from App\Services
55+
56+
Other deprecation notices (1)
57+
58+
1x: Since foo/bar 2.0: func is deprecated, use new instead.

‎src/Symfony/Bridge/PhpUnit/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/composer.json
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@
4545
"name": "phpunit/phpunit",
4646
"url": "https://github.com/sebastianbergmann/phpunit"
4747
}
48+
},
49+
"require-dev": {
50+
"symfony/deprecation-contracts": "^2.1"
4851
}
4952
}

0 commit comments

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