Skip to content

Commit b2aebf0

Browse files
committed
readme: improved info about PSR
1 parent de1843f commit b2aebf0

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

readme.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,40 +477,55 @@ function foo($a)
477477
Printers and PSR Compliance
478478
---------------------------
479479

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:
481481

482482
```php
483483
$class = new Nette\PhpGenerator\ClassType('Demo');
484484
// ...
485485

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:
493+
494+
```php
486495
$printer = new Nette\PhpGenerator\PsrPrinter;
487-
echo $printer->printClass($class); // 4 spaces indentation
496+
echo $printer->printClass($class);
488497
```
489498

490-
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:
491500

492501
```php
493502
class MyPrinter extends Nette\PhpGenerator\Printer
494503
{
495-
/** length of the line after which the line will break */
504+
// length of the line after which the line will break
496505
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
498507
public string $indentation = "\t";
499-
/** number of blank lines between properties */
508+
// number of blank lines between properties
500509
public int $linesBetweenProperties = 0;
501-
/** number of blank lines between methods */
510+
// number of blank lines between methods
502511
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
504513
public int $linesBetweenUseTypes = 0;
505-
/** position of the opening brace for functions and methods */
514+
// position of the opening brace for functions and methods
506515
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
508517
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
510519
public string $returnTypeColon = ': ';
511520
}
512521
```
513522

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).
528+
514529

515530
Types
516531
-----

0 commit comments

Comments
 (0)