Skip to content

Commit 3b58345

Browse files
authored
Also check for interfaces and traits in existence checks (#417)
1 parent e3b2a18 commit 3b58345

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

fixtures/set011/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"bin/greet.php"
44
],
55
"autoload": {
6+
"classmap": ["polyfill"],
67
"psr-4": {
78
"Set011\\": "src/"
89
}

fixtures/set011/polyfill/Iterator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* Let's polyfill the iterator interface.
5+
*
6+
* We use it as example to alias an interface into the root namespace.
7+
* It will never get loaded for real as it exists on all PHP versions.
8+
*
9+
* @link https://php.net/manual/en/class.iterator.php.
10+
* @link https://github.com/humbug/php-scoper/issues/403
11+
*/
12+
interface Iterator extends Traversable {
13+
public function current();
14+
public function next(): void;
15+
public function key();
16+
public function valid(): bool;
17+
public function rewind(): void;
18+
}

src/Autoload/ScoperAutoloadGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ static function (array $pair): string {
113113

114114
return sprintf(
115115
<<<'PHP'
116-
if (!class_exists('%s', false)) {
117-
class_exists('%s');
116+
if (!class_exists('%1$s', false) && !interface_exists('%1$s', false) && !trait_exists('%1$s', false)) {
117+
spl_autoload_call('%2$s');
118118
}
119119
PHP
120120
,

tests/Autoload/ScoperAutoloadGeneratorTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ function baz() {
187187
188188
// Aliases for the whitelisted classes. For more information see:
189189
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting
190-
if (!class_exists('A\Foo', false)) {
191-
class_exists('Humbug\A\Foo');
190+
if (!class_exists('A\Foo', false) && !interface_exists('A\Foo', false) && !trait_exists('A\Foo', false)) {
191+
spl_autoload_call('Humbug\A\Foo');
192192
}
193193
194194
return $loader;
@@ -221,11 +221,11 @@ class_exists('Humbug\A\Foo');
221221
222222
// Aliases for the whitelisted classes. For more information see:
223223
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting
224-
if (!class_exists('Foo', false)) {
225-
class_exists('Humbug\Foo');
224+
if (!class_exists('Foo', false) && !interface_exists('Foo', false) && !trait_exists('Foo', false)) {
225+
spl_autoload_call('Humbug\Foo');
226226
}
227-
if (!class_exists('Bar', false)) {
228-
class_exists('Humbug\Bar');
227+
if (!class_exists('Bar', false) && !interface_exists('Bar', false) && !trait_exists('Bar', false)) {
228+
spl_autoload_call('Humbug\Bar');
229229
}
230230
231231
return $loader;
@@ -281,8 +281,8 @@ class_exists('Humbug\Bar');
281281
// Aliases for the whitelisted classes. For more information see:
282282
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting
283283
namespace {
284-
if (!class_exists('A\Foo', false)) {
285-
class_exists('Humbug\A\Foo');
284+
if (!class_exists('A\Foo', false) && !interface_exists('A\Foo', false) && !trait_exists('A\Foo', false)) {
285+
spl_autoload_call('Humbug\A\Foo');
286286
}
287287
}
288288

0 commit comments

Comments
 (0)