Skip to content

Commit b5fd66a

Browse files
authored
Introduce a Printer abstraction (#655)
1 parent 6d2fbfe commit b5fd66a

13 files changed

+117
-11
lines changed

src/Container.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
1818
use Humbug\PhpScoper\Configuration\RegexChecker;
1919
use Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory;
20+
use Humbug\PhpScoper\PhpParser\Printer\Printer;
21+
use Humbug\PhpScoper\PhpParser\Printer\StandardPrinter;
2022
use Humbug\PhpScoper\Scoper\ScoperFactory;
2123
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
2224
use Humbug\PhpScoper\Symbol\Reflector;
2325
use PhpParser\Lexer;
2426
use PhpParser\Parser;
2527
use PhpParser\ParserFactory;
28+
use PhpParser\PrettyPrinter\Standard;
2629
use Symfony\Component\Filesystem\Filesystem;
2730

2831
final class Container
@@ -33,6 +36,7 @@ final class Container
3336
private Reflector $reflector;
3437
private ScoperFactory $scoperFactory;
3538
private EnrichedReflectorFactory $enrichedReflectorFactory;
39+
private Printer $printer;
3640

3741
public function getFileSystem(): Filesystem
3842
{
@@ -63,6 +67,7 @@ public function getScoperFactory(): ScoperFactory
6367
$this->scoperFactory = new ScoperFactory(
6468
$this->getParser(),
6569
$this->getEnrichedReflectorFactory(),
70+
$this->getPrinter(),
6671
);
6772
}
6873

@@ -97,4 +102,15 @@ public function getEnrichedReflectorFactory(): EnrichedReflectorFactory
97102

98103
return $this->enrichedReflectorFactory;
99104
}
105+
106+
public function getPrinter(): Printer
107+
{
108+
if (!isset($this->printer)) {
109+
$this->printer = new StandardPrinter(
110+
new Standard(),
111+
);
112+
}
113+
114+
return $this->printer;
115+
}
100116
}

