From ed6f8477efe2433def4a20b2c041a115b8018bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20=C5=81akomski?= Date: Tue, 15 Oct 2024 14:36:52 +0200 Subject: [PATCH] Bump nikic parser version (#35) --- composer.json | 2 +- src/Generator/Renderer.php | 26 +++++++++++---------- tests/Generator/AstAssertions.php | 2 +- tests/Usage/symfony/src/Query/UserQuery.php | 12 +++++----- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index 067426d..9964a1e 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "symfony/process": "^6.0|^7.0", "phpdocumentor/reflection-docblock": "^5.2", "open-serializer/type": "^0.1", - "nikic/php-parser": "^4.10" + "nikic/php-parser": "^5.0" }, "require-dev": { "phpstan/phpstan": "^1.7", diff --git a/src/Generator/Renderer.php b/src/Generator/Renderer.php index af6fa10..2b697f0 100644 --- a/src/Generator/Renderer.php +++ b/src/Generator/Renderer.php @@ -10,11 +10,13 @@ use OpenSerializer\Type\TypeInfo; use PhpParser\Builder\Use_; use PhpParser\BuilderFactory; +use PhpParser\Modifiers; use PhpParser\Node\Arg; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Const_; +use PhpParser\Node\DeclareItem; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; -use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\Coalesce; @@ -32,14 +34,13 @@ use PhpParser\Node\Scalar\EncapsedStringPart; use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; -use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\StaticVar; use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\Declare_; -use PhpParser\Node\Stmt\DeclareDeclare; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Static_; -use PhpParser\Node\Stmt\StaticVar; use PhpParser\PrettyPrinter\Standard; + use function array_map; use function array_push; use function array_unique; @@ -47,6 +48,7 @@ use function in_array; use function sprintf; use function ucfirst; + use const PHP_EOL; final class Renderer @@ -94,7 +96,7 @@ public function renderTableFile(Table $table): string array_map( static fn(Column $column) => new ClassConst( [new Const_($column->nameConst(), new String_($column->dbName()))], - Class_::MODIFIER_PUBLIC + Modifiers::PUBLIC ), $table->columns(), ) @@ -139,7 +141,7 @@ public function renderTableFile(Table $table): string $prettyPrinter = new Standard(); return $prettyPrinter->prettyPrintFile( - [new Declare_([new DeclareDeclare('strict_types', new LNumber(1))]), $node] + [new Declare_([new DeclareItem('strict_types', new LNumber(1))]), $node] ) . PHP_EOL; } @@ -195,7 +197,7 @@ function (Column $column) use ($table, $factory) { $prettyPrinter = new Standard(); return $prettyPrinter->prettyPrintFile( - [new Declare_([new DeclareDeclare('strict_types', new LNumber(1))]), $node] + [new Declare_([new DeclareItem('strict_types', new LNumber(1))]), $node] ) . PHP_EOL; } @@ -239,7 +241,7 @@ public function renderClientRow(AbstractPlatform $databasePlatform): string $prettyPrinter = new Standard(); return $prettyPrinter->prettyPrintFile( - [new Declare_([new DeclareDeclare('strict_types', new LNumber(1))]), $node] + [new Declare_([new DeclareItem('strict_types', new LNumber(1))]), $node] ) . PHP_EOL; } @@ -400,7 +402,7 @@ function (Column $column) use ($factory) { 'params' => [ $factory->param('raw')->setType('array')->getNode(), ], - 'returnType' => "{$table->name()}Row", + 'returnType' => new Name("{$table->name()}Row"), 'expr' => new MethodCall( new PropertyFetch(new Variable('this'), 'table'), 'createRow', @@ -442,12 +444,12 @@ function (Column $column) use ($factory) { $prettyPrinter = new Standard(); return $prettyPrinter->prettyPrintFile( - [new Declare_([new DeclareDeclare('strict_types', new LNumber(1))]), $node] + [new Declare_([new DeclareItem('strict_types', new LNumber(1))]), $node] ) . PHP_EOL; } /** @param string[] $uses */ - private function typeDef(Column $column, TypeInfo $type, array &$uses = []): string + private function typeDef(Column $column, TypeInfo $type, array &$uses = []): Name { $phpType = self::TYPE_RETURN[$column->type()] ?? 'string'; @@ -459,7 +461,7 @@ private function typeDef(Column $column, TypeInfo $type, array &$uses = []): str $uses[] = $class->fullName(); } - return sprintf('%s%s', $column->optional() ? '?' : '', $phpType); + return new Name(sprintf('%s%s', $column->optional() ? '?' : '', $phpType)); } private function valueReturn(Table $table, Column $column, TypeInfo $type): Return_ diff --git a/tests/Generator/AstAssertions.php b/tests/Generator/AstAssertions.php index 1c55de5..b921029 100644 --- a/tests/Generator/AstAssertions.php +++ b/tests/Generator/AstAssertions.php @@ -16,7 +16,7 @@ protected static function assertAstFilesEquals(string $expectedFile, string $gen protected static function assertAstEquals(string $expectedFile, string $generatedCode): void { - $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); + $parser = (new ParserFactory())->createForHostVersion(); $astExpected = $parser->parse(file_get_contents($expectedFile)); $ast = $parser->parse($generatedCode); diff --git a/tests/Usage/symfony/src/Query/UserQuery.php b/tests/Usage/symfony/src/Query/UserQuery.php index a7feec8..ae29d2d 100644 --- a/tests/Usage/symfony/src/Query/UserQuery.php +++ b/tests/Usage/symfony/src/Query/UserQuery.php @@ -16,29 +16,29 @@ public function __construct(DatabaseSelectBuilder $builder) $this->table = new UserTable(); parent::__construct($builder, $this->table); } - public function table() : UserTable + public function table(): UserTable { return $this->table; } /** @return iterable */ - public function all(string ...$fields) : iterable + public function all(string ...$fields): iterable { $builder = $this->builder()->select(...$fields ? $this->table->select(...$fields) : $this->table->selectAll()); return new RowIterator($builder, fn(array $raw): UserRow => $this->table->createRow($raw)); } - public function first() : ?UserRow + public function first(): ?UserRow { return [...$this->offsetLimit(0, 1)->all()][0] ?? null; } - public function single(int $id) : ?UserRow + public function single(int $id): ?UserRow { return $this->withId($id)->first(); } - public function withId(int $id) : self + public function withId(int $id): self { return $this->where("{$this->table->id()} = :id", ['id' => $id]); } - public function withName(string $name) : self + public function withName(string $name): self { return $this->where("{$this->table->name()} = :name", ['name' => $name]); }