You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+26-11Lines changed: 26 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -477,40 +477,55 @@ function foo($a)
477
477
Printers and PSR Compliance
478
478
---------------------------
479
479
480
-
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.
480
+
The [Printer](https://api.nette.org/php-generator/master/Nette/PhpGenerator/Printer.html) class is used to generate PHP code:
481
481
482
482
```php
483
483
$class = new Nette\PhpGenerator\ClassType('Demo');
484
484
// ...
485
485
486
+
$printer = new Nette\PhpGenerator\Printer;
487
+
echo $printer->printClass($class); // same as: echo $class
488
+
```
489
+
490
+
It can generate code for all other elements, offering methods such as `printFunction()`, `printNamespace()`, etc.
491
+
492
+
Additionally, the `PsrPrinter` class is available, whose output is in compliance with the PSR-2 / PSR-12 / PER coding style:
Need to customize printer behavior? Create your own by inheriting the `Printer` class. You can reconfigure these variables:
499
+
Need to fine-tune behavior to your needs? Create your own printer by inheriting from the `Printer` class. You can reconfigure these variables:
491
500
492
501
```php
493
502
class MyPrinter extends Nette\PhpGenerator\Printer
494
503
{
495
-
/** length of the line after which the line will break */
504
+
// length of the line after which the line will break
496
505
public int $wrapLength = 120;
497
-
/** indentation character, can be replaced with a sequence of spaces */
506
+
// indentation character, can be replaced with a sequence of spaces
498
507
public string $indentation = "\t";
499
-
/** number of blank lines between properties */
508
+
// number of blank lines between properties
500
509
public int $linesBetweenProperties = 0;
501
-
/** number of blank lines between methods */
510
+
// number of blank lines between methods
502
511
public int $linesBetweenMethods = 2;
503
-
/** number of blank lines between groups of use statements for classes, functions, and constants */
512
+
// number of blank lines between groups of use statements for classes, functions, and constants
504
513
public int $linesBetweenUseTypes = 0;
505
-
/** position of the opening brace for functions and methods */
514
+
// position of the opening brace for functions and methods
506
515
public bool $bracesOnNextLine = true;
507
-
/** place one parameter in one line, even if it has an attribute or is promoted */
516
+
// place one parameter in one line, even if it has an attribute or is promoted
508
517
public bool $singleParameterOnOneLine = false;
509
-
/** separator between the right parenthesis and return type of functions and methods */
518
+
// separator between the right parenthesis and return type of functions and methods
510
519
public string $returnTypeColon = ': ';
511
520
}
512
521
```
513
522
523
+
How and why exactly does the standard `Printer` and `PsrPrinter` differ? Why isn't there just one printer, the `PsrPrinter`, in the package?
524
+
525
+
The standard `Printer` formats the code as we do it in all of Nette. Since Nette was created much earlier than PSR, and also because PSR for many years did not deliver standards in time, but sometimes even with several years of delay from the introduction of a new feature in PHP, this resulted in a few minor differences in the coding standard.
526
+
The bigger difference is just the use of tabs instead of spaces. We know that by using tabs in our projects we allow for width adjustment, which is [essential for people with visual impairments](https://doc.nette.org/en/contributing/coding-standard#toc-tabs-instead-of-spaces).
527
+
An example of a minor difference is the placement of the curly brace on a separate line for functions and methods and always. We see the PSR recommendation as illogical and [leading to a decrease in code clarity](https://doc.nette.org/en/contributing/coding-standard#toc-wrapping-and-braces).
0 commit comments