Skip to content

Commit

Permalink
Ensure the whitelist feature works well on interfaces (#205)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
theofidry authored May 13, 2018
1 parent be22eb9 commit d6400f3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ e2e_013: bin/php-scoper.phar
diff src/scoper.inc.php.tpl build/set013/scoper.inc.php

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

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

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

Expand Down
9 changes: 0 additions & 9 deletions fixtures/set011/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
* file that was distributed with this source code.
*/

use Isolated\Symfony\Component\Finder\Finder;

return [
'finders' => [
(new Finder())
->files()
->in(__DIR__)
->exclude('tests')
->notPath('scoper.inc.php')
],
'whitelist' => [
\Set011\Dictionary::class,
],
Expand Down
4 changes: 2 additions & 2 deletions fixtures/set011/src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Set011;

abstract class Dictionary
interface Dictionary
{
/**
* @return string[]
*/
abstract public function provideWords(): array;
public function provideWords(): array;
}
2 changes: 1 addition & 1 deletion fixtures/set011/src/DirectionaryLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function ($filePath): bool {
function (array $dictionaries, string $className): array {
$class = new ReflectionClass($className);

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

Expand Down
2 changes: 1 addition & 1 deletion fixtures/set011/tests/SalutationDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Set011;

final class SalutationDictionary extends Dictionary
final class SalutationDictionary implements Dictionary
{
/**
* @inheritdoc
Expand Down
30 changes: 20 additions & 10 deletions src/Autoload/ScoperAutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace Humbug\PhpScoper\Autoload;

use function array_map;

final class ScoperAutoloadGenerator
{
private $whitelist;
Expand All @@ -28,16 +30,7 @@ public function __construct(array $whitelist)

public function dump(string $prefix): string
{
$statements = array_map(
function (string $whitelist) use ($prefix): string {
return sprintf(
'class_exists(\'%s\%s\');',
$prefix,
$whitelist
);
},
$this->whitelist
);
$statements = $this->createStatements($prefix);

$statements = implode(PHP_EOL, $statements);

Expand All @@ -54,4 +47,21 @@ function (string $whitelist) use ($prefix): string {
PHP;
}

/**
* @return string[]
*/
public function createStatements(string $prefix): array
{
return array_map(
function (string $whitelistedElement) use ($prefix): string {
return sprintf(
'class_exists(\'%s\%s\');',
$prefix,
$whitelistedElement
);
},
$this->whitelist
);
}
}

0 comments on commit d6400f3

Please sign in to comment.