Skip to content

Commit

Permalink
refactor: Introduce a Parser and Printer factory (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Jun 16, 2024
1 parent 7831a09 commit fbbcac0
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 95 deletions.
4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ parameters:
path: 'src/PhpParser/NodeVisitor/Resolver/OriginalNameResolver.php'
- message: '#NamespaceManipulator::getOriginalName\(\) should return#'
path: 'src/PhpParser/NodeVisitor/NamespaceStmt/NamespaceManipulator.php'
- message: '#DummyScoperFactory extends @final#'
path: 'tests/Console/Command/DummyScoperFactory.php'
- message: '#Anonymous function should return string but returns array#'
path: 'tests/Console/Command/AddPrefixCommandIntegrationTest.php'
- message: '#AddPrefixCommandIntegrationTest\:\:getNormalizeDisplay\(\) should return string but returns array#'
Expand Down Expand Up @@ -63,3 +61,5 @@ parameters:
# Fixed in https://github.com/nikic/PHP-Parser/pull/1003
- message: '#Standard constructor expects array#'
path: 'src/Container.php'
- message: '#Standard constructor expects array#'
path: 'src/PhpParser/Printer/StandardPrinterFactory.php'
2 changes: 1 addition & 1 deletion src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Humbug\PhpScoper\Configuration\Throwable\UnknownConfigurationKey;
use Humbug\PhpScoper\Console\ConfigLoader;
use Humbug\PhpScoper\Console\ConsoleScoper;
use Humbug\PhpScoper\Scoper\ScoperFactory;
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
use InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/InspectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Humbug\PhpScoper\Configuration\Configuration;
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
use Humbug\PhpScoper\Console\ConfigLoader;
use Humbug\PhpScoper\Scoper\ScoperFactory;
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/ConsoleScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use Humbug\PhpScoper\Autoload\ComposerFileHasher;
use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator;
use Humbug\PhpScoper\Configuration\Configuration;
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
use Humbug\PhpScoper\Scoper\Scoper;
use Humbug\PhpScoper\Scoper\ScoperFactory;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
use Symfony\Component\Filesystem\Filesystem;
Expand Down
49 changes: 33 additions & 16 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
use Humbug\PhpScoper\Configuration\RegexChecker;
use Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory;
use Humbug\PhpScoper\PhpParser\Parser\ParserFactory;
use Humbug\PhpScoper\PhpParser\Parser\StandardParserFactory;
use Humbug\PhpScoper\PhpParser\Printer\Printer;
use Humbug\PhpScoper\PhpParser\Printer\PrinterFactory;
use Humbug\PhpScoper\PhpParser\Printer\StandardPrinter;
use Humbug\PhpScoper\Scoper\ScoperFactory;
use Humbug\PhpScoper\PhpParser\Printer\StandardPrinterFactory;
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
use Humbug\PhpScoper\Scoper\Factory\StandardScoperFactory;
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
use Humbug\PhpScoper\Symbol\Reflector;
use PhpParser\Lexer;
use PhpParser\Lexer\Emulative;
use PhpParser\Parser;
use PhpParser\Parser\Php7;
use PhpParser\Parser\Php8;
use PhpParser\PhpVersion;
use PhpParser\PrettyPrinter\Standard;
use Symfony\Component\Filesystem\Filesystem;
Expand All @@ -36,12 +37,14 @@ final class Container
{
private Filesystem $filesystem;
private ConfigurationFactory $configFactory;
private ParserFactory $parserFactory;
private Parser $parser;
private ?PhpVersion $parserPhpVersion = null;
private ?PhpVersion $printerPhpVersion = null;
private Reflector $reflector;
private ScoperFactory $scoperFactory;
private EnrichedReflectorFactory $enrichedReflectorFactory;
private PrinterFactory $printerFactory;
private Printer $printer;

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

public function getScoperFactory(?PhpVersion $phpVersion = null): ScoperFactory
public function getScoperFactory(): ScoperFactory
{
if (!isset($this->scoperFactory)) {
$this->scoperFactory = new ScoperFactory(
$this->getParser($phpVersion),
$this->scoperFactory = new StandardScoperFactory(
$this->getEnrichedReflectorFactory(),
$this->getPrinter(),
$this->getParserFactory(),
$this->getPrinterFactory(),
);
}

return $this->scoperFactory;
}

/**
* @deprecated Use ::getParserFactory() instead.
*/
public function getParser(?PhpVersion $phpVersion = null): Parser
{
if (!isset($this->parser)) {
$this->parserPhpVersion = $phpVersion;
$this->parser = $this->createParser($phpVersion);
$this->parser = $this->getParserFactory()->createParser($phpVersion);
}

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

return $this->parser;
}

private function createParser(?PhpVersion $phpVersion): Parser
public function getParserFactory(): ParserFactory
{
$version = $phpVersion ?? PhpVersion::getHostVersion();
$lexer = $version->isHostVersion() ? new Lexer() : new Emulative($version);
if (!isset($this->parserFactory)) {
$this->parserFactory = new StandardParserFactory();
}

return $version->id >= 80_000
? new Php8($lexer, $version)
: new Php7($lexer, $version);
return $this->parserFactory;
}

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

/**
* @deprecated use ::getPrinterFactory() instead.
*/
public function getPrinter(?PhpVersion $phpVersion = null): Printer
{
if (!isset($this->printer)) {
Expand All @@ -138,6 +146,15 @@ public function getPrinter(?PhpVersion $phpVersion = null): Printer
return $this->printer;
}

public function getPrinterFactory(): PrinterFactory
{
if (!isset($this->printerFactory)) {
$this->printerFactory = new StandardPrinterFactory();
}

return $this->printerFactory;
}

private static function checkSamePhpVersion(
?PhpVersion $versionUsed,
?PhpVersion $versionRequest,
Expand Down
23 changes: 23 additions & 0 deletions src/PhpParser/Parser/ParserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\PhpParser\Parser;

use PhpParser\Parser;
use PhpParser\PhpVersion;

interface ParserFactory
{
public function createParser(?PhpVersion $phpVersion = null): Parser;
}
38 changes: 38 additions & 0 deletions src/PhpParser/Parser/StandardParserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\PhpParser\Parser;

use PhpParser\Lexer;
use PhpParser\Lexer\Emulative;
use PhpParser\Parser;
use PhpParser\Parser\Php7;
use PhpParser\Parser\Php8;
use PhpParser\PhpVersion;

final class StandardParserFactory implements ParserFactory
{
public function createParser(?PhpVersion $phpVersion = null): Parser
{
$version = $phpVersion ?? PhpVersion::getHostVersion();

$lexer = $version->isHostVersion()
? new Lexer()
: new Emulative($version);

return $version->id >= 80_000
? new Php8($lexer, $version)
: new Php7($lexer, $version);
}
}
22 changes: 22 additions & 0 deletions src/PhpParser/Printer/PrinterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\PhpParser\Printer;

use PhpParser\PhpVersion;

interface PrinterFactory
{
public function createPrinter(?PhpVersion $phpVersion = null): Printer;
}
30 changes: 30 additions & 0 deletions src/PhpParser/Printer/StandardPrinterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\PhpParser\Printer;

use PhpParser\PhpVersion;
use PhpParser\PrettyPrinter\Standard;

final class StandardPrinterFactory implements PrinterFactory
{
public function createPrinter(?PhpVersion $phpVersion = null): Printer
{
return new StandardPrinter(
new Standard([
'phpVersion' => $phpVersion,
]),
);
}
}
29 changes: 29 additions & 0 deletions src/Scoper/Factory/ScoperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\Scoper\Factory;

use Humbug\PhpScoper\Configuration\Configuration;
use Humbug\PhpScoper\Scoper\Scoper;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\PhpVersion;

interface ScoperFactory
{
public function createScoper(
Configuration $configuration,
SymbolsRegistry $symbolsRegistry,
?PhpVersion $phpVersion = null,
): Scoper;
}
85 changes: 85 additions & 0 deletions src/Scoper/Factory/StandardScoperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\Scoper\Factory;

use Humbug\PhpScoper\Configuration\Configuration;
use Humbug\PhpScoper\PhpParser\Parser\ParserFactory;
use Humbug\PhpScoper\PhpParser\Printer\PrinterFactory;
use Humbug\PhpScoper\PhpParser\TraverserFactory;
use Humbug\PhpScoper\Scoper\Composer\AutoloadPrefixer;
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper;
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper;
use Humbug\PhpScoper\Scoper\NullScoper;
use Humbug\PhpScoper\Scoper\PatchScoper;
use Humbug\PhpScoper\Scoper\PhpScoper;
use Humbug\PhpScoper\Scoper\Scoper;
use Humbug\PhpScoper\Scoper\SymfonyScoper;
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use PhpParser\PhpVersion;

final readonly class StandardScoperFactory implements ScoperFactory
{
public function __construct(
private EnrichedReflectorFactory $enrichedReflectorFactory,
private ParserFactory $parserFactory,
private PrinterFactory $printerFactory,
) {
}

public function createScoper(
Configuration $configuration,
SymbolsRegistry $symbolsRegistry,
?PhpVersion $phpVersion = null,
): Scoper {
$prefix = $configuration->getPrefix();
$symbolsConfiguration = $configuration->getSymbolsConfiguration();
$enrichedReflector = $this->enrichedReflectorFactory->create($symbolsConfiguration);

$parser = $this->parserFactory->createParser($phpVersion);
$printer = $this->printerFactory->createPrinter($phpVersion);

$autoloadPrefixer = new AutoloadPrefixer(
$prefix,
$enrichedReflector,
);

return new PatchScoper(
new PhpScoper(
$parser,
new JsonFileScoper(
new InstalledPackagesScoper(
new SymfonyScoper(
new NullScoper(),
$prefix,
$enrichedReflector,
$symbolsRegistry,
),
$autoloadPrefixer,
),
$autoloadPrefixer,
),
new TraverserFactory(
$enrichedReflector,
$prefix,
$symbolsRegistry,
),
$printer,
),
$prefix,
$configuration->getPatcher(),
);
}
}
Loading

0 comments on commit fbbcac0

Please sign in to comment.