|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Nette\PhpGenerator\Literal; |
| 6 | +use Nette\PhpGenerator\Printer; |
| 7 | +use Tester\Assert; |
| 8 | + |
| 9 | +require __DIR__ . '/../bootstrap.php'; |
| 10 | + |
| 11 | + |
| 12 | +$printer = new Printer; |
| 13 | +$printer->singleParameterOnOneLine = true; |
| 14 | + |
| 15 | + |
| 16 | +$function = new Nette\PhpGenerator\GlobalFunction('singleFunction'); |
| 17 | +$function |
| 18 | + ->setReturnType('array') |
| 19 | + ->addParameter('foo') |
| 20 | + ->addAttribute('Foo'); |
| 21 | + |
| 22 | +Assert::match(<<<'XX' |
| 23 | + function singleFunction(#[Foo] $foo): array |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + XX, $printer->printFunction($function)); |
| 28 | + |
| 29 | + |
| 30 | +$method = new Nette\PhpGenerator\Method('singleMethod'); |
| 31 | +$method |
| 32 | + ->setPublic() |
| 33 | + ->setReturnType('array') |
| 34 | + ->addParameter('foo') |
| 35 | + ->addAttribute('Foo'); |
| 36 | + |
| 37 | +Assert::match(<<<'XX' |
| 38 | + public function singleMethod(#[Foo] $foo): array |
| 39 | + { |
| 40 | + } |
| 41 | + |
| 42 | + XX, $printer->printMethod($method)); |
| 43 | + |
| 44 | + |
| 45 | +$method = new Nette\PhpGenerator\Method('singleMethod'); |
| 46 | +$method |
| 47 | + ->setPublic() |
| 48 | + ->setReturnType('array') |
| 49 | + ->addPromotedParameter('foo') |
| 50 | + ->setPublic(); |
| 51 | + |
| 52 | +Assert::match(<<<'XX' |
| 53 | + public function singleMethod(public $foo): array |
| 54 | + { |
| 55 | + } |
| 56 | + |
| 57 | + XX, $printer->printMethod($method)); |
| 58 | + |
| 59 | + |
| 60 | +$method = new Nette\PhpGenerator\Method('singleMethod'); |
| 61 | +$method |
| 62 | + ->setPublic() |
| 63 | + ->setReturnType('array') |
| 64 | + ->addPromotedParameter('looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong') |
| 65 | + ->setPublic(); |
| 66 | + |
| 67 | +Assert::match(<<<'XX' |
| 68 | + public function singleMethod( |
| 69 | + public $looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong, |
| 70 | + ): array |
| 71 | + { |
| 72 | + } |
| 73 | + |
| 74 | + XX, $printer->printMethod($method)); |
| 75 | + |
| 76 | + |
| 77 | +$method = new Nette\PhpGenerator\Method('singleMethod'); |
| 78 | +$method->addParameter('foo') |
| 79 | + ->addAttribute('Foo', [new Literal("'\n'")]); |
| 80 | + |
| 81 | +Assert::match(<<<'XX' |
| 82 | + function singleMethod( |
| 83 | + #[Foo(' |
| 84 | + ')] |
| 85 | + $foo, |
| 86 | + ) { |
| 87 | + } |
| 88 | + XX, $printer->printMethod($method)); |
0 commit comments