Skip to content

Commit

Permalink
readme: improved info about PSR
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 22, 2023
1 parent de1843f commit b2aebf0
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,40 +477,55 @@ function foo($a)
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.
The [Printer](https://api.nette.org/php-generator/master/Nette/PhpGenerator/Printer.html) class is used to generate PHP code:

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

$printer = new Nette\PhpGenerator\Printer;
echo $printer->printClass($class); // same as: echo $class
```

It can generate code for all other elements, offering methods such as `printFunction()`, `printNamespace()`, etc.

Additionally, the `PsrPrinter` class is available, whose output is in compliance with the PSR-2 / PSR-12 / PER coding style:

```php
$printer = new Nette\PhpGenerator\PsrPrinter;
echo $printer->printClass($class); // 4 spaces indentation
echo $printer->printClass($class);
```

Need to customize printer behavior? Create your own by inheriting the `Printer` class. You can reconfigure these variables:
Need to fine-tune behavior to your needs? Create your own printer by inheriting from the `Printer` class. You can reconfigure these variables:

```php
class MyPrinter extends Nette\PhpGenerator\Printer
{
/** length of the line after which the line will break */
// length of the line after which the line will break
public int $wrapLength = 120;
/** indentation character, can be replaced with a sequence of spaces */
// indentation character, can be replaced with a sequence of spaces
public string $indentation = "\t";
/** number of blank lines between properties */
// number of blank lines between properties
public int $linesBetweenProperties = 0;
/** number of blank lines between methods */
// number of blank lines between methods
public int $linesBetweenMethods = 2;
/** number of blank lines between groups of use statements for classes, functions, and constants */
// 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 */
// position of the opening brace for functions and methods
public bool $bracesOnNextLine = true;
/** place one parameter in one line, even if it has an attribute or is promoted */
// place one parameter in one line, even if it has an attribute or is promoted
public bool $singleParameterOnOneLine = false;
/** separator between the right parenthesis and return type of functions and methods */
// separator between the right parenthesis and return type of functions and methods
public string $returnTypeColon = ': ';
}
```

How and why exactly does the standard `Printer` and `PsrPrinter` differ? Why isn't there just one printer, the `PsrPrinter`, in the package?

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


Types
-----
Expand Down

0 comments on commit b2aebf0

Please sign in to comment.