Skip to content

Commit b163fa9

Browse files
committed
added PsrPrinter, compatible with PSR-2 and PSR-12 [Closes #30]
1 parent db3b0b3 commit b163fa9

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

readme.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,20 @@ $class->addMember($methodRecount);
179179
Tabs versus spaces
180180
------------------
181181

182-
The generated code uses tabs for indentation, which makes it very easy to change it to any number of spaces:
182+
The generated code uses tabs for indentation. If you want to have the output compatible with PSR-2 or PSR-12, use `PsrPrinter`:
183183

184184
```php
185-
use Nette\PhpGenerator\Helpers;
185+
$printer = new Nette\PhpGenerator\PsrPrinter;
186186

187187
$class = new Nette\PhpGenerator\ClassType('Demo');
188188
// ...
189189

190-
echo Helpers::tabsToSpaces((string) $class); // 4 spaces indentation
191-
echo Helpers::tabsToSpaces((string) $class, 2); // 2 spaces indentation
190+
echo $printer->printClass($class); // 4 spaces indentation
192191
```
193192

193+
It can be used also for functions, closures, namespaces etc.
194+
195+
194196
Literals
195197
--------
196198

@@ -284,6 +286,9 @@ $function->setBody('return $a + $b;');
284286
$function->addParameter('a');
285287
$function->addParameter('b');
286288
echo $function;
289+
290+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
291+
// echo (new Nette\PhpGenerator\PsrPrinter)->printFunction($function);
287292
```
288293

289294
Result:
@@ -308,6 +313,9 @@ $closure->addParameter('b');
308313
$closure->addUse('c')
309314
->setReference();
310315
echo $closure;
316+
317+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
318+
// echo (new Nette\PhpGenerator\PsrPrinter)->printClosure($closure);
311319
```
312320

313321
Result:
@@ -423,6 +431,9 @@ $method->addParameter('arg')
423431
->setTypeHint('Bar\OtherClass'); // it will resolve to \Bar\OtherClass
424432

425433
echo $namespace;
434+
435+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
436+
// echo (new Nette\PhpGenerator\PsrPrinter)->printNamespace($namespace);
426437
```
427438

428439
Result:
@@ -459,6 +470,9 @@ $class = $namespace->addClass('A');
459470
$class->addMethod('hello');
460471

461472
echo $file;
473+
474+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
475+
// echo (new Nette\PhpGenerator\PsrPrinter)->printFile($file);
462476
```
463477

464478
Result:

src/PhpGenerator/PsrPrinter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\PhpGenerator;
11+
12+
13+
/**
14+
* Generates PHP code compatible with PSR-2 and PSR-12.
15+
*/
16+
class PsrPrinter extends Printer
17+
{
18+
/** @var string */
19+
protected $indentation = ' ';
20+
21+
/** @var int */
22+
protected $linesBetweenMethods = 1;
23+
}

tests/PhpGenerator/PsrPrinter.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\ClassType;
6+
use Nette\PhpGenerator\PhpLiteral;
7+
use Nette\PhpGenerator\PsrPrinter;
8+
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
$printer = new PsrPrinter;
14+
15+
16+
$class = (new ClassType('Example'))
17+
->setFinal(true)
18+
->setExtends('ParentClass')
19+
->addImplement('IExample')
20+
->setTraits(['ObjectTrait'])
21+
->addTrait('AnotherTrait', ['sayHello as protected'])
22+
->addComment("Description of class.\nThis is example\n");
23+
24+
$class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY'))
25+
->setVisibility('private')
26+
->addComment('Commented');
27+
28+
$class->addProperty('handle')
29+
->setVisibility('private')
30+
->addComment('@var resource orignal file handle');
31+
32+
$class->addProperty('order')
33+
->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST'));
34+
35+
$class->addMethod('first')
36+
->addComment('@return resource')
37+
->setFinal(true)
38+
->setReturnType('stdClass')
39+
->setBody('return $this->?;', ['handle'])
40+
->addParameter('var')
41+
->setTypeHint('stdClass');
42+
43+
$class->addMethod('second');
44+
45+
46+
sameFile(__DIR__ . '/expected/PsrPrinter.class.expect', $printer->printClass($class));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Description of class.
3+
* This is example
4+
*/
5+
final class Example extends ParentClass implements IExample
6+
{
7+
use ObjectTrait;
8+
use AnotherTrait {
9+
sayHello as protected;
10+
}
11+
12+
/** Commented */
13+
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
14+
15+
/** @var resource orignal file handle */
16+
private $handle;
17+
18+
public $order = RecursiveIteratorIterator::SELF_FIRST;
19+
20+
/**
21+
* @return resource
22+
*/
23+
final public function first(stdClass $var): stdClass
24+
{
25+
return $this->handle;
26+
}
27+
28+
public function second()
29+
{
30+
}
31+
}

0 commit comments

Comments
 (0)