Skip to content

Simplify TraverserFactory #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/PhpParser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\NodeTraverser as PhpParserNodeTraverser;
use PhpParser\NodeTraverserInterface;
use PhpParser\NodeVisitor;
use function array_map;
use function array_slice;
use function array_splice;
Expand All @@ -34,14 +35,31 @@
/**
* @private
*/
final class NodeTraverser extends PhpParserNodeTraverser
final class NodeTraverser implements NodeTraverserInterface
{
private NodeTraverserInterface $decoratedTraverser;

public function __construct(NodeTraverserInterface $decoratedTraverser)
{
$this->decoratedTraverser = $decoratedTraverser;
}

public function addVisitor(NodeVisitor $visitor): void
{
$this->decoratedTraverser->addVisitor($visitor);
}

public function removeVisitor(NodeVisitor $visitor): void
{
$this->decoratedTraverser->removeVisitor($visitor);
}

public function traverse(array $nodes): array
{
$nodes = $this->wrapInNamespace($nodes);
$nodes = $this->replaceGroupUseStatements($nodes);

return parent::traverse($nodes);
return $this->decoratedTraverser->traverse($nodes);
}

/**
Expand Down
184 changes: 102 additions & 82 deletions src/PhpParser/TraverserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Humbug\PhpScoper\Scoper\PhpScoper;
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\NodeTraverser as PhpParserNodeTraverser;
use PhpParser\NodeTraverserInterface;
use PhpParser\NodeVisitor as PhpParserNodeVisitor;
use PhpParser\NodeVisitor\NameResolver;
Expand All @@ -30,20 +31,57 @@
class TraverserFactory
{
private EnrichedReflector $reflector;
private string $prefix;
private SymbolsRegistry $symbolsRegistry;

public function __construct(EnrichedReflector $reflector)
{
public function __construct(
EnrichedReflector $reflector,
string $prefix,
SymbolsRegistry $symbolsRegistry
) {
$this->reflector = $reflector;
$this->prefix = $prefix;
$this->symbolsRegistry = $symbolsRegistry;
}

public function create(
PhpScoper $scoper,
public function create(PhpScoper $scoper): NodeTraverserInterface
{
return self::createTraverser(
self::createNodeVisitors(
$this->prefix,
$this->reflector,
$scoper,
$this->symbolsRegistry,
)
);
}

/**
* @param PhpParserNodeVisitor[] $nodeVisitors
*/
private static function createTraverser(array $nodeVisitors): NodeTraverserInterface
{
$traverser = new NodeTraverser(
new PhpParserNodeTraverser(),
);

foreach ($nodeVisitors as $nodeVisitor) {
$traverser->addVisitor($nodeVisitor);
}

return $traverser;
}

/**
* @return PhpParserNodeVisitor[]
*/
private static function createNodeVisitors(
string $prefix,
EnrichedReflector $reflector,
PhpScoper $scoper,
SymbolsRegistry $symbolsRegistry
): NodeTraverserInterface
): array
{
$traverser = new NodeTraverser();

$namespaceStatements = new NamespaceStmtCollection();
$useStatements = new UseStmtCollection();

Expand All @@ -53,80 +91,62 @@ public function create(
);
$identifierResolver = new IdentifierResolver($nameResolver);
$stringNodePrefixer = new StringNodePrefixer($scoper);

self::addVisitors(
$traverser,
[
$nameResolver,
new NodeVisitor\ParentNodeAppender(),
new NodeVisitor\IdentifierNameAppender($identifierResolver),

new NodeVisitor\NamespaceStmt\NamespaceStmtPrefixer(
$prefix,
$this->reflector,
$namespaceStatements,
),

new NodeVisitor\UseStmt\UseStmtCollector(
$namespaceStatements,
$useStatements,
),
new NodeVisitor\UseStmt\UseStmtPrefixer(
$prefix,
$this->reflector,
),

new NodeVisitor\NamespaceStmt\FunctionIdentifierRecorder(
$prefix,
$identifierResolver,
$symbolsRegistry,
$this->reflector,
),
new NodeVisitor\ClassIdentifierRecorder(
$prefix,
$identifierResolver,
$symbolsRegistry,
$this->reflector,
),
new NodeVisitor\NameStmtPrefixer(
$prefix,
$namespaceStatements,
$useStatements,
$this->reflector,
),
new NodeVisitor\StringScalarPrefixer(
$prefix,
$this->reflector,
),
new NodeVisitor\NewdocPrefixer($stringNodePrefixer),
new NodeVisitor\EvalPrefixer($stringNodePrefixer),

new NodeVisitor\ClassAliasStmtAppender(
$prefix,
$this->reflector,
$identifierResolver,
),
new NodeVisitor\MultiConstStmtReplacer(),
new NodeVisitor\ConstStmtReplacer(
$identifierResolver,
$this->reflector,
),
],
);

return $traverser;
}

