Skip to content

Commit a9a4048

Browse files
committed
readme: improvements
1 parent 34aaf23 commit a9a4048

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

readme.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Let's start with a straightforward example of generating class using [ClassType]
3636
```php
3737
$class = new Nette\PhpGenerator\ClassType('Demo');
3838

39-
$class->setFinal()
39+
$class
40+
->setFinal()
4041
->setExtends(ParentClass::class)
4142
->addImplement(Countable::class)
4243
->addComment("Description of class.\nSecond line\n")
@@ -171,6 +172,7 @@ $methodRecount = $methodCount->cloneWithName('recount');
171172
$class->addMember($methodRecount);
172173
```
173174

175+
174176
Interface or Trait
175177
------------------
176178

@@ -267,6 +269,7 @@ $obj = new class ($val) {
267269
};
268270
```
269271

272+
270273
Global Function
271274
---------------
272275

@@ -292,6 +295,7 @@ function foo($a, $b)
292295
}
293296
```
294297

298+
295299
Closure
296300
-------
297301

@@ -318,6 +322,7 @@ function ($a, $b) use (&$c) {
318322
}
319323
```
320324

325+
321326
Arrow Function
322327
--------------
323328

@@ -338,6 +343,7 @@ Result:
338343
fn($a, $b) => $a + $b
339344
```
340345

346+
341347
Method and Function Signature
342348
-----------------------------
343349

@@ -377,6 +383,7 @@ function count(...$items)
377383
}
378384
```
379385

386+
380387
Method and Function Body
381388
------------------------
382389

@@ -466,7 +473,8 @@ function foo($a)
466473
}
467474
```
468475

469-
Printers and PSR compliance
476+
477+
Printers and PSR Compliance
470478
---------------------------
471479

472480
PHP code is generated by `Printer` objects. There is a `PsrPrinter` whose output conforms to PSR-2 and PSR-12 and uses spaces for indentation, and a `Printer` that uses tabs for indentation.
@@ -484,12 +492,19 @@ Need to customize printer behavior? Create your own by inheriting the `Printer`
484492
```php
485493
class MyPrinter extends Nette\PhpGenerator\Printer
486494
{
495+
/** length of the line after which the line will break */
487496
public int $wrapLength = 120;
497+
/** indentation character, can be replaced with a sequence of spaces */
488498
public string $indentation = "\t";
499+
/** number of blank lines between properties */
489500
public int $linesBetweenProperties = 0;
501+
/** number of blank lines between methods */
490502
public int $linesBetweenMethods = 2;
503+
/** number of blank lines between groups of use statements for classes, functions, and constants */
491504
public int $linesBetweenUseTypes = 0;
505+
/** position of the opening brace for functions and methods */
492506
public bool $bracesOnNextLine = true;
507+
/** separator between the right parenthesis and return type of functions and methods */
493508
public string $returnTypeColon = ': ';
494509
}
495510
```
@@ -551,11 +566,10 @@ new Literal('substr(?, ?)', [$a, $b]);
551566
```
552567

553568

554-
555569
Attributes
556570
----------
557571

558-
You can add PHP 8 attributes to all classes, methods, properties, constants, enum cases, functions, closures and parameters (class [Attribute](https://api.nette.org/php-generator/master/Nette/PhpGenerator/Attribute.html)).
572+
You can add PHP 8 attributes to all classes, methods, properties, constants, enum cases, functions, closures and parameters. [Literals](#literals) can also be used as parameter values.
559573

560574
```php
561575
$class = new Nette\PhpGenerator\ClassType('Demo');
@@ -590,6 +604,7 @@ class Demo
590604
}
591605
```
592606

607+
593608
Namespace
594609
---------
595610

@@ -635,13 +650,13 @@ echo $namespace->resolveName('Bar'); // 'Foo\Bar'
635650
echo $namespace->resolveName('range', $namespace::NameFunction); // 'iter\range'
636651
```
637652

653+
638654
Class Names Resolving
639655
---------------------
640656

641657
**When the class is part of the namespace, it is rendered slightly differently**: all types (ie. type hints, return types, parent class name,
642658
implemented interfaces, used traits and attributes) are automatically *resolved* (unless you turn it off, see below).
643-
It means that you have to **use full class names** in definitions and they will be replaced
644-
with aliases (according to the use-statements) or fully qualified names in the resulting code:
659+
It means that you have to **use full class names** in definitions and they will be replaced with aliases (according to the use-statements) or fully qualified names in the resulting code:
645660

646661
```php
647662
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
@@ -690,6 +705,7 @@ $printer->setTypeResolving(false);
690705
echo $printer->printNamespace($namespace);
691706
```
692707

708+
693709
PHP Files
694710
---------
695711

@@ -736,6 +752,7 @@ function foo()
736752
}
737753
```
738754

755+
739756
Generating According to Existing Ones
740757
-------------------------------------
741758

@@ -750,7 +767,7 @@ $function = Nette\PhpGenerator\GlobalFunction::from('trim');
750767

751768
// creates a closure as specified
752769
$closure = Nette\PhpGenerator\Closure::from(
753-
function (stdClass $a, $b = null) {}
770+
function (stdClass $a, $b = null) {},
754771
);
755772
```
756773

@@ -763,6 +780,7 @@ $class = Nette\PhpGenerator\ClassType::from(Foo::class, withBodies: true);
763780
$function = Nette\PhpGenerator\GlobalFunction::from('foo', withBody: true);
764781
```
765782

783+
766784
Loading from PHP File
767785
---------------------
768786

@@ -797,7 +815,7 @@ This requires `nikic/php-parser` to be installed.
797815
Variables Dumper
798816
----------------
799817

800-
The Dumper returns a parsable PHP string representation of a variable. Provides better and clearer output that native functon `var_export()`.
818+
The Dumper returns a parsable PHP string representation of a variable. Provides better and clearer output that native function `var_export()`.
801819

802820
```php
803821
$dumper = new Nette\PhpGenerator\Dumper;

0 commit comments

Comments
 (0)