Skip to content

Commit

Permalink
Merge pull request #41 from kayw-geek/feature/upgrade-php-parser
Browse files Browse the repository at this point in the history
Support the `nikic/php-parser` to 5.x
  • Loading branch information
owenvoke authored Apr 18, 2024
2 parents 2468219 + 2fc10fe commit d9c9750
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ testbench.yaml
vendor
node_modules
.php-cs-fixer.cache
.phpunit.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ parameters:
checkModelProperties: true
parallel:
processTimeout: 300.0
stubFiles:
- stubs/Array_.stub
- stubs/ArrayItem.stub
4 changes: 2 additions & 2 deletions src/EnvyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Collection;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\NodeVisitorAbstract;
use Worksome\Envy\Support\EnvironmentVariable;

Expand Down Expand Up @@ -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;
Expand Down
24 changes: 18 additions & 6 deletions src/Support/PhpParser/EnvCallNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,24 @@ private function getComment(Node\Expr\FuncCall $node): string|null
return null;
}

/** @var array<int, Comment>|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<Comment>|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
Expand All @@ -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);
}
}
11 changes: 11 additions & 0 deletions stubs/ArrayItem.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare (strict_types=1);

namespace PhpParser\Node\Expr;

use PhpParser\Node\Expr;

class ArrayItem
{
}
13 changes: 13 additions & 0 deletions stubs/Array_.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace PhpParser\Node\Expr;

use PhpParser\Node\ArrayItem;

class Array_ {

/** @var \PhpParser\Node\Expr\ArrayItem[] $items */
public array $items;
}
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

function defaultPhpParser(): Parser
{
return (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
return (new ParserFactory())->createForNewestSupportedVersion();
}

function readEnvironmentFile(string $filePath = null): Collection
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/Actions/AddEnvironmentVariablesToListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/Actions/FindEnvironmentCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d9c9750

Please sign in to comment.