/**
* @param PhpParserNodeVisitor[] $nodeVisitors
*/
private static function addVisitors(
NodeTraverserInterface $nodeTraverser,
array $nodeVisitors
): void
{
foreach ($nodeVisitors as $nodeVisitor) {
$nodeTraverser->addVisitor($nodeVisitor);
}

return [
$nameResolver,
new NodeVisitor\ParentNodeAppender(),
new NodeVisitor\IdentifierNameAppender($identifierResolver),

new NodeVisitor\NamespaceStmt\NamespaceStmtPrefixer(
$prefix,
$reflector,
$namespaceStatements,
),

new NodeVisitor\UseStmt\UseStmtCollector(
$namespaceStatements,
$useStatements,
),
new NodeVisitor\UseStmt\UseStmtPrefixer(
$prefix,
$reflector,
),

new NodeVisitor\NamespaceStmt\FunctionIdentifierRecorder(
$prefix,
$identifierResolver,
$symbolsRegistry,
$reflector,
),
new NodeVisitor\ClassIdentifierRecorder(
$prefix,
$identifierResolver,
$symbolsRegistry,
$reflector,
),
new NodeVisitor\NameStmtPrefixer(
$prefix,
$namespaceStatements,
$useStatements,
$reflector,
),
new NodeVisitor\StringScalarPrefixer(
$prefix,
$reflector,
),
new NodeVisitor\NewdocPrefixer($stringNodePrefixer),
new NodeVisitor\EvalPrefixer($stringNodePrefixer),

new NodeVisitor\ClassAliasStmtAppender(
$prefix,
$reflector,
$identifierResolver,
),
new NodeVisitor\MultiConstStmtReplacer(),
new NodeVisitor\ConstStmtReplacer(
$identifierResolver,
$reflector,
),
];
}
}
18 changes: 4 additions & 14 deletions src/Scoper/PhpScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,15 @@ final class PhpScoper implements Scoper
private Parser $parser;
private Scoper $decoratedScoper;
private TraverserFactory $traverserFactory;
private string $prefix;
private SymbolsRegistry $symbolsRegistry;

public function __construct(
Parser $parser,
Scoper $decoratedScoper,
TraverserFactory $traverserFactory,
string $prefix,
SymbolsRegistry $symbolsRegistry
TraverserFactory $traverserFactory
) {
$this->parser = $parser;
$this->decoratedScoper = $decoratedScoper;
$this->traverserFactory = $traverserFactory;
$this->prefix = $prefix;
$this->symbolsRegistry = $symbolsRegistry;
}

/**
Expand All @@ -69,17 +63,13 @@ public function scopePhp(string $php): string
{
$statements = $this->parser->parse($php);

$statements = $this->traverserFactory
->create(
$this,
$this->prefix,
$this->symbolsRegistry,
)
$scopedStatements = $this->traverserFactory
->create($this)
->traverse($statements);

$prettyPrinter = new Standard();

return $prettyPrinter->prettyPrintFile($statements)."\n";
return $prettyPrinter->prettyPrintFile($scopedStatements)."\n";
}

private static function isPhpFile(string $filePath, string $contents): bool
Expand Down
8 changes: 5 additions & 3 deletions src/Scoper/ScoperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ public function createScoper(
),
$autoloadPrefixer
),
new TraverserFactory($enrichedReflector),
$prefix,
$symbolsRegistry,
new TraverserFactory(
$enrichedReflector,
$prefix,
$symbolsRegistry,
),
),
$prefix,
$configuration->getPatcher(),
Expand Down
14 changes: 3 additions & 11 deletions tests/PhpParser/TraverserFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public function test_creates_a_new_traverser_at_each_call(): void
new FakeParser(),
new FakeScoper(),
(new ReflectionClass(TraverserFactory::class))->newInstanceWithoutConstructor(),
$prefix,
new SymbolsRegistry(),
);
$symbolsRegistry = new SymbolsRegistry();

Expand All @@ -45,19 +43,13 @@ public function test_creates_a_new_traverser_at_each_call(): void
Reflector::createEmpty(),
SymbolsConfiguration::create(),
),
);

$firstTraverser = $traverserFactory->create(
$phpScoper,
$prefix,
$symbolsRegistry,
);
$secondTraverser = $traverserFactory->create(
$phpScoper,
$prefix,
$symbolsRegistry,
);

$firstTraverser = $traverserFactory->create($phpScoper);
$secondTraverser = $traverserFactory->create($phpScoper);

self::assertNotSame($firstTraverser, $secondTraverser);
}
}
6 changes: 4 additions & 2 deletions tests/PhpParser/UseStmtNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Humbug\PhpScoper\PhpParser\NodeVisitor\UseStmt\UseStmtCollection;
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use Humbug\PhpScoper\Symbol\Reflector;
use Humbug\PhpScoper\Whitelist;
use LogicException;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeTraverser as PhpParserNodeTraverser;
use PHPUnit\Framework\TestCase;
use function Humbug\PhpScoper\create_parser;
use function Safe\sprintf;
Expand Down Expand Up @@ -172,7 +172,9 @@ private static function parseUseStmtName(string $php): Name
$namespaceStatements = new NamespaceStmtCollection();
$useStatements = new UseStmtCollection();

$traverser = new NodeTraverser();
$traverser = new NodeTraverser(
new PhpParserNodeTraverser(),
);

$traverser->addVisitor(new NodeVisitor\ParentNodeAppender());
$traverser->addVisitor(
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpScoperAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Humbug\PhpScoper;

use PHPUnit\Framework\Assert;
use function array_is_list;
use function var_export;

final class PhpScoperAssertions
{
Expand Down
8 changes: 5 additions & 3 deletions tests/Scoper/PhpScoperSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,11 @@ private static function createScoper(
return new PhpScoper(
$phpParser,
new FakeScoper(),
new TraverserFactory($enrichedReflector),
$prefix,
$symbolsRegistry,
new TraverserFactory(
$enrichedReflector,
$prefix,
$symbolsRegistry,
),
);
}

Expand Down
Loading