src/PhpParser/Printer/Printer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Humbug\PhpScoper\PhpParser\Printer;
6+
7+
use PhpParser\Node;
8+
9+
interface Printer
10+
{
11+
/**
12+
* @param Node[] $statements
13+
*/
14+
public function print(array $statements): string;
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Humbug\PhpScoper\PhpParser\Printer;
6+
7+
use PhpParser\PrettyPrinterAbstract;
8+
9+
final class StandardPrinter implements Printer
10+
{
11+
private PrettyPrinterAbstract $decoratedPrinter;
12+
13+
public function __construct(
14+
PrettyPrinterAbstract $decoratedPrinter
15+
) {
16+
17+
$this->decoratedPrinter = $decoratedPrinter;
18+
}
19+
20+
public function print(array $statements): string
21+
{
22+
return $this->decoratedPrinter->prettyPrintFile($statements)."\n";
23+
}
24+
}

src/Scoper/PhpScoper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Humbug\PhpScoper\Scoper;
1616

17+
use Humbug\PhpScoper\PhpParser\Printer\Printer;
1718
use Humbug\PhpScoper\PhpParser\TraverserFactory;
1819
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
1920
use PhpParser\Error as PhpParserError;
@@ -34,15 +35,18 @@ final class PhpScoper implements Scoper
3435
private Parser $parser;
3536
private Scoper $decoratedScoper;
3637
private TraverserFactory $traverserFactory;
38+
private Printer $printer;
3739

3840
public function __construct(
3941
Parser $parser,
4042
Scoper $decoratedScoper,
41-
TraverserFactory $traverserFactory
43+
TraverserFactory $traverserFactory,
44+
Printer $printer
4245
) {
4346
$this->parser = $parser;
4447
$this->decoratedScoper = $decoratedScoper;
4548
$this->traverserFactory = $traverserFactory;
49+
$this->printer = $printer;
4650
}
4751

4852
/**
@@ -67,9 +71,7 @@ public function scopePhp(string $php): string
6771
->create($this)
6872
->traverse($statements);
6973

70-
$prettyPrinter = new Standard();
71-
72-
return $prettyPrinter->prettyPrintFile($scopedStatements)."\n";
74+
return $this->printer->print($scopedStatements);
7375
}
7476

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

src/Scoper/ScoperFactory.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Humbug\PhpScoper\Scoper;
1616

1717
use Humbug\PhpScoper\Configuration\Configuration;
18+
use Humbug\PhpScoper\PhpParser\Printer\Printer;
1819
use Humbug\PhpScoper\PhpParser\TraverserFactory;
1920
use Humbug\PhpScoper\Scoper\Composer\AutoloadPrefixer;
2021
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper;
@@ -30,11 +31,16 @@ class ScoperFactory
3031
{
3132
private Parser $parser;
3233
private EnrichedReflectorFactory $enrichedReflectorFactory;
34+
private Printer $printer;
3335

34-
public function __construct(Parser $parser, EnrichedReflectorFactory $enrichedReflectorFactory)
35-
{
36+
public function __construct(
37+
Parser $parser,
38+
EnrichedReflectorFactory $enrichedReflectorFactory,
39+
Printer $printer
40+
) {
3641
$this->parser = $parser;
3742
$this->enrichedReflectorFactory = $enrichedReflectorFactory;
43+
$this->printer = $printer;
3844
}
3945

4046
public function createScoper(
@@ -71,6 +77,7 @@ public function createScoper(
7177
$prefix,
7278
$symbolsRegistry,
7379
),
80+
$this->printer,
7481
),
7582
$prefix,
7683
$configuration->getPatcher(),

tests/Console/Command/AddPrefixCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Humbug\PhpScoper\Container;
2626
use Humbug\PhpScoper\FileSystemTestCase;
2727
use Humbug\PhpScoper\PhpParser\FakeParser;
28+
use Humbug\PhpScoper\PhpParser\FakePrinter;
2829
use Humbug\PhpScoper\Scoper\Scoper;
2930
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
3031
use Humbug\PhpScoper\Symbol\Reflector;
@@ -587,6 +588,7 @@ private function createAppTester(): ApplicationTester
587588
new EnrichedReflectorFactory(
588589
Reflector::createEmpty(),
589590
),
591+
new FakePrinter(),
590592
$scoper,
591593
),
592594
$innerApp,

tests/Console/Command/AppIntegrationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Humbug\PhpScoper\Container;
2626
use Humbug\PhpScoper\FileSystemTestCase;
2727
use Humbug\PhpScoper\PhpParser\FakeParser;
28+
use Humbug\PhpScoper\PhpParser\FakePrinter;
2829
use Humbug\PhpScoper\Scoper\Scoper;
2930
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
3031
use Humbug\PhpScoper\Symbol\Reflector;
@@ -157,6 +158,7 @@ private function createAppTester(): ApplicationTester
157158
new DummyScoperFactory(
158159
new FakeParser(),
159160
new EnrichedReflectorFactory(Reflector::createEmpty()),
161+
new FakePrinter(),
160162
$scoper,
161163
),
162164
$innerApp,

tests/Console/Command/DummyScoperFactory.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Humbug\PhpScoper\Console\Command;
66

77
use Humbug\PhpScoper\Configuration\Configuration;
8+
use Humbug\PhpScoper\PhpParser\Printer\Printer;
89
use Humbug\PhpScoper\Scoper\Scoper;
910
use Humbug\PhpScoper\Scoper\ScoperFactory;
1011
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
@@ -15,9 +16,13 @@ final class DummyScoperFactory extends ScoperFactory
1516
{
1617
private Scoper $scoper;
1718

18-
public function __construct(Parser $parser, EnrichedReflectorFactory $enrichedReflectorFactory, Scoper $scoper)
19-
{
20-
parent::__construct($parser, $enrichedReflectorFactory);
19+
public function __construct(
20+
Parser $parser,
21+
EnrichedReflectorFactory $enrichedReflectorFactory,
22+
Printer $printer,
23+
Scoper $scoper
24+
) {
25+
parent::__construct($parser, $enrichedReflectorFactory, $printer);
2126

2227
$this->scoper = $scoper;
2328
}

tests/PhpParser/FakePrinter.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Humbug\PhpScoper\PhpParser;
6+
7+
use Humbug\PhpScoper\PhpParser\Printer\Printer;
8+
use LogicException;
9+
use PhpParser\Node;
10+
11+
final class FakePrinter implements Printer
12+
{
13+
public function print(array $statements): string
14+
{
15+
throw new LogicException();
16+
}
17+
}

tests/PhpParser/TraverserFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function test_creates_a_new_traverser_at_each_call(): void
3535
new FakeParser(),
3636
new FakeScoper(),
3737
(new ReflectionClass(TraverserFactory::class))->newInstanceWithoutConstructor(),
38+
new FakePrinter(),
3839
);
3940
$symbolsRegistry = new SymbolsRegistry();
4041

tests/Scoper/PhpScoperSpecTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Humbug\PhpScoper\Configuration\RegexChecker;
2020
use Humbug\PhpScoper\Configuration\SymbolsConfiguration;
2121
use Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory;
22+
use Humbug\PhpScoper\Container;
2223
use Humbug\PhpScoper\PhpParser\TraverserFactory;
2324
use Humbug\PhpScoper\Symbol\EnrichedReflector;
2425
use Humbug\PhpScoper\Symbol\NamespaceRegistry;
@@ -275,7 +276,7 @@ private static function createScoper(
275276
SymbolsRegistry $symbolsRegistry
276277
): Scoper
277278
{
278-
$phpParser = create_parser();
279+
$container = new Container();
279280

280281
$reflector = Reflector
281282
::createWithPhpStormStubs()
@@ -291,13 +292,14 @@ private static function createScoper(
291292
);
292293

293294
return new PhpScoper(
294-
$phpParser,
295+
$container->getParser(),
295296
new FakeScoper(),
296297
new TraverserFactory(
297298
$enrichedReflector,
298299
$prefix,
299300
$symbolsRegistry,
300301
),
302+
$container->getPrinter(),
301303
);
302304
}
303305

tests/Scoper/PhpScoperTest.php

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

1717
use Humbug\PhpScoper\Configuration\SymbolsConfiguration;
1818
use Humbug\PhpScoper\PhpParser\FakeParser;
19+
use Humbug\PhpScoper\PhpParser\FakePrinter;
20+
use Humbug\PhpScoper\PhpParser\Printer\Printer;
21+
use Humbug\PhpScoper\PhpParser\Printer\StandardPrinter;
1922
use Humbug\PhpScoper\PhpParser\TraverserFactory;
2023
use Humbug\PhpScoper\Symbol\EnrichedReflector;
2124
use Humbug\PhpScoper\Symbol\Reflector;
@@ -25,6 +28,7 @@
2528
use PhpParser\Node\Name;
2629
use PhpParser\NodeTraverserInterface;
2730
use PhpParser\Parser;
31+
use PhpParser\PrettyPrinter\Standard;
2832
use PHPUnit\Framework\TestCase;
2933
use Prophecy\Argument;
3034
use Prophecy\PhpUnit\ProphecyTrait;
@@ -63,6 +67,8 @@ class PhpScoperTest extends TestCase
6367

6468
private SymbolsRegistry $symbolsRegistry;
6569

70+
private Printer $printer;
71+
6672
protected function setUp(): void
6773
{
6874
$this->decoratedScoperProphecy = $this->prophesize(Scoper::class);
@@ -75,6 +81,7 @@ protected function setUp(): void
7581
$this->parser = $this->parserProphecy->reveal();
7682

7783
$this->symbolsRegistry = new SymbolsRegistry();
84+
$this->printer = new StandardPrinter(new Standard());
7885

7986
$this->scoper = new PhpScoper(
8087
create_parser(),
@@ -87,6 +94,7 @@ protected function setUp(): void
8794
self::PREFIX,
8895
$this->symbolsRegistry,
8996
),
97+
$this->printer,
9098
);
9199
}
92100

@@ -138,6 +146,7 @@ public function test_does_not_scope_file_if_is_not_a_PHP_file(): void
138146
new FakeParser(),
139147
$this->decoratedScoper,
140148
$this->traverserFactory,
149+
new FakePrinter(),
141150
);
142151

143152
$actual = $scoper->scope($filePath, $fileContents);
@@ -224,6 +233,7 @@ public function test_does_not_scope_a_non_PHP_executable_files(): void
224233
new FakeParser(),
225234
$this->decoratedScoper,
226235
$this->traverserFactory,
236+
new FakePrinter(),
227237
);
228238

229239
$actual = $scoper->scope($filePath, $contents);
@@ -321,6 +331,7 @@ static function () use (&$i): bool {
321331
$this->parser,
322332
new FakeScoper(),
323333
$this->traverserFactory,
334+
$this->printer,
324335
);
325336

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

tests/Scoper/ScoperFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Humbug\PhpScoper\Configuration\SymbolsConfiguration;
99
use Humbug\PhpScoper\Patcher\FakePatcher;
1010
use Humbug\PhpScoper\PhpParser\FakeParser;
11+
use Humbug\PhpScoper\PhpParser\FakePrinter;
1112
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
1213
use Humbug\PhpScoper\Symbol\Reflector;
1314
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
@@ -25,6 +26,7 @@ public function test_it_can_create_a_scoper(): void
2526
new EnrichedReflectorFactory(
2627
Reflector::createEmpty(),
2728
),
29+
new FakePrinter(),
2830
);
2931

3032
$factory->createScoper(

0 commit comments

Comments
 (0)