Skip to content

Commit 2fad29c

Browse files
authored
Adapt printer API to preserve formatting (#652)
1 parent 5c945c4 commit 2fad29c

File tree

11 files changed

+63
-12
lines changed

11 files changed

+63
-12
lines changed

src/Container.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class Container
3737
private ScoperFactory $scoperFactory;
3838
private EnrichedReflectorFactory $enrichedReflectorFactory;
3939
private Printer $printer;
40+
private Lexer $lexer;
4041

4142
public function getFileSystem(): Filesystem
4243
{
@@ -68,6 +69,7 @@ public function getScoperFactory(): ScoperFactory
6869
$this->getParser(),
6970
$this->getEnrichedReflectorFactory(),
7071
$this->getPrinter(),
72+
$this->getLexer(),
7173
);
7274
}
7375

@@ -77,7 +79,10 @@ public function getScoperFactory(): ScoperFactory
7779
public function getParser(): Parser
7880
{
7981
if (!isset($this->parser)) {
80-
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new Lexer());
82+
$this->parser = (new ParserFactory())->create(
83+
ParserFactory::PREFER_PHP7,
84+
$this->getLexer(),
85+
);
8186
}
8287

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

114119
return $this->printer;
115120
}
121+
122+
public function getLexer(): Lexer
123+
{
124+
if (!isset($this->lexer)) {
125+
$this->lexer = new Lexer();
126+
}
127+
128+
return $this->lexer;
129+
}
116130
}

src/PhpParser/Printer/Printer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
interface Printer
1010
{
1111
/**
12-
* @param Node[] $statements
12+
* @param Node[] $newStmts
13+
* @param Node[] $oldStmts
14+
* @param array<mixed> $oldTokens
1315
*/
14-
public function print(array $statements): string;
16+
public function print(array $newStmts, array $oldStmts, array $oldTokens): string;
1517
}

src/PhpParser/Printer/StandardPrinter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function __construct(
1717
$this->decoratedPrinter = $decoratedPrinter;
1818
}
1919

20-
public function print(array $statements): string
20+
public function print(array $newStmts, array $oldStmts, array $oldTokens): string
2121
{
22-
return $this->decoratedPrinter->prettyPrintFile($statements)."\n";
22+
return $this->decoratedPrinter->prettyPrintFile($newStmts)."\n";
2323
}
2424
}

src/Scoper/PhpScoper.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
use Humbug\PhpScoper\PhpParser\Printer\Printer;
1818
use Humbug\PhpScoper\PhpParser\TraverserFactory;
19-
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
2019
use PhpParser\Error as PhpParserError;
20+
use PhpParser\Lexer;
2121
use PhpParser\Parser;
22-
use PhpParser\PrettyPrinter\Standard;
2322
use function basename;
2423
use function func_get_args;
2524
use function ltrim;
@@ -36,17 +35,20 @@ final class PhpScoper implements Scoper
3635
private Scoper $decoratedScoper;
3736
private TraverserFactory $traverserFactory;
3837
private Printer $printer;
38+
private Lexer $lexer;
3939

4040
public function __construct(
4141
Parser $parser,
4242
Scoper $decoratedScoper,
4343
TraverserFactory $traverserFactory,
44-
Printer $printer
44+
Printer $printer,
45+
Lexer $lexer
4546
) {
4647
$this->parser = $parser;
4748
$this->decoratedScoper = $decoratedScoper;
4849
$this->traverserFactory = $traverserFactory;
4950
$this->printer = $printer;
51+
$this->lexer = $lexer;
5052
}
5153

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

7073
$scopedStatements = $this->traverserFactory
7174
->create($this)
7275
->traverse($statements);
7376

74-
return $this->printer->print($scopedStatements);
77+
return $this->printer->print(
78+
$scopedStatements,
79+
$scopedStatements,
80+
$oldTokens,
81+
);
7582
}
7683

7784
private static function isPhpFile(string $filePath, string $contents): bool

src/Scoper/ScoperFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper;
2323
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
2424
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
25+
use PhpParser\Lexer;
2526
use PhpParser\Parser;
2627

