Skip to content

Commit d6400f3

Browse files
authored
Ensure the whitelist feature works well on interfaces (#205)
Closes #188. The current mechanism works fine since the `class_exists()` is here only to trigger the autoload. Since the autoloading mechanism is the same for classes and interfaces, `class_exists()` works fine.
1 parent be22eb9 commit d6400f3

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ e2e_013: bin/php-scoper.phar
8888
diff src/scoper.inc.php.tpl build/set013/scoper.inc.php
8989

9090
.PHONY: e2e_014
91-
e2e_014: ## Run end-to-end tests for the fixture set 014: source code case with psr-0
91+
e2e_014: ## Run end-to-end tests for the fixture set 014: source code case with PSR-0
9292
e2e_014: bin/php-scoper.phar
9393
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set014
9494

9595
php build/set014/bin/greet.phar > build/set014/output
9696
diff fixtures/set014/expected-output build/set014/output
9797

9898
.PHONY: e2e_015
99-
e2e_015: ## Run end-to-end tests for the fixture set 015: third-party code case with psr-0
99+
e2e_015: ## Run end-to-end tests for the fixture set 015: third-party code case with PSR-0
100100
e2e_015: bin/php-scoper.phar fixtures/set015/vendor
101101
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set015
102102

fixtures/set011/scoper.inc.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@
1212
* file that was distributed with this source code.
1313
*/
1414

15-
use Isolated\Symfony\Component\Finder\Finder;
16-
1715
return [
18-
'finders' => [
19-
(new Finder())
20-
->files()
21-
->in(__DIR__)
22-
->exclude('tests')
23-
->notPath('scoper.inc.php')
24-
],
2516
'whitelist' => [
2617
\Set011\Dictionary::class,
2718
],

fixtures/set011/src/Dictionary.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Set011;
66

7-
abstract class Dictionary
7+
interface Dictionary
88
{
99
/**
1010
* @return string[]
1111
*/
12-
abstract public function provideWords(): array;
12+
public function provideWords(): array;
1313
}

fixtures/set011/src/DirectionaryLocator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function ($filePath): bool {
4848
function (array $dictionaries, string $className): array {
4949
$class = new ReflectionClass($className);
5050

51-
if (false === $class->isAbstract() && $class->isSubclassOf(Dictionary::class)) {
51+
if (false === $class->isAbstract() && $class->implementsInterface(Dictionary::class)) {
5252
$dictionaries[] = $class->newInstanceWithoutConstructor();
5353
}
5454

fixtures/set011/tests/SalutationDictionary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Set011;
66

7-
final class SalutationDictionary extends Dictionary
7+
final class SalutationDictionary implements Dictionary
88
{
99
/**
1010
* @inheritdoc

src/Autoload/ScoperAutoloadGenerator.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace Humbug\PhpScoper\Autoload;
1616

17+
use function array_map;
18+
1719
final class ScoperAutoloadGenerator
1820
{
1921
private $whitelist;
@@ -28,16 +30,7 @@ public function __construct(array $whitelist)
2830

2931
public function dump(string $prefix): string
3032
{
31-
$statements = array_map(
32-
function (string $whitelist) use ($prefix): string {
33-
return sprintf(
34-
'class_exists(\'%s\%s\');',
35-
$prefix,
36-
$whitelist
37-
);
38-
},
39-
$this->whitelist
40-
);
33+
$statements = $this->createStatements($prefix);
4134

4235
$statements = implode(PHP_EOL, $statements);
4336

@@ -54,4 +47,21 @@ function (string $whitelist) use ($prefix): string {
5447
5548
PHP;
5649
}
50+
51+
/**
52+
* @return string[]
53+
*/
54+
public function createStatements(string $prefix): array
55+
{
56+
return array_map(
57+
function (string $whitelistedElement) use ($prefix): string {
58+
return sprintf(
59+
'class_exists(\'%s\%s\');',
60+
$prefix,
61+
$whitelistedElement
62+
);
63+
},
64+
$this->whitelist
65+
);
66+
}
5767
}

0 commit comments

Comments
 (0)