Skip to content
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

Adapt printer API to preserve formatting #652

Merged
merged 4 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
16 changes: 15 additions & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class Container
private ScoperFactory $scoperFactory;
private EnrichedReflectorFactory $enrichedReflectorFactory;
private Printer $printer;
private Lexer $lexer;

public function getFileSystem(): Filesystem
{
Expand Down Expand Up @@ -68,6 +69,7 @@ public function getScoperFactory(): ScoperFactory
$this->getParser(),
$this->getEnrichedReflectorFactory(),
$this->getPrinter(),
$this->getLexer(),
);
}

Expand All @@ -77,7 +79,10 @@ public function getScoperFactory(): ScoperFactory
public function getParser(): Parser
{
if (!isset($this->parser)) {
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new Lexer());
$this->parser = (new ParserFactory())->create(
ParserFactory::PREFER_PHP7,
$this->getLexer(),
);
}

return $this->parser;
Expand Down Expand Up @@ -113,4 +118,13 @@ public function getPrinter(): Printer

return $this->printer;
}

public function getLexer(): Lexer
{
if (!isset($this->lexer)) {
$this->lexer = new Lexer();
}

return $this->lexer;
}
}
6 changes: 4 additions & 2 deletions src/PhpParser/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
interface Printer
{
/**
* @param Node[] $statements
* @param Node[] $newStmts
* @param Node[] $oldStmts
* @param array<mixed> $oldTokens
*/
public function print(array $statements): string;
public function print(array $newStmts, array $oldStmts, array $oldTokens): string;
}
4 changes: 2 additions & 2 deletions src/PhpParser/Printer/StandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function __construct(
$this->decoratedPrinter = $decoratedPrinter;
}

public function print(array $statements): string
public function print(array $newStmts, array $oldStmts, array $oldTokens): string
{
return $this->decoratedPrinter->prettyPrintFile($statements)."\n";
return $this->decoratedPrinter->prettyPrintFile($newStmts)."\n";
}
}
15 changes: 11 additions & 4 deletions src/Scoper/PhpScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

use Humbug\PhpScoper\PhpParser\Printer\Printer;
use Humbug\PhpScoper\PhpParser\TraverserFactory;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Error as PhpParserError;
use PhpParser\Lexer;
use PhpParser\Parser;
use PhpParser\PrettyPrinter\Standard;
use function basename;
use function func_get_args;
use function ltrim;
Expand All @@ -36,17 +35,20 @@ final class PhpScoper implements Scoper
private Scoper $decoratedScoper;
private TraverserFactory $traverserFactory;
private Printer $printer;
private Lexer $lexer;

public function __construct(
Parser $parser,
Scoper $decoratedScoper,
TraverserFactory $traverserFactory,
Printer $printer
Printer $printer,
Lexer $lexer
) {
$this->parser = $parser;
$this->decoratedScoper = $decoratedScoper;
$this->traverserFactory = $traverserFactory;
$this->printer = $printer;
$this->lexer = $lexer;
}

