Skip to content

Commit 995ed5b

Browse files
committed
Printer: added $linesBetweenProperties [Closes #60]
1 parent ea2c8e8 commit 995ed5b

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

src/PhpGenerator/Printer.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class Printer
2323
/** @var string */
2424
protected $indentation = "\t";
2525

26+
/** @var int */
27+
protected $linesBetweenProperties = 0;
28+
2629
/** @var int */
2730
protected $linesBetweenMethods = 2;
2831

@@ -138,8 +141,8 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
138141

139142
$members = array_filter([
140143
implode('', $traits),
141-
preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $consts)),
142-
preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $properties)),
144+
$this->joinProperties($consts),
145+
$this->joinProperties($properties),
143146
($methods && $properties ? str_repeat("\n", $this->linesBetweenMethods - 1) : '')
144147
. implode(str_repeat("\n", $this->linesBetweenMethods), $methods),
145148
]);
@@ -278,4 +281,12 @@ private function printReturnType($function, ?PhpNamespace $namespace): string
278281
? ': ' . $tmp
279282
: '';
280283
}
284+
285+
286+
private function joinProperties(array $props)
287+
{
288+
return $this->linesBetweenProperties
289+
? implode(str_repeat("\n", $this->linesBetweenProperties), $props)
290+
: preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $props));
291+
}
281292
}

tests/PhpGenerator/Printer.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ sameFile(__DIR__ . '/expected/Printer.class.expect', $printer->printClass($class
5454
sameFile(__DIR__ . '/expected/Printer.method.expect', $printer->printMethod($class->getMethod('first')));
5555

5656

57+
Assert::with($printer, function () {
58+
$this->linesBetweenProperties = 1;
59+
$this->linesBetweenMethods = 3;
60+
});
61+
sameFile(__DIR__ . '/expected/Printer.class-alt.expect', $printer->printClass($class));
62+
63+
64+
5765
$function = new Nette\PhpGenerator\GlobalFunction('func');
5866
$function
5967
->setReturnType('stdClass')
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
const MULTILINE_LONG = [
16+
'aaaaaaaa' => 1,
17+
'bbbbbbbb' => 2,
18+
'cccccccc' => 3,
19+
'dddddddd' => 4,
20+
'eeeeeeee' => 5,
21+
'ffffffff' => 6,
22+
];
23+
24+
const SHORT = ['aaaaaaaa' => 1, 'bbbbbbbb' => 2, 'cccccccc' => 3, 'dddddddd' => 4, 'eeeeeeee' => 5, 'ffffffff' => 6];
25+
26+
/** @var resource orignal file handle */
27+
private $handle;
28+
29+
public $order = RecursiveIteratorIterator::SELF_FIRST;
30+
31+
public $multilineLong = [
32+
'aaaaaaaa' => 1,
33+
'bbbbbbbb' => 2,
34+
'cccccccc' => 3,
35+
'dddddddd' => 4,
36+
'eeeeeeee' => 5,
37+
'ffffffff' => 6,
38+
];
39+
40+
public $short = ['aaaaaaaa' => 1, 'bbbbbbbb' => 2, 'cccccccc' => 3, 'dddddddd' => 4, 'eeeeeeee' => 5, 'ffffffff' => 6];
41+
42+
43+
44+
/**
45+
* @return resource
46+
*/
47+
final public function first(stdClass $var): stdClass
48+
{
49+
func();
50+
return [
51+
'aaaaaaaaaaaa' => 1,
52+
'bbbbbbbbbbb' => 2,
53+
'cccccccccccccc' => 3,
54+
'dddddddddddd' => 4,
55+
'eeeeeeeeeeee' => 5,
56+
'ffffffff' => 6,
57+
];
58+
}
59+
60+
61+
62+
public function second()
63+
{
64+
}
65+
}

0 commit comments

Comments
 (0)