Skip to content

Commit fbbcac0

Browse files
authored
refactor: Introduce a Parser and Printer factory (#1047)
1 parent 7831a09 commit fbbcac0

20 files changed

+379
-95
lines changed

phpstan.neon.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ parameters:
2222
path: 'src/PhpParser/NodeVisitor/Resolver/OriginalNameResolver.php'
2323
- message: '#NamespaceManipulator::getOriginalName\(\) should return#'
2424
path: 'src/PhpParser/NodeVisitor/NamespaceStmt/NamespaceManipulator.php'
25-
- message: '#DummyScoperFactory extends @final#'
26-
path: 'tests/Console/Command/DummyScoperFactory.php'
2725
- message: '#Anonymous function should return string but returns array#'
2826
path: 'tests/Console/Command/AddPrefixCommandIntegrationTest.php'
2927
- message: '#AddPrefixCommandIntegrationTest\:\:getNormalizeDisplay\(\) should return string but returns array#'
@@ -63,3 +61,5 @@ parameters:
6361
# Fixed in https://github.com/nikic/PHP-Parser/pull/1003
6462
- message: '#Standard constructor expects array#'
6563
path: 'src/Container.php'
64+
- message: '#Standard constructor expects array#'
65+
path: 'src/PhpParser/Printer/StandardPrinterFactory.php'

src/Console/Command/AddPrefixCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use Humbug\PhpScoper\Configuration\Throwable\UnknownConfigurationKey;
2828
use Humbug\PhpScoper\Console\ConfigLoader;
2929
use Humbug\PhpScoper\Console\ConsoleScoper;
30-
use Humbug\PhpScoper\Scoper\ScoperFactory;
30+
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
3131
use InvalidArgumentException;
3232
use Symfony\Component\Console\Exception\RuntimeException;
3333
use Symfony\Component\Console\Input\InputArgument;

src/Console/Command/InspectCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Humbug\PhpScoper\Configuration\Configuration;
2424
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
2525
use Humbug\PhpScoper\Console\ConfigLoader;
26-
use Humbug\PhpScoper\Scoper\ScoperFactory;
26+
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
2727
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
2828
use InvalidArgumentException;
2929
use Symfony\Component\Console\Input\InputArgument;

src/Console/ConsoleScoper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
use Humbug\PhpScoper\Autoload\ComposerFileHasher;
2020
use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator;
2121
use Humbug\PhpScoper\Configuration\Configuration;
22+
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
2223
use Humbug\PhpScoper\Scoper\Scoper;
23-
use Humbug\PhpScoper\Scoper\ScoperFactory;
2424
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
2525
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
2626
use Symfony\Component\Filesystem\Filesystem;

src/Container.php

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
1818
use Humbug\PhpScoper\Configuration\RegexChecker;
1919
use Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory;
20+
use Humbug\PhpScoper\PhpParser\Parser\ParserFactory;
21+
use Humbug\PhpScoper\PhpParser\Parser\StandardParserFactory;
2022
use Humbug\PhpScoper\PhpParser\Printer\Printer;
23+
use Humbug\PhpScoper\PhpParser\Printer\PrinterFactory;
2124
use Humbug\PhpScoper\PhpParser\Printer\StandardPrinter;
22-
use Humbug\PhpScoper\Scoper\ScoperFactory;
25+
use Humbug\PhpScoper\PhpParser\Printer\StandardPrinterFactory;
26+
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
27+
use Humbug\PhpScoper\Scoper\Factory\StandardScoperFactory;
2328
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
2429
use Humbug\PhpScoper\Symbol\Reflector;
25-
use PhpParser\Lexer;
26-
use PhpParser\Lexer\Emulative;
2730
use PhpParser\Parser;
28-
use PhpParser\Parser\Php7;
29-
use PhpParser\Parser\Php8;
3031
use PhpParser\PhpVersion;
3132
use PhpParser\PrettyPrinter\Standard;
3233
use Symfony\Component\Filesystem\Filesystem;
@@ -36,12 +37,14 @@ final class Container
3637
{
3738
private Filesystem $filesystem;
3839
private ConfigurationFactory $configFactory;
40+
private ParserFactory $parserFactory;
3941
private Parser $parser;
4042
private ?PhpVersion $parserPhpVersion = null;
4143
private ?PhpVersion $printerPhpVersion = null;
4244
private Reflector $reflector;
4345
private ScoperFactory $scoperFactory;
4446
private EnrichedReflectorFactory $enrichedReflectorFactory;
47+
private PrinterFactory $printerFactory;
4548
private Printer $printer;
4649

4750
public function getFileSystem(): Filesystem
@@ -67,39 +70,41 @@ public function getConfigurationFactory(): ConfigurationFactory
6770
return $this->configFactory;
6871
}
6972

70-
public function getScoperFactory(?PhpVersion $phpVersion = null): ScoperFactory
73+
public function getScoperFactory(): ScoperFactory
7174
{
7275
if (!isset($this->scoperFactory)) {
73-
$this->scoperFactory = new ScoperFactory(
74-
$this->getParser($phpVersion),
76+
$this->scoperFactory = new StandardScoperFactory(
7577
$this->getEnrichedReflectorFactory(),
76-
$this->getPrinter(),
78+
$this->getParserFactory(),
79+
$this->getPrinterFactory(),
7780
);
7881
}
7982

8083
return $this->scoperFactory;
8184
}
8285

86+
/**
87+
* @deprecated Use ::getParserFactory() instead.
88+
*/
8389
public function getParser(?PhpVersion $phpVersion = null): Parser
8490
{
8591
if (!isset($this->parser)) {
8692
$this->parserPhpVersion = $phpVersion;
87-
$this->parser = $this->createParser($phpVersion);
93+
$this->parser = $this->getParserFactory()->createParser($phpVersion);
8894
}
8995

9096
self::checkSamePhpVersion($this->parserPhpVersion, $phpVersion);
9197

9298
return $this->parser;
9399
}
94100

95-
private function createParser(?PhpVersion $phpVersion): Parser
101+
public function getParserFactory(): ParserFactory
96102
{
97-
$version = $phpVersion ?? PhpVersion::getHostVersion();
98-
$lexer = $version->isHostVersion() ? new Lexer() : new Emulative($version);
103+
if (!isset($this->parserFactory)) {
104+
$this->parserFactory = new StandardParserFactory();
105+
}
99106

100-
return $version->id >= 80_000
101-
? new Php8($lexer, $version)
102-
: new Php7($lexer, $version);
107+
return $this->parserFactory;
103108
}
104109

105110
public function getReflector(): Reflector
@@ -122,6 +127,9 @@ public function getEnrichedReflectorFactory(): EnrichedReflectorFactory
122127
return $this->enrichedReflectorFactory;
123128
}
124129

130+
/**
131+
* @deprecated use ::getPrinterFactory() instead.
132+
*/
125133
public function getPrinter(?PhpVersion $phpVersion = null): Printer
126134
{
127135
if (!isset($this->printer)) {
@@ -138,6 +146,15 @@ public function getPrinter(?PhpVersion $phpVersion = null): Printer
138146
return $this->printer;
139147
}
140148

149+
public function getPrinterFactory(): PrinterFactory
150+
{
151+
if (!isset($this->printerFactory)) {
152+
$this->printerFactory = new StandardPrinterFactory();
153+
}
154+
155+
return $this->printerFactory;
156+
}
157+
141158
private static function checkSamePhpVersion(
142159
?PhpVersion $versionUsed,
143160
?PhpVersion $versionRequest,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\PhpParser\Parser;
16+
17+
use PhpParser\Parser;
18+
use PhpParser\PhpVersion;
19+
20+
interface ParserFactory
21+
{
22+
public function createParser(?PhpVersion $phpVersion = null): Parser;
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\PhpParser\Parser;
16+
17+
use PhpParser\Lexer;
18+
use PhpParser\Lexer\Emulative;
19+
use PhpParser\Parser;
20+
use PhpParser\Parser\Php7;
21+
use PhpParser\Parser\Php8;
22+
use PhpParser\PhpVersion;
23+
24+
final class StandardParserFactory implements ParserFactory
25+
{
26+
public function createParser(?PhpVersion $phpVersion = null): Parser
27+
{
28+
$version = $phpVersion ?? PhpVersion::getHostVersion();
29+
30+
$lexer = $version->isHostVersion()
31+
? new Lexer()
32+
: new Emulative($version);
33+
34+
return $version->id >= 80_000
35+
? new Php8($lexer, $version)
36+
: new Php7($lexer, $version);
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\PhpParser\Printer;
16+
17+
use PhpParser\PhpVersion;
18+
19+
interface PrinterFactory
20+
{
21+
public function createPrinter(?PhpVersion $phpVersion = null): Printer;
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\PhpParser\Printer;
16+
17+
use PhpParser\PhpVersion;
18+
use PhpParser\PrettyPrinter\Standard;
19+
20+
final class StandardPrinterFactory implements PrinterFactory
21+
{
22+
public function createPrinter(?PhpVersion $phpVersion = null): Printer
23+
{
24+
return new StandardPrinter(
25+
new Standard([
26+
'phpVersion' => $phpVersion,
27+
]),
28+
);
29+
}
30+
}

src/Scoper/Factory/ScoperFactory.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\Scoper\Factory;
16+
17+
use Humbug\PhpScoper\Configuration\Configuration;
18+
use Humbug\PhpScoper\Scoper\Scoper;
19+
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
20+
use PhpParser\PhpVersion;
21+
22+
interface ScoperFactory
23+
{
24+
public function createScoper(
25+
Configuration $configuration,
26+
SymbolsRegistry $symbolsRegistry,
27+
?PhpVersion $phpVersion = null,
28+
): Scoper;
29+
}

0 commit comments

Comments
 (0)