From 2fc10fe03ef95ec7a71020c1d75c6467deacd6e2 Mon Sep 17 00:00:00 2001 From: Kayw Date: Tue, 2 Apr 2024 14:54:49 +0800 Subject: [PATCH] Support the `nikic/php-parser` to 5.x --- .gitignore | 1 + composer.json | 2 +- phpstan-baseline.neon | 4 ---- phpstan.neon.dist | 3 +++ src/EnvyServiceProvider.php | 4 ++-- .../AppendEnvironmentVariablesNodeVisitor.php | 5 +++- src/Support/PhpParser/EnvCallNodeVisitor.php | 24 ++++++++++++++----- stubs/ArrayItem.stub | 11 +++++++++ stubs/Array_.stub | 13 ++++++++++ tests/Pest.php | 2 +- .../AddEnvironmentVariablesToListTest.php | 7 +++++- .../Unit/Actions/FindEnvironmentCallsTest.php | 7 +++++- 12 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 stubs/ArrayItem.stub create mode 100644 stubs/Array_.stub diff --git a/.gitignore b/.gitignore index 9a43686..23b44d4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ testbench.yaml vendor node_modules .php-cs-fixer.cache +.phpunit.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 68958bb..29d4c75 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require": { "php": "^8.2", "illuminate/contracts": "^10.0 || ^11.0", - "nikic/php-parser": "^4.15", + "nikic/php-parser": "^4.19.1 || ^5.0.2", "nunomaduro/termwind": "^1.15 || ^2.0", "spatie/laravel-package-tools": "^1.16", "thecodingmachine/safe": "^2.4" diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index bd038ff..ab191c3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,7 +10,3 @@ parameters: count: 1 path: src/Commands/SyncCommand.php - - - message: "#^Parameter \\#1 \\$value of function strval expects bool\\|float\\|int\\|resource\\|string\\|null, mixed given\\.$#" - count: 1 - path: src/Support/PhpParser/EnvCallNodeVisitor.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index cddc118..5aa8d5c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -10,3 +10,6 @@ parameters: checkModelProperties: true parallel: processTimeout: 300.0 + stubFiles: + - stubs/Array_.stub + - stubs/ArrayItem.stub diff --git a/src/EnvyServiceProvider.php b/src/EnvyServiceProvider.php index c272884..cf4efb6 100644 --- a/src/EnvyServiceProvider.php +++ b/src/EnvyServiceProvider.php @@ -45,7 +45,7 @@ public function packageRegistered(): void $this->app->bind( FindsEnvironmentCalls::class, fn(Application $app) => $app->make(FindEnvironmentCalls::class, [ - 'parser' => (new ParserFactory())->create(ParserFactory::PREFER_PHP7), + 'parser' => (new ParserFactory())->createForNewestSupportedVersion(), ]) ); $this->app->bind(ReadsEnvironmentFile::class, ReadEnvironmentFile::class); @@ -65,7 +65,7 @@ public function packageRegistered(): void $this->app->bind( AddsEnvironmentVariablesToList::class, fn (Application $app) => $app->make(AddEnvironmentVariablesToList::class, [ - 'parser' => (new ParserFactory())->create(ParserFactory::PREFER_PHP7), + 'parser' => (new ParserFactory())->createForNewestSupportedVersion(), ]) ); $this->app->bind( diff --git a/src/Support/PhpParser/AppendEnvironmentVariablesNodeVisitor.php b/src/Support/PhpParser/AppendEnvironmentVariablesNodeVisitor.php index 31b3d4a..9c0ed48 100644 --- a/src/Support/PhpParser/AppendEnvironmentVariablesNodeVisitor.php +++ b/src/Support/PhpParser/AppendEnvironmentVariablesNodeVisitor.php @@ -6,6 +6,7 @@ use Illuminate\Support\Collection; use PhpParser\Node; +use PhpParser\Node\Expr\ArrayItem; use PhpParser\NodeVisitorAbstract; use Worksome\Envy\Support\EnvironmentVariable; @@ -41,7 +42,9 @@ public function leaveNode(Node $node) } $this->updates->each(function (EnvironmentVariable $variable) use (&$node) { - $node->items[] = new Node\Expr\ArrayItem(new Node\Scalar\String_($variable->getKey())); + $item = new ArrayItem(new Node\Scalar\String_($variable->getKey())); + + $node->items[] = $item; }); $this->variablesWereAppended = true; diff --git a/src/Support/PhpParser/EnvCallNodeVisitor.php b/src/Support/PhpParser/EnvCallNodeVisitor.php index d3f034e..9b958f6 100644 --- a/src/Support/PhpParser/EnvCallNodeVisitor.php +++ b/src/Support/PhpParser/EnvCallNodeVisitor.php @@ -123,14 +123,24 @@ private function getComment(Node\Expr\FuncCall $node): string|null return null; } - /** @var array|null $comments */ - $comments = $previousNode->getAttribute('comments'); + /** @var \PhpParser\Node\Expr\ArrayItem $parent */ + $parent = $previousNode->getAttribute('parent'); - if ($comments === null || count($comments) === 0) { - return null; + if ($parent->hasAttribute('comments')) { + /** @var list|null $comments */ + $comments = $parent->getAttribute('comments'); + + if ($comments === null || count($comments) === 0) { + return null; + } + + /** @var string $comment */ + $comment = $comments[0]->getReformattedText(); + + return $comment; } - return strval($comments[0]->getReformattedText()); + return null; } private function print(Node $node): string @@ -148,6 +158,8 @@ private function isBoolean(Node\Expr $providedDefault): bool return false; } - return in_array($providedDefault->name->parts[0], ['true', 'false'], true); + $name = $providedDefault->name->name ?? $providedDefault->name->getParts()[0]; + + return in_array($name, ['true', 'false'], true); } } diff --git a/stubs/ArrayItem.stub b/stubs/ArrayItem.stub new file mode 100644 index 0000000..d535a32 --- /dev/null +++ b/stubs/ArrayItem.stub @@ -0,0 +1,11 @@ +create(ParserFactory::PREFER_PHP7); + return (new ParserFactory())->createForNewestSupportedVersion(); } function readEnvironmentFile(string $filePath = null): Collection diff --git a/tests/Unit/Actions/AddEnvironmentVariablesToListTest.php b/tests/Unit/Actions/AddEnvironmentVariablesToListTest.php index 92466ca..66ec5ea 100644 --- a/tests/Unit/Actions/AddEnvironmentVariablesToListTest.php +++ b/tests/Unit/Actions/AddEnvironmentVariablesToListTest.php @@ -30,10 +30,15 @@ it('performs no changes if the parser returns null', function (string $list) { $parser = new class() implements Parser { - public function parse(string $code, ErrorHandler $errorHandler = null) + public function parse(string $code, ?ErrorHandler $errorHandler = null): ?array { return null; } + + public function getTokens(): array + { + return []; + } }; $action = new AddEnvironmentVariablesToList($parser, new TestFinder()); diff --git a/tests/Unit/Actions/FindEnvironmentCallsTest.php b/tests/Unit/Actions/FindEnvironmentCallsTest.php index 3cc03a8..9fc0d62 100644 --- a/tests/Unit/Actions/FindEnvironmentCallsTest.php +++ b/tests/Unit/Actions/FindEnvironmentCallsTest.php @@ -88,10 +88,15 @@ function (bool $excludeVariablesWithDefaults, int $expectedCount) { it('returns an empty collection if the parser returns null', function () { $parser = new class() implements Parser { - public function parse(string $code, ErrorHandler $errorHandler = null) + public function parse(string $code, ?ErrorHandler $errorHandler = null): ?array { return null; } + + public function getTokens(): array + { + return []; + } }; $action = new FindEnvironmentCalls($parser);