Skip to content

Commit

Permalink
readme: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 30, 2023
1 parent 34aaf23 commit a9a4048
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Let's start with a straightforward example of generating class using [ClassType]
```php
$class = new Nette\PhpGenerator\ClassType('Demo');

$class->setFinal()
$class
->setFinal()
->setExtends(ParentClass::class)
->addImplement(Countable::class)
->addComment("Description of class.\nSecond line\n")
Expand Down Expand Up @@ -171,6 +172,7 @@ $methodRecount = $methodCount->cloneWithName('recount');
$class->addMember($methodRecount);
```


Interface or Trait
------------------

Expand Down Expand Up @@ -267,6 +269,7 @@ $obj = new class ($val) {
};
```


Global Function
---------------

Expand All @@ -292,6 +295,7 @@ function foo($a, $b)
}
```


Closure
-------

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


Arrow Function
--------------

Expand All @@ -338,6 +343,7 @@ Result:
fn($a, $b) => $a + $b
```


Method and Function Signature
-----------------------------

Expand Down Expand Up @@ -377,6 +383,7 @@ function count(...$items)
}
```


Method and Function Body
------------------------

Expand Down Expand Up @@ -466,7 +473,8 @@ function foo($a)
}
```

Printers and PSR compliance

Printers and PSR Compliance
---------------------------

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.
Expand All @@ -484,12 +492,19 @@ Need to customize printer behavior? Create your own by inheriting the `Printer`
```php
class MyPrinter extends Nette\PhpGenerator\Printer
{
/** length of the line after which the line will break */
public int $wrapLength = 120;
/** indentation character, can be replaced with a sequence of spaces */
public string $indentation = "\t";
/** number of blank lines between properties */
public int $linesBetweenProperties = 0;
/** number of blank lines between methods */
public int $linesBetweenMethods = 2;
/** number of blank lines between groups of use statements for classes, functions, and constants */
public int $linesBetweenUseTypes = 0;
/** position of the opening brace for functions and methods */
public bool $bracesOnNextLine = true;
/** separator between the right parenthesis and return type of functions and methods */
public string $returnTypeColon = ': ';
}
```
Expand Down Expand Up @@ -551,11 +566,10 @@ new Literal('substr(?, ?)', [$a, $b]);
```



Attributes
----------

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)).
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.

```php
$class = new Nette\PhpGenerator\ClassType('Demo');
Expand Down Expand Up @@ -590,6 +604,7 @@ class Demo
}
```


Namespace
---------

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


Class Names Resolving
---------------------

**When the class is part of the namespace, it is rendered slightly differently**: all types (ie. type hints, return types, parent class name,
implemented interfaces, used traits and attributes) are automatically *resolved* (unless you turn it off, see below).
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:
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:

```php
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
Expand Down Expand Up @@ -690,6 +705,7 @@ $printer->setTypeResolving(false);
echo $printer->printNamespace($namespace);
```


PHP Files
---------

Expand Down Expand Up @@ -736,6 +752,7 @@ function foo()
}
```


Generating According to Existing Ones
-------------------------------------

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

// creates a closure as specified
$closure = Nette\PhpGenerator\Closure::from(
function (stdClass $a, $b = null) {}
function (stdClass $a, $b = null) {},
);
```

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


Loading from PHP File
---------------------

Expand Down Expand Up @@ -797,7 +815,7 @@ This requires `nikic/php-parser` to be installed.
Variables Dumper
----------------

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

```php
$dumper = new Nette\PhpGenerator\Dumper;
Expand Down

0 comments on commit a9a4048

Please sign in to comment.