Skip to content

Commit

Permalink
Remove ParserFactory::create()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Nov 12, 2023
1 parent b54302f commit 3640d18
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 35 deletions.
28 changes: 0 additions & 28 deletions lib/PhpParser/ParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions test/PhpParser/ParserFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 3640d18

Please sign in to comment.