8000 bug #37959 [PhpunitBridge] Fix deprecation type detection (when sever… · symfony/symfony@52719f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52719f2

Browse files
committed
bug #37959 [PhpunitBridge] Fix deprecation type detection (when several autoload files are used) (l-vo)
This PR was merged into the 4.4 branch. Discussion ---------- [PhpunitBridge] Fix deprecation type detection (when several autoload files are used) | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Several autoload files are supported by the PHPUnit Bridge but when the internal paths are registered (for deprecation type detection), the paths (from prefixes) of the last autoload file override the paths previously registered. This PR fixes this bug. Commits ------- cc7b6c5 [PhpunitBridge] Fix deprecation type detection
2 parents ddede31 + cc7b6c5 commit 52719f2

File tree

8 files changed

+167
-5
lines changed

8 files changed

+167
-5
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ private static function getVendors()
266266
if (file_exists($v.'/composer/installed.json')) {
267267
self::$vendors[] = $v;
268268
$loader = require $v.'/autoload.php';
269-
$paths = self::getSourcePathsFromPrefixes(array_merge($loader->getPrefixes(), $loader->getPrefixesPsr4()));
269+
$paths = self::addSourcePathsFromPrefixes(
270+
array_merge($loader->getPrefixes(), $loader->getPrefixesPsr4()),
271+
$paths
272+
);
270273
}
271274
}
272275
}
@@ -282,15 +285,17 @@ private static function getVendors()
282285
return self::$vendors;
283286
}
284287

285-
private static function getSourcePathsFromPrefixes(array $prefixesByNamespace)
288+
private static function addSourcePathsFromPrefixes(array $prefixesByNamespace, array $paths)
286289
{
287290
foreach ($prefixesByNamespace as $prefixes) {
288291
foreach ($prefixes as $prefix) {
289292
if (false !== realpath($prefix)) {
290-
yield realpath($prefix);
293+
$paths[] = realpath($prefix);
291294
}
292295
}
293296
}
297+
298+
return $paths;
294299
}
295300

296301
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 directDeprecations()
11+
{
12+
$service1 = new SomeService();
13+
$service1->deprecatedApi();
14+
15+
$service2 = new SomeOtherService();
16+
$service2->deprecatedApi();
17+
}
18+
}
19+

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,40 @@ 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+
];
16+
}
17+
18+
public function loadClass($className)
19+
{
20+
foreach ($this->getPrefixesPsr4() as $prefix => $baseDirs) {
21+
if (strpos($className, $prefix) !== 0) {
22+
continue;
23+
}
24+
25+
foreach ($baseDirs as $baseDir) {
26+
$file = str_replace([$prefix, '\\'], [$baseDir, '/'], $className.'.php');
27+
if (file_exists($file)) {
28+
require $file;
29+
}
30+
}
31+
}
1332
}
1433
}
1534

1635
class ComposerAutoloaderInitFake
1736
{
37+
private static $loader;
38+
1839
public static function getLoader()
1940
{
20-
return new ComposerLoaderFake();
41+
if (null === self::$loader) {
42+
self::$loader = new ComposerLoaderFake();
43+
spl_autoload_register([self::$loader, 'loadClass']);
44+
}
45+
46+
return self::$loader;
2147
}
2248
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
require_once __DIR__.'/composer/autoload_real.php';
4+
5+
return ComposerAutoloaderInitFakeBis::getLoader();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
class ComposerLoaderFakeBis
4+
{
5+
public function getPrefixes()
6+
{
7+
return [];
8+
}
9+
10+
public function getPrefixesPsr4()
11+
{
12+
return [
13+
'foo\\lib\\' => [__DIR__.'/../foo/lib/'],
14+
];
15+
}
16+
17+
public function loadClass($className)
18+
{
19+
foreach ($this->getPrefixesPsr4() as $prefix => $baseDirs) {
20+
if (strpos($className, $prefix) !== 0) {
21+
continue;
22+
}
23+
24+
foreach ($baseDirs as $baseDir) {
25+
$file = str_replace([$prefix, '\\'], [$baseDir, '/'], $className.'.php');
26+
if (file_exists($file)) {
27+
require $file;
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
class ComposerAutoloaderInitFakeBis
35+
{
36+
private static $loader;
37+
38+
public static function getLoader()
39+
{
40+
if (null === self::$loader) {
41+
self::$loader = new ComposerLoaderFakeBis();
42+
spl_autoload_register([self::$loader, 'loadClass']);
43+
}
44+
45+
return self::$loader;
46+
}
47+
}
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace foo\lib;
4+
5+
class SomeOtherService
6+
{
7+
public function deprecatedApi()
8+
{
9+
@trigger_error(
10+
__FUNCTION__.' from foo is deprecated! You should stop relying on it!',
11+
E_USER_DEPRECATED
12+
);
13+
}
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
Test DeprecationErrorHandler with multiple autoload files
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+
require __DIR__.'/fake_vendor_bis/autoload.php';
35+
36+
(new \App\Services\AppService())->directDeprecations();
37+
?>
38+
--EXPECTF--
39+
Remaining direct deprecation notices (2)
40+
41+
1x: deprecatedApi is deprecated! You should stop relying on it!
42+
1x in AppService::directDeprecations from App\Services
43+
44+
1x: deprecatedApi from foo is deprecated! You should stop relying on it!
45+
1x in AppService::directDeprecations from App\Services

0 commit comments

Comments
 (0)
0