Skip to content

Commit

Permalink
Use the recorded symbols to decide when to add the class_alias statem…
Browse files Browse the repository at this point in the history
…ents (#724)

This makes the feature more reliable as avoid to have to keep synchronized the logic of when a symbol is aliased.
  • Loading branch information
theofidry authored Nov 7, 2022
1 parent 365bbf3 commit 4501e36
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
28 changes: 15 additions & 13 deletions src/PhpParser/NodeVisitor/ClassAliasStmtAppender.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
namespace Humbug\PhpScoper\PhpParser\NodeVisitor;

use Humbug\PhpScoper\PhpParser\Node\ClassAliasFuncCall;
use Humbug\PhpScoper\PhpParser\Node\FullyQualifiedFactory;
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
use Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario;
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -57,9 +56,8 @@
final class ClassAliasStmtAppender extends NodeVisitorAbstract
{
public function __construct(
private readonly string $prefix,
private readonly EnrichedReflector $enrichedReflector,
private readonly IdentifierResolver $identifierResolver,
private readonly SymbolsRegistry $symbolsRegistry,
) {
}

Expand Down Expand Up @@ -112,23 +110,27 @@ private function createNamespaceStmts(array $stmts, Stmt $stmt): array

$resolvedName = $this->identifierResolver->resolveIdentifier($name);

if ($resolvedName instanceof FullyQualified
&& $this->enrichedReflector->isExposedClass((string) $resolvedName)
) {
$stmts[] = self::createAliasStmt($resolvedName, $stmt, $this->prefix);
if (!($resolvedName instanceof FullyQualified)) {
return $stmts;
}

$record = $this->symbolsRegistry->getRecordedClass((string) $resolvedName);

if (null !== $record) {
$stmts[] = self::createAliasStmt($record[0], $record[1], $stmt);
}

return $stmts;
}

private static function createAliasStmt(
FullyQualified $originalName,
Node $stmt,
string $prefix
string $originalName,
string $prefixedName,
Node $stmt
): Expression {
$call = new ClassAliasFuncCall(
FullyQualifiedFactory::concat($prefix, $originalName),
$originalName,
new FullyQualified($prefixedName),
new FullyQualified($originalName),
$stmt->getAttributes(),
);

Expand Down
3 changes: 1 addition & 2 deletions src/PhpParser/TraverserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ private static function createNodeVisitors(
new NodeVisitor\EvalPrefixer($stringNodePrefixer),

new NodeVisitor\ClassAliasStmtAppender(
$prefix,
$reflector,
$identifierResolver,
$symbolsRegistry,
),
new NodeVisitor\MultiConstStmtReplacer(),
new NodeVisitor\ConstStmtReplacer(
Expand Down
8 changes: 8 additions & 0 deletions src/Symbol/SymbolsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function recordClass(FullyQualified $original, FullyQualified $alias): vo
$this->recordedClasses[(string) $original] = [(string) $original, (string) $alias];
}

/**
* @return array{string, string}|null
*/
public function getRecordedClass(string $original): ?array
{
return $this->recordedClasses[$original] ?? null;
}

/**
* @return list<array{string, string}>
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Symbol/SymbolsRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,22 @@ public static function provideRegistriesToMerge(): iterable
];
}

public function test_it_exposes_recorded_classes(): void
{
$registry = self::createRegistry(
[[new FullyQualified('foo'), new FullyQualified('Humbug\foo')]],
[[new FullyQualified('Bar'), new FullyQualified('Humbug\Bar')]],
);

self::assertNull($registry->getRecordedClass('foo'));
self::assertNull($registry->getRecordedClass('bar'));
self::assertNull($registry->getRecordedClass('Humbug\Bar'));
self::assertEquals(
['Bar', 'Humbug\Bar'],
$registry->getRecordedClass('Bar'),
);
}

private static function assertStateIs(
SymbolsRegistry $symbolsRegistry,
array $expectedRecordedFunctions,
Expand Down

0 comments on commit 4501e36

Please sign in to comment.