Skip to content

Commit

Permalink
Rename variables in AutoloadGenerator (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 7, 2022
1 parent 9da2d15 commit 37d4d5f
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions src/Autoload/ScoperAutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function count;
use function explode;
use function implode;
use function is_string;
use function sprintf;
use function str_repeat;
use function str_replace;
Expand Down Expand Up @@ -62,26 +63,24 @@ public function dump(): string
$this->registry->getRecordedFunctions(),
);

$hasNamespacedFunctions = self::hasNamespacedFunctions($exposedFunctions);
$wrapInNamespace = self::hasNamespacedFunctions($exposedFunctions);

$statements = implode(
self::$eol,
self::createClassAliasStatementsSection(
$statements = [
...self::createClassAliasStatementsSection(
$this->registry->getRecordedClasses(),
$hasNamespacedFunctions,
$wrapInNamespace,
),
)
.self::$eol
.self::$eol;
$statements .= implode(
self::$eol,
self::createFunctionAliasStatements(
self::$eol,
...self::createFunctionAliasStatements(
$exposedFunctions,
$hasNamespacedFunctions,
$wrapInNamespace,
),
);
];

if ($hasNamespacedFunctions) {
$statements = implode(self::$eol, $statements);

if ($wrapInNamespace) {
$dump = <<<PHP
<?php
Expand Down Expand Up @@ -140,7 +139,7 @@ private static function sortExposedFunctions(array $exposedFunctions): array
*/
private static function createClassAliasStatementsSection(
array $exposedClasses,
bool $hasNamespacedFunctions
bool $wrapInNamespace
): array {
$statements = self::createClassAliasStatements($exposedClasses);

Expand All @@ -150,7 +149,7 @@ private static function createClassAliasStatementsSection(

array_unshift($statements, self::EXPOSE_CLASS_DECLARATION);

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

Expand All @@ -167,7 +166,7 @@ private static function createClassAliasStatementsSection(
private static function createClassAliasStatements(array $exposedClasses): array
{
return array_map(
static fn (array $pair) => self::createClassAliasStatement(...$pair),
static fn (array $class) => self::createClassAliasStatement(...$class),
$exposedClasses,
);
}
Expand All @@ -184,12 +183,16 @@ private static function createClassAliasStatement(
}

/**
* @param list<string> $statements
* @param string|list<string> $statements
*
* @return list<string>
*/
private static function wrapStatementsInNamespaceBlock(string $namespace, array $statements): array
private static function wrapStatementsInNamespaceBlock(string $namespace, string|array $statements): array
{
if (is_string($statements)) {
$statements = explode(self::$eol, $statements);
}

$indent = str_repeat(' ', 4);
$indentLine = static fn (string $line) => $indent.$line;

Expand Down Expand Up @@ -224,13 +227,13 @@ static function (string $statement) use ($indentLine): string {
*/
private static function createFunctionAliasStatements(
array $exposedFunctions,
bool $hasNamespacedFunctions
bool $wrapInNamespace
): array {
$functionsGroupedByNamespace = self::groupFunctionsByNamespace($exposedFunctions);

$statements = array_map(
static fn (string $namespace) => self::createNamespacedFunctionAliasStatement(
$hasNamespacedFunctions,
$wrapInNamespace,
$namespace,
$functionsGroupedByNamespace[$namespace],
),
Expand All @@ -255,12 +258,13 @@ private static function groupFunctionsByNamespace(array $exposedFunctions): arra
{
$groupedFunctions = [];

foreach ($exposedFunctions as [$original, $alias]) {
$originalFQ = new FullyQualified($original);
foreach ($exposedFunctions as [$exposed, $prefix]) {
$originalFQ = new FullyQualified($exposed);

$namespace = $originalFQ->slice(0, -1);
$functionName = null === $namespace ? $original : (string) $originalFQ->slice(1);
$functionName = null === $namespace ? $exposed : (string) $originalFQ->slice(1);

$groupedFunctions[(string) $namespace][] = [$original, $functionName, $alias];
$groupedFunctions[(string) $namespace][] = [$exposed, $functionName, $prefix];
}

return $groupedFunctions;
Expand All @@ -270,7 +274,7 @@ private static function groupFunctionsByNamespace(array $exposedFunctions): arra
* @param list<array{string, string, string}> $functions
*/
private static function createNamespacedFunctionAliasStatement(
bool $hasNamespacedFunctions,
bool $wrapInNamespace,
string $namespace,
array $functions
): string {
Expand All @@ -279,7 +283,7 @@ private static function createNamespacedFunctionAliasStatement(
$functions,
);

if ($hasNamespacedFunctions) {
if ($wrapInNamespace) {
$statements = self::wrapStatementsInNamespaceBlock(
$namespace,
$statements,
Expand All @@ -290,18 +294,18 @@ private static function createNamespacedFunctionAliasStatement(
}

private static function createFunctionAliasStatement(
string $original,
string $exposed,
string $functionName,
string $alias
string $prefixed
): string {
return sprintf(
<<<'PHP'
if (!function_exists('%s')) { function %s(%s) { return \%s(...func_get_args()); } }
PHP,
$original,
$exposed,
$functionName,
'__autoload' === $functionName ? '$className' : '',
$alias,
$prefixed,
);
}

Expand Down

0 comments on commit 37d4d5f

Please sign in to comment.