Skip to content

Commit

Permalink
Add PHP-Parser 5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Jan 8, 2024
1 parent 77a4db8 commit 99100b8
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [v3.4.0]
- Add PHP-Parser 5 support

## [v3.3.0]
- Test against PHP 8.3
- Add Symfony 7 support
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"php": "^7.4 || ^8.0",
"symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0",
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0",
"nikic/php-parser": "^4.13",
"nikic/php-parser": "^4.18 || ^5.0",
"php-parallel-lint/php-console-highlighter": "^1.0",
"phpunit/php-timer": "^2.0||^3.0||^4.0||^5.0||^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^2.8.1||^3.5"
},
"autoload": {
Expand Down
16 changes: 1 addition & 15 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
use function gettype;
use InvalidArgumentException;
use function is_object;
use const PHP_VERSION;
use Povils\PHPMND\PhpParser\FileParser;
use PhpParser\Lexer;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use function sprintf;
use function version_compare;

class Container
{
Expand All @@ -34,14 +31,8 @@ private function __construct(array $values)
public static function create(): self
{
return new self([
Lexer::class => static function (self $container): Lexer {
// For PHP < 8.0 we want to specify a lexer object.
// Otherwise, the code creates a `Lexer\Emulative()` instance, which by default uses PHP 8 compatibility
// with e.g. longer list of reserved keywords
return version_compare('8.0', PHP_VERSION, '<') ? new Lexer() : new Lexer\Emulative();
},
Parser::class => static function (self $container): Parser {
return (new ParserFactory())->create(ParserFactory::PREFER_PHP7, $container->getLexer());
return (new ParserFactory())->createForHostVersion();
},
FileParser::class => static function (self $container): FileParser {
return new FileParser($container->getParser());
Expand All @@ -59,11 +50,6 @@ private function getParser(): Parser
return $this->get(Parser::class);
}

private function getLexer(): Lexer
{
return $this->get(Lexer::class);
}

private function offsetSet(string $id, Closure $value): void
{
$this->keys[$id] = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function setUp(): void
$this->hintList = new HintList();

$this->detector = new Detector(
new FileParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7)),
new FileParser((new ParserFactory())->createForHostVersion()),
$this->option,
$this->hintList
);
Expand Down
12 changes: 2 additions & 10 deletions tests/PhpParser/FileParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testItParsesTheGivenFileOnlyOnce(): void
*/
public function testItCanParseFile(SplFileInfo $fileInfo, string $expectedPrintedParsedContents): void
{
$statements = (new FileParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7)))->parse($fileInfo);
$statements = (new FileParser((new ParserFactory())->createForHostVersion()))->parse($fileInfo);

foreach ($statements as $statement) {
$this->assertInstanceOf(Node::class, $statement);
Expand All @@ -61,7 +61,7 @@ public function testItCanParseFile(SplFileInfo $fileInfo, string $expectedPrinte

public function testItThrowsUponFailure(): void
{
$parser = new FileParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7));
$parser = new FileParser((new ParserFactory())->createForHostVersion());

try {
$parser->parse(self::createFileInfo('/unknown', '<?php use foo as self;'));
Expand Down Expand Up @@ -145,12 +145,4 @@ public function getContents(): string
}
};
}

private function normalizeString(string $string): string
{
return implode(
"\n",
array_map('rtrim', explode("\n", $string))
);
}
}
2 changes: 1 addition & 1 deletion tests/PhpParser/Visitor/BaseVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class BaseVisitorTest extends TestCase
*/
final protected function parseCode(string $code): array
{
return (array) (new ParserFactory())->create(ParserFactory::PREFER_PHP7)->parse($code);
return (array) (new ParserFactory())->createForHostVersion()->parse($code);
}

/**
Expand Down

0 comments on commit 99100b8

Please sign in to comment.