From 3640d18b87c00d8cec6564f1b3a5f72c3cf7d229 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 12 Nov 2023 16:51:30 +0100 Subject: [PATCH] Remove ParserFactory::create() Don't try to keep backwards-compatibility with the old factory style, which doesn't map cleanly onto supported options (we only have ONLY_PHP7/PREFER_PHP7, which should probably create a Php8 parser in terms of how they are used, but this would no longer match their names). Instead, I have backported the new createForNewestSupportedVersion() and createForHostVersion() methods to PHP-Parser 4. --- lib/PhpParser/ParserFactory.php | 28 ---------------------------- test/PhpParser/ParserFactoryTest.php | 7 ------- 2 files changed, 35 deletions(-) diff --git a/lib/PhpParser/ParserFactory.php b/lib/PhpParser/ParserFactory.php index b70991808d..3a7586ea29 100644 --- a/lib/PhpParser/ParserFactory.php +++ b/lib/PhpParser/ParserFactory.php @@ -6,34 +6,6 @@ use PhpParser\Parser\Php8; class ParserFactory { - public const PREFER_PHP7 = 1; - public const ONLY_PHP7 = 3; - - /** - * Creates a Parser instance, according to the provided kind. - * - * @param int $kind One of ::PREFER_PHP7 or ::ONLY_PHP7 - * @param Lexer|null $lexer Lexer to use. Defaults to emulative lexer when not specified - * - * @return Parser The parser instance - * - * @deprecated Use createForVersion(), createForNewestSupportedVersion() or createForHostVersion() instead. - */ - public function create(int $kind, ?Lexer $lexer = null): Parser { - if (null === $lexer) { - $lexer = new Lexer\Emulative(); - } - switch ($kind) { - case self::PREFER_PHP7: - case self::ONLY_PHP7: - return new Parser\Php7($lexer); - default: - throw new \LogicException( - 'Kind must be one of ::PREFER_PHP7 or ::ONLY_PHP7' - ); - } - } - /** * Create a parser targeting the given version on a best-effort basis. The parser will generally * accept code for the newest supported version, but will try to accommodate code that becomes diff --git a/test/PhpParser/ParserFactoryTest.php b/test/PhpParser/ParserFactoryTest.php index c3ca3863fe..e0f5697a1d 100644 --- a/test/PhpParser/ParserFactoryTest.php +++ b/test/PhpParser/ParserFactoryTest.php @@ -11,13 +11,6 @@ class ParserFactoryTest extends \PHPUnit\Framework\TestCase { public function testCreate() { $factory = new ParserFactory(); - - $lexer = new Lexer(); - $this->assertInstanceOf(Php7::class, $factory->create(ParserFactory::PREFER_PHP7, $lexer)); - $this->assertInstanceOf(Php7::class, $factory->create(ParserFactory::ONLY_PHP7, $lexer)); - $this->assertInstanceOf(Php7::class, $factory->create(ParserFactory::PREFER_PHP7)); - $this->assertInstanceOf(Php7::class, $factory->create(ParserFactory::ONLY_PHP7)); - $this->assertInstanceOf(Php8::class, $factory->createForNewestSupportedVersion()); $this->assertInstanceOf(Parser::class, $factory->createForHostVersion()); }