From 849a038a6d336fefc9fb36fd47270dbb92cc2766 Mon Sep 17 00:00:00 2001 From: Moritz Bischof <59830009+19bischof@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:08:32 +0200 Subject: [PATCH 1/5] Attribute Printer bug fix now also allows falsy values as parameters for Attributes i.e. a single 0 --- src/PhpGenerator/Printer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 40485a57..9fc18c9a 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -422,7 +422,7 @@ protected function printAttributes(array $attrs, bool $inline = false): string foreach ($attrs as $attr) { $args = $this->dumper->format('...?:', $attr->getArguments()); $args = Helpers::simplifyTaggedNames($args, $this->namespace); - $items[] = $this->printType($attr->getName(), nullable: false) . ($args ? "($args)" : ''); + $items[] = $this->printType($attr->getName(), nullable: false) . ($args === '' ? "($args)" : ''); $inline = $inline && !str_contains($args, "\n"); } From 8f094bb610f94748b76c6f0acf90f0e9879f0488 Mon Sep 17 00:00:00 2001 From: Moritz Bischof <59830009+19bischof@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:48:59 +0200 Subject: [PATCH 2/5] Printer Attribute Test tests if attributes can have a falsy value as parameter --- tests/PhpGenerator/Printer.attribute.phpt | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/PhpGenerator/Printer.attribute.phpt diff --git a/tests/PhpGenerator/Printer.attribute.phpt b/tests/PhpGenerator/Printer.attribute.phpt new file mode 100644 index 00000000..72255b19 --- /dev/null +++ b/tests/PhpGenerator/Printer.attribute.phpt @@ -0,0 +1,27 @@ +addAttribute(MyAttribute::class, [0]); + +Assert::same('#[MyAttribute(0)] +class Classy +{ +} +', $printer->printClass($classy)); From 9a872910bf1ff32f8daa3e68df9e00acf4fa9ef1 Mon Sep 17 00:00:00 2001 From: "moritz.bischof" Date: Thu, 28 Sep 2023 14:52:39 +0200 Subject: [PATCH 3/5] fix attribute evaluation --- src/PhpGenerator/Printer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 9fc18c9a..469bf95f 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -422,7 +422,7 @@ protected function printAttributes(array $attrs, bool $inline = false): string foreach ($attrs as $attr) { $args = $this->dumper->format('...?:', $attr->getArguments()); $args = Helpers::simplifyTaggedNames($args, $this->namespace); - $items[] = $this->printType($attr->getName(), nullable: false) . ($args === '' ? "($args)" : ''); + $items[] = $this->printType($attr->getName(), nullable: false) . ($args !== '' ? "($args)" : ''); $inline = $inline && !str_contains($args, "\n"); } From 48d72c3f06584995275004790e7aebb0bb942824 Mon Sep 17 00:00:00 2001 From: "moritz.bischof" Date: Thu, 28 Sep 2023 14:53:50 +0200 Subject: [PATCH 4/5] remove unnecessary use --- tests/PhpGenerator/Printer.attribute.phpt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/PhpGenerator/Printer.attribute.phpt b/tests/PhpGenerator/Printer.attribute.phpt index 72255b19..970b5626 100644 --- a/tests/PhpGenerator/Printer.attribute.phpt +++ b/tests/PhpGenerator/Printer.attribute.phpt @@ -3,7 +3,6 @@ declare(strict_types=1); use Nette\PhpGenerator\ClassType; -use Nette\PhpGenerator\Literal; use Nette\PhpGenerator\Printer; use Tester\Assert; From f9fa42746acef1131d343e8f0ea428b4ae9ffb60 Mon Sep 17 00:00:00 2001 From: "moritz.bischof" Date: Thu, 28 Sep 2023 14:56:44 +0200 Subject: [PATCH 5/5] refactor file --- tests/PhpGenerator/Printer.attribute.phpt | 27 ++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/tests/PhpGenerator/Printer.attribute.phpt b/tests/PhpGenerator/Printer.attribute.phpt index 970b5626..a9176add 100644 --- a/tests/PhpGenerator/Printer.attribute.phpt +++ b/tests/PhpGenerator/Printer.attribute.phpt @@ -3,24 +3,21 @@ declare(strict_types=1); use Nette\PhpGenerator\ClassType; -use Nette\PhpGenerator\Printer; use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -#[Attribute] -class MyAttribute -{ -} - - -$printer = new Printer; $classy = (new ClassType('Classy')) - ->addAttribute(MyAttribute::class, [0]); - -Assert::same('#[MyAttribute(0)] -class Classy -{ -} -', $printer->printClass($classy)); + ->addAttribute('MyAttribute', [0]); + +same( + <<<'XX' + #[MyAttribute(0)] + class Classy + { + } + + XX, + (string) $classy, +);