Skip to content

Commit

Permalink
[Refactor] Re-use namespace wrapper (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 7, 2022
1 parent cabec44 commit a547797
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/Autoload/ScoperAutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static function createClassAliasStatementsSection(
}

if ($hasNamespacedFunctions) {
$statements = self::wrapStatementsInNamespaceBlock($statements);
$statements = self::wrapStatementsInNamespaceBlock('', $statements);
}

array_unshift($statements, self::CLASS_ALIASES_DOC);
Expand Down Expand Up @@ -183,7 +183,7 @@ private static function createClassAliasStatement(
*
* @return list<string>
*/
private static function wrapStatementsInNamespaceBlock(array $statements): array
private static function wrapStatementsInNamespaceBlock(string $namespace, array $statements): array
{
$indent = str_repeat(' ', 4);
$indentLine = static fn (string $line) => $indent.$line;
Expand All @@ -200,7 +200,13 @@ static function (string $statement) use ($indentLine): string {
$statements,
);

array_unshift($statements, 'namespace {');
array_unshift(
$statements,
sprintf(
'namespace %s{',
'' === $namespace ? '' : $namespace.' ',
),
);
$statements[] = '}'.self::$eol;

return $statements;
Expand Down Expand Up @@ -258,17 +264,22 @@ private static function createFunctionAliasStatement(
$namespace = $originalFQ->slice(0, -1);
$functionName = null === $namespace ? $original : (string) $originalFQ->slice(1);

return sprintf(
<<<'PHP'
namespace %s{
if (!function_exists('%s')) { function %s(%s) { return \%s(...func_get_args()); } }
}
PHP,
null === $namespace ? '' : $namespace->toString().' ',
$original,
$functionName,
'__autoload' === $functionName ? '$className' : '',
$alias,
return implode(
self::$eol,
self::wrapStatementsInNamespaceBlock(
(string) $namespace,
[
sprintf(
<<<'PHP'
if (!function_exists('%s')) { function %s(%s) { return \%s(...func_get_args()); } }
PHP,
$original,
$functionName,
'__autoload' === $functionName ? '$className' : '',
$alias,
),
],
),
);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Autoload/ScoperAutoloadGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ public static function provideRegistry(): iterable
namespace Acme {
if (!function_exists('Acme\bar')) { function bar() { return \Humbug\Acme\bar(...func_get_args()); } }
}
namespace Acme {
if (!function_exists('Acme\foo')) { function foo() { return \Humbug\Acme\foo(...func_get_args()); } }
}
namespace Emca {
if (!function_exists('Emca\baz')) { function baz() { return \Humbug\Emca\baz(...func_get_args()); } }
}
Expand Down Expand Up @@ -166,9 +168,11 @@ public static function provideRegistry(): iterable
namespace Acme {
if (!function_exists('Acme\bar')) { function bar() { return \Humbug\Acme\bar(...func_get_args()); } }
}
namespace Acme {
if (!function_exists('Acme\foo')) { function foo() { return \Humbug\Acme\foo(...func_get_args()); } }
}
namespace Emca {
if (!function_exists('Emca\baz')) { function baz() { return \Humbug\Emca\baz(...func_get_args()); } }
}
Expand Down Expand Up @@ -272,15 +276,19 @@ public static function provideRegistry(): iterable
namespace Acme {
if (!function_exists('Acme\bar')) { function bar() { return \Humbug\Acme\bar(...func_get_args()); } }
}
namespace Acme {
if (!function_exists('Acme\foo')) { function foo() { return \Humbug\Acme\foo(...func_get_args()); } }
}
namespace Emca {
if (!function_exists('Emca\baz')) { function baz() { return \Humbug\Emca\baz(...func_get_args()); } }
}
namespace {
if (!function_exists('bar')) { function bar() { return \Humbug\bar(...func_get_args()); } }
}
namespace {
if (!function_exists('foo')) { function foo() { return \Humbug\foo(...func_get_args()); } }
}
Expand Down Expand Up @@ -341,6 +349,7 @@ public static function provideRegistry(): iterable
namespace Acme {
if (!function_exists('Acme\foo')) { function foo() { return \Humbug\Acme\foo(...func_get_args()); } }
}
namespace {
if (!function_exists('__autoload')) { function __autoload($className) { return \Humbug\__autoload(...func_get_args()); } }
}
Expand Down

0 comments on commit a547797

Please sign in to comment.