/**
Expand All @@ -66,12 +68,17 @@ public function scope(string $filePath, string $contents): string
public function scopePhp(string $php): string
{
$statements = $this->parser->parse($php);
$oldTokens = $this->lexer->getTokens();

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

return $this->printer->print($scopedStatements);
return $this->printer->print(
$scopedStatements,
$scopedStatements,
$oldTokens,
);
}

private static function isPhpFile(string $filePath, string $contents): bool
Expand Down
7 changes: 6 additions & 1 deletion src/Scoper/ScoperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper;
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Lexer;
use PhpParser\Parser;

/**
Expand All @@ -32,15 +33,18 @@ class ScoperFactory
private Parser $parser;
private EnrichedReflectorFactory $enrichedReflectorFactory;
private Printer $printer;
private Lexer $lexer;

public function __construct(
Parser $parser,
EnrichedReflectorFactory $enrichedReflectorFactory,
Printer $printer
Printer $printer,
Lexer $lexer
) {
$this->parser = $parser;
$this->enrichedReflectorFactory = $enrichedReflectorFactory;
$this->printer = $printer;
$this->lexer = $lexer;
}

public function createScoper(
Expand Down Expand Up @@ -78,6 +82,7 @@ public function createScoper(
$symbolsRegistry,
),
$this->printer,
$this->lexer,
),
$prefix,
$configuration->getPatcher(),
Expand Down
8 changes: 7 additions & 1 deletion tests/Console/Command/DummyScoperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Humbug\PhpScoper\Scoper\ScoperFactory;
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Lexer;
use PhpParser\Parser;

final class DummyScoperFactory extends ScoperFactory
Expand All @@ -22,7 +23,12 @@ public function __construct(
Printer $printer,
Scoper $scoper
) {
parent::__construct($parser, $enrichedReflectorFactory, $printer);
parent::__construct(
$parser,
$enrichedReflectorFactory,
$printer,
new Lexer(),
);

$this->scoper = $scoper;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpParser/FakePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class FakePrinter implements Printer
{
public function print(array $statements): string
public function print(array $newStmts, array $oldStmts, array $oldTokens): string
{
throw new LogicException();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpParser/TraverserFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use Humbug\PhpScoper\Symbol\Reflector;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Lexer;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

Expand All @@ -36,6 +37,7 @@ public function test_creates_a_new_traverser_at_each_call(): void
new FakeScoper(),
(new ReflectionClass(TraverserFactory::class))->newInstanceWithoutConstructor(),
new FakePrinter(),
new Lexer(),
);
$symbolsRegistry = new SymbolsRegistry();

Expand Down
1 change: 1 addition & 0 deletions tests/Scoper/PhpScoperSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ private static function createScoper(
$symbolsRegistry,
),
$container->getPrinter(),
$container->getLexer(),
);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Scoper/PhpScoperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use LogicException;
use PhpParser\Error as PhpParserError;
use PhpParser\Lexer;
use PhpParser\Node\Name;
use PhpParser\NodeTraverserInterface;
use PhpParser\Parser;
Expand Down Expand Up @@ -68,6 +69,8 @@ class PhpScoperTest extends TestCase
private SymbolsRegistry $symbolsRegistry;

private Printer $printer;

private Lexer $lexer;

protected function setUp(): void
{
Expand All @@ -83,6 +86,11 @@ protected function setUp(): void
$this->symbolsRegistry = new SymbolsRegistry();
$this->printer = new StandardPrinter(new Standard());

$lexerProphecy = $this->prophesize(Lexer::class);
$lexerProphecy->getTokens()->willReturn([]);

$this->lexer = $lexerProphecy->reveal();

$this->scoper = new PhpScoper(
create_parser(),
new FakeScoper(),
Expand All @@ -95,6 +103,7 @@ protected function setUp(): void
$this->symbolsRegistry,
),
$this->printer,
$this->lexer,
);
}

Expand Down Expand Up @@ -147,6 +156,7 @@ public function test_does_not_scope_file_if_is_not_a_PHP_file(): void
$this->decoratedScoper,
$this->traverserFactory,
new FakePrinter(),
$this->lexer,
);

$actual = $scoper->scope($filePath, $fileContents);
Expand Down Expand Up @@ -234,6 +244,7 @@ public function test_does_not_scope_a_non_PHP_executable_files(): void
$this->decoratedScoper,
$this->traverserFactory,
new FakePrinter(),
$this->lexer,
);

$actual = $scoper->scope($filePath, $contents);
Expand Down Expand Up @@ -332,6 +343,7 @@ static function () use (&$i): bool {
new FakeScoper(),
$this->traverserFactory,
$this->printer,
$this->lexer,
);

foreach ($files as $file => $contents) {
Expand Down
2 changes: 2 additions & 0 deletions tests/Scoper/ScoperFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
use Humbug\PhpScoper\Symbol\Reflector;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\Lexer;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -27,6 +28,7 @@ public function test_it_can_create_a_scoper(): void
Reflector::createEmpty(),
),
new FakePrinter(),
new Lexer(),
);

$factory->createScoper(
Expand Down