Skip to content

Commit ef74ccf

Browse files
committed
[PhpunitBridge] Fix deprecation type detection
When using several vendor directories
1 parent 0bc6184 commit ef74ccf

File tree

8 files changed

+167
-5
lines changed

8 files changed

+167
-5
lines changed

DeprecationErrorHandler/Deprecation.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ private static function getVendors()
264264
if (file_exists($v.'/composer/installed.json')) {
265265
self::$vendors[] = $v;
266266
$loader = require $v.'/autoload.php';
267-
$paths = self::getSourcePathsFromPrefixes(array_merge($loader->getPrefixes(), $loader->getPrefixesPsr4()));
267+
$paths = self::addSourcePathsFromPrefixes(
268+
array_merge($loader->getPrefixes(), $loader->getPrefixesPsr4()),
269+
$paths
270+
);
268271
}
269272
}
270273
}
@@ -280,15 +283,17 @@ private static function getVendors()
280283
return self::$vendors;
281284
}
282285

283-
private static function getSourcePathsFromPrefixes(array $prefixesByNamespace)
286+
private static function addSourcePathsFromPrefixes(array $prefixesByNamespace, array $paths)
284287
{
285288
foreach ($prefixesByNamespace as $prefixes) {
286289
foreach ($prefixes as $prefix) {
287290
if (false !== realpath($prefix)) {
288-
yield realpath($prefix);
291+
$paths[] = realpath($prefix);
289292
}
290293
}
291294
}
295+
296+
return $paths;
292297
}
293298

294299
/**
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+

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)