Skip to content

Commit

Permalink
Fix the dumping of the whitelisted classes
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Oct 11, 2018
1 parent 03a9219 commit cd0787c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 32 deletions.
33 changes: 25 additions & 8 deletions src/Autoload/ScoperAutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Humbug\PhpScoper\Autoload;

use function array_column;
use Humbug\PhpScoper\Whitelist;
use PhpParser\Node\Name\FullyQualified;
use const PHP_EOL;
Expand All @@ -39,8 +40,22 @@ public function dump(string $prefix): string

$hasNamespacedFunctions = $this->hasNamespacedFunctions($whitelistedFunctions);

$statements = implode(PHP_EOL, $this->createClassAliasStatements($prefix, $hasNamespacedFunctions)).PHP_EOL.PHP_EOL;
$statements .= implode(PHP_EOL, $this->createFunctionAliasStatements($whitelistedFunctions, $hasNamespacedFunctions));
$statements = implode(
PHP_EOL,
$this->createClassAliasStatements(
$this->whitelist->getRecordedWhitelistedClasses(),
$hasNamespacedFunctions)
)
.PHP_EOL
.PHP_EOL
;
$statements .= implode(
PHP_EOL,
$this->createFunctionAliasStatements(
$whitelistedFunctions,
$hasNamespacedFunctions
)
);

if ($hasNamespacedFunctions) {
$dump = <<<PHP
Expand Down Expand Up @@ -82,17 +97,19 @@ public function dump(string $prefix): string
/**
* @return string[]
*/
private function createClassAliasStatements(string $prefix, bool $hasNamespacedFunctions): array
private function createClassAliasStatements(array $whitelistedClasses, bool $hasNamespacedFunctions): array
{
$statements = array_map(
function (string $whitelistedElement) use ($prefix): string {
function (string $prefixedClass): string {
return sprintf(
'class_exists(\'%s\%s\');',
$prefix,
$whitelistedElement
'class_exists(\'%s\');',
$prefixedClass
);
},
$this->whitelist->getClassWhitelistArray()
array_column(
$whitelistedClasses,
1
)
);

if ([] === $statements) {
Expand Down
107 changes: 83 additions & 24 deletions tests/Autoload/ScoperAutoloadGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_generate_the_autoload(Whitelist $whitelist, string $expecte

public function provideWhitelists()
{
yield [
yield 'empty whitelist' => [
Whitelist::create(true, true, true),
<<<'PHP'
<?php
Expand All @@ -50,26 +50,7 @@ public function provideWhitelists()
PHP
];

yield [
Whitelist::create(true, true, true, 'A\Foo', 'B\Bar'),
<<<'PHP'
<?php
// scoper-autoload.php @generated by PhpScoper
$loader = require_once __DIR__.'/autoload.php';
// Aliases for the whitelisted classes. For more information see:
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting
class_exists('Humbug\A\Foo');
class_exists('Humbug\B\Bar');
return $loader;

PHP
];

yield [
yield 'whitelist with whitelisted global functions recorded' => [
(function () {
$whitelist = Whitelist::create(true, true, true);

Expand Down Expand Up @@ -110,7 +91,7 @@ function bar() {
PHP
];

yield [
yield 'whitelist with whitelisted namespaced functions recorded' => [
(function () {
$whitelist = Whitelist::create(true, true, true);

Expand Down Expand Up @@ -171,10 +152,89 @@ function baz() {
PHP
];

yield [
yield 'whitelist with whitelisted classes but none recorded' => [
Whitelist::create(true, true, true, 'A\Foo', 'B\Bar'),
<<<'PHP'
<?php
// scoper-autoload.php @generated by PhpScoper
$loader = require_once __DIR__.'/autoload.php';
return $loader;

PHP
];

yield 'whitelist with whitelisted classes recorded' => [
(function () {
$whitelist = Whitelist::create(true, true, true, 'A\Foo', 'B\Bar');

$whitelist->recordWhitelistedClass(
new FullyQualified('A\Foo'),
new FullyQualified('Humbug\A\Foo')
);

return $whitelist;
})(),
<<<'PHP'
<?php
// scoper-autoload.php @generated by PhpScoper
$loader = require_once __DIR__.'/autoload.php';
// Aliases for the whitelisted classes. For more information see:
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting
class_exists('Humbug\A\Foo');
return $loader;

PHP
];

yield 'whitelist with whitelisted global classes recorded' => [
(function () {
$whitelist = Whitelist::create(true, true, true);

$whitelist->recordWhitelistedClass(
new FullyQualified('Foo'),
new FullyQualified('Humbug\Foo')
);

$whitelist->recordWhitelistedClass(
new FullyQualified('Bar'),
new FullyQualified('Humbug\Bar')
);

return $whitelist;
})(),
<<<'PHP'
<?php
// scoper-autoload.php @generated by PhpScoper
$loader = require_once __DIR__.'/autoload.php';
// Aliases for the whitelisted classes. For more information see:
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting
class_exists('Humbug\Foo');
class_exists('Humbug\Bar');
return $loader;

PHP
];

yield 'complete whitelist' => [
(function () {
$whitelist = Whitelist::create(true, true, true, 'A\Foo', 'B\Bar');

$whitelist->recordWhitelistedClass(
new FullyQualified('A\Foo'),
new FullyQualified('Humbug\A\Foo')
);

$whitelist->recordWhitelistedFunction(
new FullyQualified('foo'),
new FullyQualified('Humbug\foo')
Expand Down Expand Up @@ -215,7 +275,6 @@ function baz() {
// https://github.com/humbug/php-scoper/blob/master/README.md#class-whitelisting
namespace {
class_exists('Humbug\A\Foo');
class_exists('Humbug\B\Bar');
}
// Functions whitelisting. For more information see:
Expand Down

0 comments on commit cd0787c

Please sign in to comment.