2728
/**
@@ -32,15 +33,18 @@ class ScoperFactory
3233
private Parser $parser;
3334
private EnrichedReflectorFactory $enrichedReflectorFactory;
3435
private Printer $printer;
36+
private Lexer $lexer;
3537

3638
public function __construct(
3739
Parser $parser,
3840
EnrichedReflectorFactory $enrichedReflectorFactory,
39-
Printer $printer
41+
Printer $printer,
42+
Lexer $lexer
4043
) {
4144
$this->parser = $parser;
4245
$this->enrichedReflectorFactory = $enrichedReflectorFactory;
4346
$this->printer = $printer;
47+
$this->lexer = $lexer;
4448
}
4549

4650
public function createScoper(
@@ -78,6 +82,7 @@ public function createScoper(
7882
$symbolsRegistry,
7983
),
8084
$this->printer,
85+
$this->lexer,
8186
),
8287
$prefix,
8388
$configuration->getPatcher(),

tests/Console/Command/DummyScoperFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Humbug\PhpScoper\Scoper\ScoperFactory;
1111
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
1212
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
13+
use PhpParser\Lexer;
1314
use PhpParser\Parser;
1415

1516
final class DummyScoperFactory extends ScoperFactory
@@ -22,7 +23,12 @@ public function __construct(
2223
Printer $printer,
2324
Scoper $scoper
2425
) {
25-
parent::__construct($parser, $enrichedReflectorFactory, $printer);
26+
parent::__construct(
27+
$parser,
28+
$enrichedReflectorFactory,
29+
$printer,
30+
new Lexer(),
31+
);
2632

2733
$this->scoper = $scoper;
2834
}

tests/PhpParser/FakePrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
final class FakePrinter implements Printer
1212
{
13-
public function print(array $statements): string
13+
public function print(array $newStmts, array $oldStmts, array $oldTokens): string
1414
{
1515
throw new LogicException();
1616
}

tests/PhpParser/TraverserFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Humbug\PhpScoper\Symbol\EnrichedReflector;
2121
use Humbug\PhpScoper\Symbol\Reflector;
2222
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
23+
use PhpParser\Lexer;
2324
use PHPUnit\Framework\TestCase;
2425
use ReflectionClass;
2526

@@ -36,6 +37,7 @@ public function test_creates_a_new_traverser_at_each_call(): void
3637
new FakeScoper(),
3738
(new ReflectionClass(TraverserFactory::class))->newInstanceWithoutConstructor(),
3839
new FakePrinter(),
40+
new Lexer(),
3941
);
4042
$symbolsRegistry = new SymbolsRegistry();
4143

tests/Scoper/PhpScoperSpecTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ private static function createScoper(
300300
$symbolsRegistry,
301301
),
302302
$container->getPrinter(),
303+
$container->getLexer(),
303304
);
304305
}
305306

tests/Scoper/PhpScoperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
2626
use LogicException;
2727
use PhpParser\Error as PhpParserError;
28+
use PhpParser\Lexer;
2829
use PhpParser\Node\Name;
2930
use PhpParser\NodeTraverserInterface;
3031
use PhpParser\Parser;
@@ -68,6 +69,8 @@ class PhpScoperTest extends TestCase
6869
private SymbolsRegistry $symbolsRegistry;
6970

7071
private Printer $printer;
72+
73+
private Lexer $lexer;
7174

7275
protected function setUp(): void
7376
{
@@ -83,6 +86,11 @@ protected function setUp(): void
8386
$this->symbolsRegistry = new SymbolsRegistry();
8487
$this->printer = new StandardPrinter(new Standard());
8588

89+
$lexerProphecy = $this->prophesize(Lexer::class);
90+
$lexerProphecy->getTokens()->willReturn([]);
91+
92+
$this->lexer = $lexerProphecy->reveal();
93+
8694
$this->scoper = new PhpScoper(
8795
create_parser(),
8896
new FakeScoper(),
@@ -95,6 +103,7 @@ protected function setUp(): void
95103
$this->symbolsRegistry,
96104
),
97105
$this->printer,
106+
$this->lexer,
98107
);
99108
}
100109

@@ -147,6 +156,7 @@ public function test_does_not_scope_file_if_is_not_a_PHP_file(): void
147156
$this->decoratedScoper,
148157
$this->traverserFactory,
149158
new FakePrinter(),
159+
$this->lexer,
150160
);
151161

152162
$actual = $scoper->scope($filePath, $fileContents);
@@ -234,6 +244,7 @@ public function test_does_not_scope_a_non_PHP_executable_files(): void
234244
$this->decoratedScoper,
235245
$this->traverserFactory,
236246
new FakePrinter(),
247+
$this->lexer,
237248
);
238249

239250
$actual = $scoper->scope($filePath, $contents);
@@ -332,6 +343,7 @@ static function () use (&$i): bool {
332343
new FakeScoper(),
333344
$this->traverserFactory,
334345
$this->printer,
346+
$this->lexer,
335347
);
336348

337349
foreach ($files as $file => $contents) {

0 commit comments

Comments
 (0)