From 4b8fe1c8b40ac6ccc7dc8c3a08093a71ecd962f9 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Fri, 10 May 2024 19:00:05 +0200 Subject: [PATCH] Upgrade to nikic/php-parser v5 --- CHANGELOG.md | 7 +++++++ composer.json | 2 +- .../ObjectExporter/ClosureExporter.php | 3 ++- tests/ExportClosureTest.php | 18 +++++++++--------- tests/VarExporterTest.php | 12 ++++++------ 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 847cae6..04e54d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## UNRELEASED (0.5.0) + +✨ **Compatibility** + +- Added compatibility with `nikic/php-parser` `5.x` +- Removed compatibility with `nikic/php-parser` `4.x` + ## [0.4.0](https://github.com/brick/varexporter/releases/tag/0.4.0) - 2023-09-01 Minimum PHP version is now `7.4`. No breaking changes. diff --git a/composer.json b/composer.json index 5153bd8..24126ea 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "license": "MIT", "require": { "php": "^7.4 || ^8.0", - "nikic/php-parser": "^4.0" + "nikic/php-parser": "^5.0" }, "require-dev": { "phpunit/phpunit": "^9.3", diff --git a/src/Internal/ObjectExporter/ClosureExporter.php b/src/Internal/ObjectExporter/ClosureExporter.php index 5ecdf10..d1883b1 100644 --- a/src/Internal/ObjectExporter/ClosureExporter.php +++ b/src/Internal/ObjectExporter/ClosureExporter.php @@ -14,6 +14,7 @@ use PhpParser\NodeVisitor\NameResolver; use PhpParser\Parser; use PhpParser\ParserFactory; +use PhpParser\PhpVersion; use ReflectionFunction; /** @@ -67,7 +68,7 @@ public function export(object $object, \ReflectionObject $reflectionObject, arra private function getParser() { if ($this->parser === null) { - $this->parser = (new ParserFactory)->create(ParserFactory::ONLY_PHP7); + $this->parser = (new ParserFactory)->createForVersion(PhpVersion::fromComponents(7, 4)); } return $this->parser; diff --git a/tests/ExportClosureTest.php b/tests/ExportClosureTest.php index c00ddb7..6c9464d 100644 --- a/tests/ExportClosureTest.php +++ b/tests/ExportClosureTest.php @@ -61,7 +61,7 @@ public function testExportNestedComplexClosure(): void [ (object) [ 'callback' => function () { - return function (\Brick\VarExporter\Tests\Classes\PublicPropertiesOnly $a, int $b, string &$c, string ...$d) : ?string { + return function (\Brick\VarExporter\Tests\Classes\PublicPropertiesOnly $a, int $b, string &$c, string ...$d): ?string { $a->foo += $b; $c = $a->bar; $a->bar = implode('', $d); @@ -91,8 +91,8 @@ public function testExportNamespacedCode(): void }; $expected = <<<'PHP' -function (\Brick\VarExporter\Tests\Classes\SetState $a) : array { - return ['callback' => function (\Brick\VarExporter\Tests\Classes\SetState $a) : \Brick\VarExporter\Tests\Classes\NoProperties { +function (\Brick\VarExporter\Tests\Classes\SetState $a): array { + return ['callback' => function (\Brick\VarExporter\Tests\Classes\SetState $a): \Brick\VarExporter\Tests\Classes\NoProperties { strlen(PHP_VERSION); \Brick\VarExporter\Dummy\Functions\imported_function(\Brick\VarExporter\Dummy\Constants\IMPORTED_CONSTANT); \Brick\VarExporter\Dummy\Functions\explicitly_namespaced_function(\Brick\VarExporter\Dummy\Constants\EXPLICITLY_NAMESPACED_CONSTANT); @@ -128,13 +128,13 @@ public function testExportClosureWithStringsContainingLikeBreaks(): void $a = 'Hello, World!'; $b = <<