Skip to content

Commit fef1284

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

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

readme.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,20 @@ class Demo
154154
Tabs versus spaces
155155
------------------
156156

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

159159
```php
160-
use Nette\PhpGenerator\Helpers;
160+
$printer = new Nette\PhpGenerator\PsrPrinter;
161161

162162
$class = new Nette\PhpGenerator\ClassType('Demo');
163163
// ...
164164

165-
echo Helpers::tabsToSpaces((string) $class); // 4 spaces indentation
166-
echo Helpers::tabsToSpaces((string) $class, 2); // 2 spaces indentation
165+
echo $printer->printClass($class); // 4 spaces indentation
167166
```
168167

168+
It can be used also for functions, closures, namespaces etc.
169+
170+
169171
Literals
170172
--------
171173

@@ -259,6 +261,9 @@ $function->setBody('return $a + $b;');
259261
$function->addParameter('a');
260262
$function->addParameter('b');
261263
echo $function;
264+
265+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
266+
// echo (new Nette\PhpGenerator\PsrPrinter)->printFunction($function);
262267
```
263268

264269
Result:
@@ -283,6 +288,9 @@ $closure->addParameter('b');
283288
$closure->addUse('c')
284289
->setReference();
285290
echo $closure;
291+
292+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
293+
// echo (new Nette\PhpGenerator\PsrPrinter)->printClosure($closure);
286294
```
287295

288296
Result:
@@ -372,6 +380,9 @@ $method->addParameter('arg')
372380
->setTypeHint('Bar\OtherClass'); // resolves to \Bar\OtherClass
373381

374382
echo $namespace;
383+
384+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
385+
// echo (new Nette\PhpGenerator\PsrPrinter)->printNamespace($namespace);
375386
```
376387

377388
Result:
@@ -405,6 +416,9 @@ $class = $namespace->addClass('A');
405416
$class->addMethod('hello');
406417

407418
echo $file;
419+
420+
// or use PsrPrinter for output compatible with PSR-2 / PSR-12
421+
// echo (new Nette\PhpGenerator\PsrPrinter)->printFile($file);
408422
```
409423

410424
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
$printer = new PsrPrinter;
15+
16+
17+
$class = (new ClassType('Example'))
18+
->setAbstract(true)
19+
->setFinal(true)
20+
->setExtends('ParentClass')
21+
->addImplement('IExample')
22+
->setTraits(['ObjectTrait'])
23+
->addTrait('AnotherTrait', ['sayHello as protected'])
24+
->addComment("Description of class.\nThis is example\n");
25+
26+
$class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY'))
27+
->setVisibility('private')
28+
->addComment('Commented');
29+
30+
$class->addProperty('handle')
31+
->setVisibility('private')
32+
->addComment('@var resource orignal file handle');
33+
34+
$class->addProperty('order')
35+
->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST'));
36+
37+
$class->addMethod('first')
38+
->addComment('@return resource')
39+
->setFinal(true)
40+
->setReturnType('stdClass')
41+
->setBody('return $this->?;', ['handle'])
42+
->addParameter('var')
43+
->setTypeHint('stdClass');
44+
45+
$class->addMethod('second');
46+
47+
48+
Assert::matchFile(__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+
abstract 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)