Skip to content

Commit

Permalink
Bump nikic parser version (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlakomski authored Oct 15, 2024
1 parent 559b9d8 commit ed6f847
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 14 additions & 12 deletions src/Generator/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,21 +34,21 @@
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;
use function get_class;
use function in_array;
use function sprintf;
use function ucfirst;

use const PHP_EOL;

final class Renderer
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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';

Expand All @@ -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_
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/AstAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions tests/Usage/symfony/src/Query/UserQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserRow> */
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]);
}
Expand Down

0 comments on commit ed6f847

Please sign in to comment.