Skip to content

Commit 72cf9e2

Browse files
committed
added Literal::new() [Closes #130]
1 parent 163c5e9 commit 72cf9e2

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

readme.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,13 @@ new Literal('substr(?, ?)', [$a, $b]);
582582
// generates, for example: substr('hello', 5);
583583
```
584584

585+
The literal representing the creation of a new object is easily generated by the `new` method:
586+
587+
```php
588+
Literal::new(Demo::class, [$a, 'foo' => $b]);
589+
// generates, for example: new Demo(10, foo: 20)
590+
```
591+
585592

586593
Attributes
587594
----------
@@ -590,10 +597,15 @@ You can add PHP 8 attributes to all classes, methods, properties, constants, enu
590597

591598
```php
592599
$class = new Nette\PhpGenerator\ClassType('Demo');
593-
$class->addAttribute('Deprecated');
600+
$class->addAttribute('Table', [
601+
'name' => 'user',
602+
'constraints' => [
603+
Literal::new('UniqueConstraint', ['name' => 'ean', 'columns' => ['ean']]),
604+
],
605+
]);
594606

595607
$class->addProperty('list')
596-
->addAttribute('WithArguments', [1, 2]);
608+
->addAttribute('Deprecated');
597609

598610
$method = $class->addMethod('count')
599611
->addAttribute('Foo\Cached', ['mode' => true]);
@@ -607,16 +619,18 @@ echo $class;
607619
Result:
608620

609621
```php
610-
#[Deprecated]
622+
#[Table(name: 'user', constraints: [new UniqueConstraint(name: 'ean', columns: ['ean'])])]
611623
class Demo
612624
{
613-
#[WithArguments(1, 2)]
625+
#[Deprecated]
614626
public $list;
615627

616628

617629
#[Foo\Cached(mode: true)]
618-
public function count(#[Bar] $items)
619-
{
630+
public function count(
631+
#[Bar]
632+
$items,
633+
) {
620634
}
621635
}
622636
```

src/PhpGenerator/Literal.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
*/
1616
class Literal
1717
{
18+
/**
19+
* Creates a literal representing the creation of an object using the new operator.
20+
*/
21+
public static function new(string $class, array $args = []): self
22+
{
23+
return new self('new ' . $class . '(...?:)', [$args]);
24+
}
25+
26+
1827
public function __construct(
1928
private string $value,
2029
/** @var ?mixed[] */

tests/PhpGenerator/ClassType.attributes.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ $class
1818
->addComment('Description of class.')
1919
->addAttribute('ExampleAttribute')
2020
->addAttribute('WithArgument', [new Literal('Foo::BAR')])
21-
->addAttribute('NamedArguments', ['foo' => 'bar', 'bar' => [1, 2, 3]]);
21+
->addAttribute('Table', [
22+
'name' => 'user',
23+
'constraints' => [
24+
Literal::new('UniqueConstraint', ['name' => 'ean', 'columns' => ['ean']]),
25+
],
26+
]);
2227

2328
$class->addConstant('FOO', 123)
2429
->addComment('Commented')

tests/PhpGenerator/ClassType.promotion.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $method->addPromotedParameter('c')
1818
->addComment('promo')
1919
->addAttribute('Example');
2020

21-
$method->addPromotedParameter('d', new Literal('new Draft(?)', [10]))
21+
$method->addPromotedParameter('d', Literal::new('Draft', [10]))
2222
->setType('Draft')
2323
->setReadOnly();
2424

tests/PhpGenerator/Dumper.dump().phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ Assert::same("[strlen('hello')]", $dumper->dump([new Literal('strlen(?)', ['hell
5151
Assert::same("a\nb", $dumper->dump(new Literal("a\r\nb")));
5252

5353

54+
// Literal::new
55+
Assert::same('new stdClass()', $dumper->dump(Literal::new('stdClass')));
56+
Assert::same('new stdClass(10, 20)', $dumper->dump(Literal::new('stdClass', [10, 20])));
57+
Assert::same('new stdClass(10, c: 20)', $dumper->dump(Literal::new('stdClass', [10, 'c' => 20])));
58+
59+
5460
// arrays
5561
Assert::same('[]', $dumper->dump([]));
5662
Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3]));

tests/PhpGenerator/expected/ClassType.attributes.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
#[ExampleAttribute]
55
#[WithArgument(Foo::BAR)]
6-
#[NamedArguments(foo: 'bar', bar: [1, 2, 3])]
6+
#[Table(name: 'user', constraints: [new UniqueConstraint(name: 'ean', columns: ['ean'])])]
77
class Example
88
{
99
/** Commented */

0 commit comments

Comments
 (0)